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

main
ts 144 lines 4.91 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { PROBE_NAME, PROBE_PARENT } from "@alchemy.run/gcp/RealTimeBidding/internal";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as rtb from "@distilled.cloud/gcp/realtimebidding_v1";
6 import * as Effect from "effect/Effect";
7 import { MinimumLogLevel } from "effect/References";
8 import * as Schedule from "effect/Schedule";
9
10 const { test } = Test.make({ providers: GCP.providers() });
11
12 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
13
14 const waitUntilGone = (name: string) =>
15 rtb.getBiddersPretargetingConfigs({ name }).pipe(
16 Effect.as("found" as const),
17 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
18 Effect.repeat({
19 schedule: Schedule.spaced("1 second"),
20 until: (status) => status === "gone",
21 times: 10,
22 }),
23 );
24
25 const probeCreate = (parent: string) =>
26 rtb.createBiddersPretargetingConfigs({
27 parent,
28 body: { displayName: "alchemy-rtb-probe" },
29 });
30
31 // Real-time Bidding needs a bidder account and the realtime-bidding OAuth
32 // scope; the service account profile gets InsufficientScopes. Set
33 // GCP_TEST_REALTIMEBIDDING_PARENT=bidders/{id} with such credentials.
34 const parent = process.env.GCP_TEST_REALTIMEBIDDING_PARENT?.trim();
35 const runLifecycle = !!parent && !process.env.FAST;
36
37 test.provider.skipIf(runLifecycle)(
38 "getBiddersPretargetingConfigs without the Real-time Bidding scope fails with InsufficientScopes",
39 (stack) =>
40 Effect.gen(function* () {
41 yield* stack.destroy();
42
43 const error = yield* Effect.flip(rtb.getBiddersPretargetingConfigs({ name: PROBE_NAME }));
44 expect(error._tag).toEqual("InsufficientScopes");
45
46 yield* stack.destroy();
47 }).pipe(logLevel),
48 {
49 tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"],
50 timeout: 90_000,
51 },
52 );
53
54 test.provider.skipIf(runLifecycle)(
55 "createBiddersPretargetingConfigs without the Real-time Bidding scope fails with InsufficientScopes",
56 (stack) =>
57 Effect.gen(function* () {
58 yield* stack.destroy();
59
60 const error = yield* Effect.flip(probeCreate(PROBE_PARENT));
61 expect(error._tag).toEqual("InsufficientScopes");
62
63 yield* stack.destroy();
64 }).pipe(logLevel),
65 {
66 tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"],
67 timeout: 90_000,
68 },
69 );
70
71 test.provider.skipIf(!runLifecycle)(
72 "create, update, and delete a pretargeting config",
73 (stack) =>
74 Effect.gen(function* () {
75 yield* stack.destroy();
76
77 const created = yield* stack.deploy(
78 Effect.gen(function* () {
79 return yield* GCP.RealTimeBidding.BiddersPretargetingConfig("WebHtml", {
80 parent: parent!,
81 displayName: "web-html",
82 includedEnvironments: ["WEB"],
83 includedFormats: ["HTML"],
84 });
85 }),
86 );
87
88 expect(created.name).toContain("/pretargetingConfigs/");
89 expect(created.parent).toEqual(parent);
90 expect(created.configId.length).toBeGreaterThan(0);
91 expect(created.displayName).toEqual("web-html");
92 expect(created.includedEnvironments).toContain("WEB");
93 expect(created.includedFormats).toContain("HTML");
94 expect(["ACTIVE", "SUSPENDED"]).toContain(created.state);
95
96 const fetched = yield* rtb.getBiddersPretargetingConfigs({
97 name: created.name,
98 });
99 expect(fetched.name).toEqual(created.name);
100 expect(fetched.displayName).toContain("[alchemy ");
101 expect(fetched.displayName).toContain("web-html");
102 expect(fetched.includedFormats).toContain("HTML");
103
104 const listed = yield* rtb.listBiddersPretargetingConfigs({
105 parent: parent!,
106 pageSize: 100,
107 });
108 expect((listed.pretargetingConfigs ?? []).some((row) => row.name === created.name)).toEqual(
109 true,
110 );
111
112 const updated = yield* stack.deploy(
113 Effect.gen(function* () {
114 return yield* GCP.RealTimeBidding.BiddersPretargetingConfig("WebHtml", {
115 parent: created.parent,
116 configId: created.configId,
117 displayName: "web-html-v2",
118 includedEnvironments: ["WEB"],
119 includedFormats: ["HTML", "NATIVE"],
120 });
121 }),
122 );
123
124 expect(updated.name).toEqual(created.name);
125 expect(updated.displayName).toEqual("web-html-v2");
126 expect(updated.includedFormats).toContain("NATIVE");
127
128 const fetchedUpdate = yield* rtb.getBiddersPretargetingConfigs({
129 name: updated.name,
130 });
131 expect(fetchedUpdate.displayName).toContain("web-html-v2");
132 expect(fetchedUpdate.displayName).toContain("[alchemy ");
133 expect(fetchedUpdate.includedFormats).toContain("NATIVE");
134
135 yield* stack.destroy();
136
137 const gone = yield* waitUntilGone(updated.name);
138 expect(gone).toEqual("gone");
139 }).pipe(logLevel),
140 {
141 tags: ["provider:gcp", "provider:gcp:realtimebidding", "live"],
142 timeout: 90_000,
143 },
144 );