@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 167 lines 5.55 KB
Raw
1 import * as Cloudflare from "@alchemy.run/cloudflare";
2 import { CloudflareEnvironment } from "@alchemy.run/cloudflare/CloudflareEnvironment";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as stream from "@distilled.cloud/cloudflare/stream";
6 import * as Provider from "alchemy/Provider";
7 import * as Effect from "effect/Effect";
8 import { MinimumLogLevel } from "effect/References";
9 import * as Schedule from "effect/Schedule";
10 import { testName } from "../../names.ts";
11
12 const { test } = Test.make({ providers: Cloudflare.providers() });
13
14 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
15
16 /**
17 * A small, stable, publicly hosted PNG for Cloudflare to download
18 * (watermark images must be PNG, <= 2 MiB).
19 */
20 const PNG_URL =
21 "https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png";
22
23 // Ride out 403 blips (`Forbidden`) from scoped-token propagation on the
24 // test's own out-of-band verification calls.
25 const getWatermark = (accountId: string, watermarkId: string) =>
26 stream.getWatermark({ accountId, identifier: watermarkId }).pipe(
27 Effect.retry({
28 while: (e) => e._tag === "Forbidden",
29 schedule: Schedule.exponential("500 millis"),
30 times: 8,
31 }),
32 );
33
34 const expectGone = (accountId: string, watermarkId: string) =>
35 getWatermark(accountId, watermarkId).pipe(
36 Effect.flatMap(() => Effect.fail({ _tag: "WatermarkNotDeleted" } as const)),
37 // A missing watermark surfaces as `WatermarkNotFound` (Cloudflare
38 // error code 10003) — that's the success condition here.
39 Effect.catchTag("WatermarkNotFound", () => Effect.void),
40 Effect.retry({
41 while: (e) => e._tag === "WatermarkNotDeleted",
42 schedule: Schedule.max([Schedule.exponential("500 millis"), Schedule.recurs(10)]),
43 }),
44 );
45
46 test.provider(
47 "create and delete a watermark with default name",
48 (stack) =>
49 Effect.gen(function* () {
50 const { accountId } = yield* yield* CloudflareEnvironment;
51
52 yield* stack.destroy();
53
54 const watermark = yield* stack.deploy(
55 Cloudflare.Stream.Watermark("DefaultWatermark", {
56 url: PNG_URL,
57 }),
58 );
59
60 expect(watermark.watermarkId).toBeTruthy();
61 expect(watermark.accountId).toEqual(accountId);
62 expect(watermark.downloadedFrom).toEqual(PNG_URL);
63 expect(watermark.position).toEqual("upperRight");
64 expect(watermark.opacity).toEqual(1.0);
65 expect(watermark.width).toBeGreaterThan(0);
66
67 const live = yield* getWatermark(accountId, watermark.watermarkId);
68 expect(live.uid).toEqual(watermark.watermarkId);
69 expect(live.name).toEqual(watermark.name);
70
71 yield* stack.destroy();
72
73 yield* expectGone(accountId, watermark.watermarkId);
74 }).pipe(logLevel),
75 {
76 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
77 timeout: 120_000,
78 },
79 );
80
81 test.provider(
82 "prop change replaces the watermark (create-only resource)",
83 (stack) =>
84 Effect.gen(function* () {
85 const { accountId } = yield* yield* CloudflareEnvironment;
86
87 yield* stack.destroy();
88
89 const initial = yield* stack.deploy(
90 Cloudflare.Stream.Watermark("ReplaceWatermark", {
91 name: testName("alchemy-stream-wm-replace"),
92 url: PNG_URL,
93 }),
94 );
95
96 expect(initial.position).toEqual("upperRight");
97
98 // No update endpoint — changing any prop must replace (new uid).
99 const replaced = yield* stack.deploy(
100 Cloudflare.Stream.Watermark("ReplaceWatermark", {
101 name: testName("alchemy-stream-wm-replace"),
102 url: PNG_URL,
103 position: "center",
104 opacity: 0.5,
105 }),
106 );
107
108 expect(replaced.watermarkId).not.toEqual(initial.watermarkId);
109 expect(replaced.position).toEqual("center");
110 expect(replaced.opacity).toEqual(0.5);
111
112 const live = yield* getWatermark(accountId, replaced.watermarkId);
113 expect(live.position).toEqual("center");
114 expect(live.opacity).toEqual(0.5);
115
116 // The old watermark must be gone after the replacement.
117 yield* expectGone(accountId, initial.watermarkId);
118
119 // Redeploying identical props is a no-op (same uid).
120 const noop = yield* stack.deploy(
121 Cloudflare.Stream.Watermark("ReplaceWatermark", {
122 name: testName("alchemy-stream-wm-replace"),
123 url: PNG_URL,
124 position: "center",
125 opacity: 0.5,
126 }),
127 );
128 expect(noop.watermarkId).toEqual(replaced.watermarkId);
129
130 yield* stack.destroy();
131
132 yield* expectGone(accountId, replaced.watermarkId);
133 }).pipe(logLevel),
134 {
135 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
136 timeout: 120_000,
137 },
138 );
139
140 // Canonical `list()` test (account collection): deploy a watermark, then
141 // enumerate every watermark profile in the account via the provider's
142 // `list()` and assert the deployed uid is present in the exhaustively
143 // paginated result.
144 test.provider(
145 "list enumerates the deployed watermark",
146 (stack) =>
147 Effect.gen(function* () {
148 yield* stack.destroy();
149
150 const deployed = yield* stack.deploy(
151 Cloudflare.Stream.Watermark("ListWatermark", {
152 url: PNG_URL,
153 }),
154 );
155
156 const provider = yield* Provider.findProvider(Cloudflare.Stream.Watermark);
157 const all = yield* provider.list();
158
159 expect(all.some((w) => w.watermarkId === deployed.watermarkId)).toBe(true);
160
161 yield* stack.destroy();
162 }).pipe(logLevel),
163 {
164 tags: ["provider:cloudflare", "provider:cloudflare:stream", "live"],
165 timeout: 120_000,
166 },
167 );