@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 212 lines 7.38 KB
Raw
1 import * as Docker from "@alchemy.run/docker";
2 import { describe, expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Provider from "alchemy/Provider";
5 import { inMemoryState } from "alchemy/State";
6 import * as Effect from "effect/Effect";
7
8 const { test } = Test.make({ providers: Docker.providers(), state: inMemoryState() });
9
10 test.provider(
11 "diff replaces a context when name changes",
12 () =>
13 Effect.gen(function* () {
14 const contextProvider = yield* Provider.findProvider(Docker.Context);
15 const contextDiff = yield* contextProvider.diff!({
16 id: "build-context",
17 fqn: "build-context",
18 instanceId: "instance",
19 olds: { name: "old-context", description: "old" },
20 news: { name: "new-context", description: "old" },
21 oldBindings: [],
22 newBindings: [],
23 output: { id: "old-context", name: "old-context", description: "old", docker: undefined },
24 });
25 expect(contextDiff).toEqual({ action: "replace", deleteFirst: true });
26 }),
27 { tags: ["provider:docker", "provider:docker:context", "local"] },
28 );
29
30 test.provider(
31 "diff updates a context when description changes",
32 () =>
33 Effect.gen(function* () {
34 const contextProvider = yield* Provider.findProvider(Docker.Context);
35 const contextDiff = yield* contextProvider.diff!({
36 id: "build-context",
37 fqn: "build-context",
38 instanceId: "instance",
39 olds: {
40 name: "build-context",
41 description: "old description",
42 docker: "host=unix:///var/run/docker.sock",
43 },
44 news: {
45 name: "build-context",
46 description: "new description",
47 docker: "host=unix:///var/run/docker.sock",
48 },
49 oldBindings: [],
50 newBindings: [],
51 output: {
52 id: "build-context",
53 name: "build-context",
54 description: "old description",
55 docker: "host=unix:///var/run/docker.sock",
56 },
57 });
58 expect(contextDiff).toEqual({ action: "update" });
59 }),
60 { tags: ["provider:docker", "provider:docker:context", "local"] },
61 );
62
63 test.provider(
64 "diff replaces a context when docker endpoint is removed",
65 () =>
66 Effect.gen(function* () {
67 const contextProvider = yield* Provider.findProvider(Docker.Context);
68 const contextDiff = yield* contextProvider.diff!({
69 id: "build-context",
70 fqn: "build-context",
71 instanceId: "instance",
72 olds: {
73 name: "build-context",
74 description: "dev context",
75 docker: "host=ssh://user@example.com",
76 },
77 news: { name: "build-context", description: "dev context" },
78 oldBindings: [],
79 newBindings: [],
80 output: {
81 id: "build-context",
82 name: "build-context",
83 description: "dev context",
84 docker: "host=ssh://user@example.com",
85 },
86 });
87 expect(contextDiff).toEqual({ action: "replace", deleteFirst: true });
88 }),
89 { tags: ["provider:docker", "provider:docker:context", "local"] },
90 );
91
92 describe(
93 "Docker.Context",
94 { tags: ["provider:docker", "provider:docker:context", "local"], concurrent: false },
95 () => {
96 // Every prop is optional: `Docker.Context("ctx")` must plan, deploy,
97 // redeploy in place, and destroy without a props object.
98 test.provider("deploys a context declared without props", (stack) =>
99 Effect.gen(function* () {
100 const docker = yield* Docker.Docker;
101 yield* stack.destroy();
102 const first = yield* stack.deploy(Docker.Context("bare-context"));
103 const second = yield* stack.deploy(Docker.Context("bare-context"));
104 expect(second.id).toBe(first.id);
105 yield* stack.destroy();
106 const gone = yield* docker.context.inspect(first.name).pipe(Effect.flip);
107 expect(gone.reason._tag).toBe("NotFound");
108 }),
109 );
110
111 test.provider("creates a context with description and endpoint", (stack) =>
112 Effect.gen(function* () {
113 const docker = yield* Docker.Docker;
114 const contextName = "alchemy-test-context-create";
115
116 yield* Effect.addFinalizer(() =>
117 docker.context.remove(contextName, true).pipe(Effect.ignore),
118 );
119
120 const context = yield* stack.deploy(
121 Docker.Context("created-context", {
122 name: contextName,
123 description: "created by alchemy tests",
124 docker: "host=unix:///var/run/docker.sock",
125 }),
126 );
127
128 expect(context.name).toBe(contextName);
129 expect(context.id).toBe(contextName);
130 expect(context.description).toBe("created by alchemy tests");
131 expect(extractDockerHost(context.docker)).toBe("unix:///var/run/docker.sock");
132 }),
133 );
134
135 test.provider("updates an existing context without replacing it", (stack) =>
136 Effect.gen(function* () {
137 const contextName = "alchemy-test-context-update";
138
139 const first = yield* stack.deploy(
140 Docker.Context("updatable-context", {
141 name: contextName,
142 description: "v1",
143 docker: "host=unix:///var/run/docker.sock",
144 }),
145 );
146
147 const second = yield* stack.deploy(
148 Docker.Context("updatable-context", {
149 name: contextName,
150 description: "v2",
151 docker: "host=unix:///var/run/docker.sock",
152 }),
153 );
154
155 expect(second.id).toBe(first.id);
156 expect(second.name).toBe(first.name);
157 expect(second.description).toBe("v2");
158 expect(extractDockerHost(second.docker)).toBe("unix:///var/run/docker.sock");
159 }),
160 );
161
162 test.provider("re-creates a context removed from the local docker", (stack) =>
163 Effect.gen(function* () {
164 const docker = yield* Docker.Docker;
165 const context = Docker.Context("machine-local-context", {
166 description: "lives on one machine",
167 docker: "host=unix:///var/run/docker.sock",
168 });
169
170 yield* stack.destroy();
171 const created = yield* stack.deploy(context);
172 yield* docker.context.remove(created.name, true);
173
174 const plan = yield* stack.plan(context);
175 expect(plan.resources["machine-local-context"]).toMatchObject({ action: "update" });
176
177 yield* stack.deploy(context);
178 const live = yield* docker.context.inspect(created.name);
179 expect(live.Name).toBe(created.name);
180 expect(live.Metadata?.Description).toBe("lives on one machine");
181
182 yield* stack.destroy();
183 }),
184 );
185
186 test.provider("plans a replace when docker endpoint is cleared", (stack) =>
187 Effect.gen(function* () {
188 const base = Docker.Context("planned-context", {
189 description: "with endpoint",
190 docker: "host=ssh://user@example.com",
191 });
192
193 const changed = Docker.Context("planned-context", { description: "with endpoint" });
194
195 yield* stack.deploy(base);
196 const plan = yield* stack.plan(changed);
197 expect(plan.resources["planned-context"]).toMatchObject({ action: "replace" });
198 }),
199 );
200 },
201 );
202
203 const extractDockerHost = (docker: unknown): string | undefined => {
204 if (typeof docker === "string") {
205 return docker.startsWith("host=") ? docker.slice("host=".length) : docker;
206 }
207 if (typeof docker === "object" && docker !== null && "Host" in docker) {
208 const host = (docker as { Host?: unknown }).Host;
209 return typeof host === "string" ? host : undefined;
210 }
211 return undefined;
212 };