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

main
ts 115 lines 4.64 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 { inMemoryState } from "alchemy/State";
5 import * as Effect from "effect/Effect";
6 import * as FileSystem from "effect/FileSystem";
7 import * as HttpClient from "effect/http/HttpClient";
8 import * as HttpClientRequest from "effect/http/HttpClientRequest";
9 import * as Path from "effect/Path";
10 import { authenticatedRegistry } from "./Runtime.ts";
11
12 /**
13 * The Docker resources against a real Podman, declared in code with
14 * `Docker.providers({ bin })`. Set `ALCHEMY_TEST_PODMAN_BIN` to a `podman`
15 * executable to run them; Podman must treat `localhost` registries as
16 * insecure (plain HTTP).
17 */
18 const podman = process.env.ALCHEMY_TEST_PODMAN_BIN;
19
20 const { test } = Test.make({
21 providers: Docker.providers({ bin: podman ?? "podman" }),
22 state: inMemoryState(),
23 });
24
25 // The registry runs on the host's Docker: its published port is reachable
26 // from the test and from Podman alike.
27 const hostRegistry = () => authenticatedRegistry().pipe(Effect.provide(Docker.DockerLive));
28
29 /** The digest the registry serves for `repository:tag`. */
30 const registryDigest = (host: string, repository: string, tag: string) =>
31 Effect.gen(function* () {
32 const client = yield* HttpClient.HttpClient;
33 const basic = Buffer.from("alchemy:alchemy-test-password").toString("base64");
34 const response = yield* client.execute(
35 HttpClientRequest.head(`http://${host}/v2/${repository}/manifests/${tag}`).pipe(
36 HttpClientRequest.setHeaders({
37 authorization: `Basic ${basic}`,
38 accept: [
39 "application/vnd.oci.image.index.v1+json",
40 "application/vnd.oci.image.manifest.v1+json",
41 "application/vnd.docker.distribution.manifest.list.v2+json",
42 "application/vnd.docker.distribution.manifest.v2+json",
43 ].join(", "),
44 }),
45 ),
46 );
47 return response.headers["docker-content-digest"];
48 });
49
50 describe.skipIf(!podman)(
51 "Docker resources on Podman",
52 { tags: ["provider:docker", "local"], concurrent: false },
53 () => {
54 // Podman reports a missing image as "image not known"; the first plan
55 // reads the image before it exists.
56 test.provider("builds an image whose first read finds nothing", (stack) =>
57 Effect.gen(function* () {
58 const fs = yield* FileSystem.FileSystem;
59 const path = yield* Path.Path;
60 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-podman-build-" });
61 yield* fs.writeFileString(path.join(root, "Dockerfile"), "FROM scratch\nLABEL podman=1\n");
62
63 const image = yield* stack.deploy(
64 Docker.Image("podman-local-image", { tag: "v1", build: { context: root } }),
65 );
66 expect(image.imageId.length).toBeGreaterThan(0);
67 yield* stack.destroy();
68 }),
69 );
70
71 // `podman push` prints no `digest:` line, so the digest comes from the
72 // image's RepoDigests after the push.
73 test.provider("reports the pushed digest of a built image", (stack) =>
74 Effect.gen(function* () {
75 const fs = yield* FileSystem.FileSystem;
76 const path = yield* Path.Path;
77 const registry = yield* hostRegistry();
78 const root = yield* fs.makeTempDirectoryScoped({ prefix: "alchemy-podman-push-" });
79 yield* fs.writeFileString(path.join(root, "Dockerfile"), "FROM scratch\nLABEL podman=2\n");
80
81 const image = yield* stack.deploy(
82 Docker.Image("podman-pushed-image", {
83 name: `${registry.host}/podman-app`,
84 tag: "v1",
85 registry: registry.credentials,
86 build: { context: root },
87 }),
88 );
89 const digest = yield* registryDigest(registry.host, "podman-app", "v1");
90 expect(digest).toMatch(/^sha256:/);
91 expect(image.repoDigest).toBe(`${registry.host}/podman-app@${digest}`);
92 yield* stack.destroy();
93 }),
94 );
95
96 test.provider("reports the pushed digest of a re-tagged remote image", (stack) =>
97 Effect.gen(function* () {
98 const registry = yield* hostRegistry();
99 const image = yield* stack.deploy(
100 Docker.RemoteImage("podman-remote-image", {
101 name: "docker.io/library/busybox",
102 tag: "1.36",
103 targetName: `${registry.host}/podman-busybox`,
104 targetTag: "v1",
105 registry: registry.credentials,
106 }),
107 );
108 const digest = yield* registryDigest(registry.host, "podman-busybox", "v1");
109 expect(digest).toMatch(/^sha256:/);
110 expect(image.repoDigest).toBe(`${registry.host}/podman-busybox@${digest}`);
111 yield* stack.destroy();
112 }),
113 );
114 },
115 );