@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 139 lines 4.6 KB
Raw
1 /**
2 * Repro for the workers.dev URL regression under `alchemy dev`:
3 * a Worker previously deployed live (stamped `providerMode: "live"`) is
4 * replaced live → local by a dev run. The stack output referencing
5 * `worker.url` must resolve to the LOCAL dev server URL (localhost), not
6 * the deployed workers.dev / custom-domain URL.
7 */
8 import * as Cloudflare from "@alchemy.run/cloudflare";
9 import { expect } from "@alchemy.run/test";
10 import * as Test from "@alchemy.run/test/Alchemy";
11 import { Stack } from "alchemy/Stack";
12 import { State, type ResourceState } from "alchemy/State";
13 import * as Effect from "effect/Effect";
14 import { testName } from "../../names.ts";
15 import { inDev } from "../../test.resources.ts";
16
17 const { test } = Test.make({ providers: Cloudflare.providers() });
18
19 const script = `export default {
20 async fetch() {
21 return Response.json({ ok: true });
22 },
23 };`;
24
25 test.provider(
26 "worker.url resolves to localhost after a live → local mode switch",
27 (stack) =>
28 Effect.gen(function* () {
29 yield* stack.destroy();
30
31 const program = Effect.gen(function* () {
32 const worker = yield* Cloudflare.Worker("url-mode-switch-worker", {
33 script,
34 });
35 return { url: worker.url, urls: worker.urls };
36 });
37
38 // 1. live deploy: url is the workers.dev URL, row stamped live.
39 const live = yield* stack.deploy(program);
40 expect(String(live.url)).toContain("workers.dev");
41
42 // 2. dev run over the same stage: live → local replacement. The
43 // stack output must now resolve to the local dev server URL.
44 const dev = yield* inDev(stack.deploy(program));
45 expect(String(dev.url)).toContain("localhost");
46
47 yield* stack.destroy();
48 }),
49 {
50 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
51 timeout: 240_000,
52 },
53 );
54
55 /**
56 * A state row written before provider modes existed (pre-beta.66) has no
57 * `providerMode` stamp. Such a row was necessarily deployed LIVE — the old
58 * engine had no local mode. A dev run must therefore treat it as live and
59 * plan the live → local replacement; assuming it is "already local" leaves
60 * the deployed cloud attrs (workers.dev / custom-domain URL) masquerading
61 * as the local instance and never starts a dev server.
62 */
63 test.provider(
64 "a legacy (unstamped) live row is replaced by a dev run, not assumed local",
65 (stack) =>
66 Effect.gen(function* () {
67 yield* stack.destroy();
68
69 const program = Effect.gen(function* () {
70 const worker = yield* Cloudflare.Worker("url-legacy-row-worker", {
71 script,
72 });
73 return { url: worker.url, urls: worker.urls };
74 });
75
76 // 1. live deploy, then simulate a pre-providerMode row.
77 const live = yield* stack.deploy(program);
78 expect(String(live.url)).toContain("workers.dev");
79
80 const fqn = "url-legacy-row-worker";
81 const state = yield* yield* State;
82 const stk = yield* Stack;
83 const row = (yield* state.get({
84 stack: stk.name,
85 stage: stk.stage,
86 fqn,
87 })) as ResourceState;
88 yield* state.set({
89 stack: stk.name,
90 stage: stk.stage,
91 fqn,
92 value: { ...row, providerMode: undefined },
93 });
94
95 // 2. dev run: the worker must come up locally.
96 const dev = yield* inDev(stack.deploy(program));
97 expect(String(dev.url)).toContain("localhost");
98
99 yield* stack.destroy();
100 }),
101 {
102 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
103 timeout: 240_000,
104 },
105 );
106
107 const zoneName = process.env.CLOUDFLARE_TEST_DNS_ZONE_NAME ?? "alchemy-test-2.us";
108
109 test.provider(
110 "worker.url with a custom domain resolves to localhost after live → local",
111 (stack) =>
112 Effect.gen(function* () {
113 yield* stack.destroy();
114
115 const hostname = `${testName("url-mode-switch")}.${zoneName}`;
116 const program = Effect.gen(function* () {
117 const worker = yield* Cloudflare.Worker("url-mode-switch-domain", {
118 script,
119 domain: hostname,
120 });
121 return { url: worker.url, urls: worker.urls };
122 });
123
124 // 1. live deploy: url is the custom domain, row stamped live.
125 const live = yield* stack.deploy(program);
126 expect(String(live.url)).toBe(`https://${hostname}`);
127
128 // 2. dev run: live → local replacement. The output must resolve to
129 // the local dev server URL, not the deployed custom domain.
130 const dev = yield* inDev(stack.deploy(program));
131 expect(String(dev.url)).toContain("localhost");
132
133 yield* stack.destroy();
134 }),
135 {
136 tags: ["provider:cloudflare", "provider:cloudflare:worker", "live"],
137 timeout: 240_000,
138 },
139 );