The best way to control Claude Code, Codex and more from any computer or phone

main
ts 150 lines 4.62 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as siteVerification from "@distilled.cloud/gcp/siteVerification_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 // Site Verification needs a credential with the siteverification OAuth
14 // scope (a Cloud Platform service-account token is rejected with
15 // InsufficientAuthenticationScopes) and a site whose verification token is
16 // already placed; set GCP_TEST_SITE_VERIFICATION=1 when both hold.
17 const runLifecycle = !process.env.FAST && process.env.GCP_TEST_SITE_VERIFICATION === "1";
18
19 const identifier = "https://alchemy-site-verification.test/";
20
21 const waitUntilGone = (webResourceId: string) =>
22 siteVerification
23 .getWebResource({
24 id: decodeURIComponent(webResourceId),
25 })
26 .pipe(
27 Effect.as("found" as const),
28 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
29 Effect.repeat({
30 schedule: Schedule.spaced("1 second"),
31 until: (status) => status === "gone",
32 times: 10,
33 }),
34 );
35
36 test.provider.skipIf(!runLifecycle)(
37 "getWebResource on a missing site fails with NotFound",
38 (stack) =>
39 Effect.gen(function* () {
40 yield* stack.destroy();
41
42 const error = yield* Effect.flip(
43 siteVerification.getWebResource({
44 id: "http://alchemy-missing.example.com/",
45 }),
46 );
47 expect(error._tag).toEqual("NotFound");
48
49 yield* stack.destroy();
50 }).pipe(logLevel),
51 {
52 tags: ["provider:gcp", "provider:gcp:siteverification", "live"],
53 timeout: 90_000,
54 },
55 );
56
57 test.provider.skipIf(runLifecycle)(
58 "getTokenWebResource without the siteverification scope fails with InsufficientAuthenticationScopes",
59 (stack) =>
60 Effect.gen(function* () {
61 yield* stack.destroy();
62
63 const error = yield* Effect.flip(
64 siteVerification.getTokenWebResource({
65 body: {
66 site: { identifier, type: "SITE" },
67 verificationMethod: "FILE",
68 },
69 }),
70 );
71 expect(error._tag).toEqual("InsufficientAuthenticationScopes");
72
73 yield* stack.destroy();
74 }).pipe(logLevel),
75 {
76 tags: ["provider:gcp", "provider:gcp:siteverification", "live"],
77 timeout: 90_000,
78 },
79 );
80
81 test.provider.skipIf(runLifecycle)(
82 "insertWebResource without the siteverification scope fails with InsufficientAuthenticationScopes",
83 (stack) =>
84 Effect.gen(function* () {
85 yield* stack.destroy();
86
87 const error = yield* Effect.flip(
88 siteVerification.insertWebResource({
89 verificationMethod: "FILE",
90 body: { site: { identifier, type: "SITE" } },
91 }),
92 );
93 expect(error._tag).toEqual("InsufficientAuthenticationScopes");
94
95 yield* stack.destroy();
96 }).pipe(logLevel),
97 {
98 tags: ["provider:gcp", "provider:gcp:siteverification", "live"],
99 timeout: 90_000,
100 },
101 );
102
103 test.provider.skipIf(!runLifecycle)(
104 "create, update, and delete a web resource",
105 (stack) =>
106 Effect.gen(function* () {
107 yield* stack.destroy();
108
109 const resource = yield* stack.deploy(
110 Effect.gen(function* () {
111 return yield* GCP.SiteVerification.WebResource("Docs", {
112 identifier,
113 siteType: "SITE",
114 verificationMethod: "FILE",
115 });
116 }),
117 );
118 expect(resource.webResourceId.length).toBeGreaterThan(0);
119 expect(resource.identifier).toContain("alchemy-site-verification.test");
120 expect(resource.siteType).toEqual("SITE");
121
122 const fetched = yield* siteVerification.getWebResource({
123 id: decodeURIComponent(resource.webResourceId),
124 });
125 expect(fetched.id).toEqual(resource.webResourceId);
126 expect(fetched.site?.type).toEqual("SITE");
127
128 const updated = yield* stack.deploy(
129 Effect.gen(function* () {
130 return yield* GCP.SiteVerification.WebResource("Docs", {
131 identifier: resource.identifier,
132 siteType: "SITE",
133 owners: resource.owners,
134 });
135 }),
136 );
137
138 expect(updated.webResourceId).toEqual(resource.webResourceId);
139 expect(updated.owners).toEqual(resource.owners);
140
141 yield* stack.destroy();
142
143 const gone = yield* waitUntilGone(resource.webResourceId);
144 expect(gone).toEqual("gone");
145 }).pipe(logLevel),
146 {
147 tags: ["provider:gcp", "provider:gcp:siteverification", "live"],
148 timeout: 90_000,
149 },
150 );