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

main
ts 166 lines 5.1 KB
Raw
1 import {
2 parseCreatedAt,
3 parseRepoDigest,
4 publishedRepoDigest,
5 repositoryFromImageRef,
6 withRegistryHost,
7 } from "@alchemy.run/docker/Registry";
8 import { describe, expect, it } from "@alchemy.run/test";
9
10 const digest = `sha256:${"a".repeat(64)}`;
11 const other = `sha256:${"b".repeat(64)}`;
12
13 describe(
14 "repositoryFromImageRef",
15 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
16 () => {
17 it("strips a simple tag", () => {
18 expect(repositoryFromImageRef("nginx:alpine")).toBe("nginx");
19 });
20
21 it("keeps the registry host and path", () => {
22 expect(repositoryFromImageRef("ghcr.io/acme/app:latest")).toBe("ghcr.io/acme/app");
23 });
24
25 it("does not confuse a registry port for a tag", () => {
26 expect(repositoryFromImageRef("localhost:5000/acme/app:latest")).toBe(
27 "localhost:5000/acme/app",
28 );
29 });
30
31 it("strips a digest", () => {
32 expect(
33 repositoryFromImageRef(
34 "localhost:5000/acme/app@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
35 ),
36 ).toBe("localhost:5000/acme/app");
37 });
38
39 it("returns a bare repository unchanged", () => {
40 expect(repositoryFromImageRef("nginx")).toBe("nginx");
41 });
42 },
43 );
44
45 describe(
46 "withRegistryHost",
47 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
48 () => {
49 it("prefixes a bare reference with the registry host", () => {
50 expect(withRegistryHost("app:latest", { server: "ghcr.io" })).toBe("ghcr.io/app:latest");
51 });
52
53 it("trims a trailing slash from the server", () => {
54 expect(withRegistryHost("app:latest", { server: "ghcr.io/" })).toBe("ghcr.io/app:latest");
55 });
56
57 it("leaves a reference that already has a dotted-host prefix", () => {
58 expect(withRegistryHost("registry.example.com/app:latest", { server: "ghcr.io" })).toBe(
59 "registry.example.com/app:latest",
60 );
61 });
62
63 it("leaves a localhost:port reference untouched", () => {
64 expect(withRegistryHost("localhost:5000/app:latest", { server: "ghcr.io" })).toBe(
65 "localhost:5000/app:latest",
66 );
67 });
68 },
69 );
70
71 describe(
72 "parseRepoDigest",
73 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
74 () => {
75 it("extracts the repo digest from push output", () => {
76 expect(
77 parseRepoDigest(
78 "localhost:5000/app:latest",
79 "latest: digest: sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa size: 123",
80 ),
81 ).toBe(
82 "localhost:5000/app@sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
83 );
84 });
85
86 it("returns undefined when no digest is present", () => {
87 expect(parseRepoDigest("app:latest", "Pushed without a digest")).toBe(undefined);
88 });
89 },
90 );
91
92 describe(
93 "publishedRepoDigest",
94 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
95 () => {
96 it("reads RepoDigests when push output has no digest line", () => {
97 expect(
98 publishedRepoDigest(
99 "localhost:5055/app:latest",
100 {
101 stdout: "",
102 stderr:
103 "Getting image source signatures\nCopying blob sha256:74d97c42\nWriting manifest to image destination\n",
104 },
105 [`localhost:5055/app@${digest}`, `ghcr.io/acme/app@${other}`],
106 ),
107 ).toBe(`localhost:5055/app@${digest}`);
108 });
109
110 it("prefers a digest line from push output", () => {
111 expect(
112 publishedRepoDigest(
113 "localhost:5055/app:latest",
114 { stdout: `latest: digest: ${digest}`, stderr: "" },
115 [`localhost:5055/app@${other}`],
116 ),
117 ).toBe(`localhost:5055/app@${digest}`);
118 });
119
120 it("uses the registry-qualified RepoDigests entry", () => {
121 expect(
122 publishedRepoDigest(
123 "app:latest",
124 { stdout: "", stderr: "" },
125 [`ghcr.io/acme/app@${digest}`],
126 "ghcr.io/acme/app:latest",
127 ),
128 ).toBe(`ghcr.io/acme/app@${digest}`);
129 });
130
131 it("returns undefined when neither output nor RepoDigests match", () => {
132 expect(
133 publishedRepoDigest("app:latest", { stdout: "", stderr: "Writing manifest\n" }, [
134 `ghcr.io/acme/other@${digest}`,
135 ]),
136 ).toBe(undefined);
137 });
138 },
139 );
140
141 describe(
142 "parseCreatedAt",
143 { tags: ["unit", "provider:docker", "provider:docker:registry", "local"] },
144 () => {
145 it("parses an RFC 3339 timestamp", () => {
146 const created = "2026-06-22T20:53:00.395Z";
147 expect(parseCreatedAt(created)).toBe(Date.parse(created));
148 });
149
150 it("falls back to the wall clock when omitted", () => {
151 const before = Date.now();
152 const result = parseCreatedAt(undefined);
153 expect(result).toBeGreaterThanOrEqual(before);
154 });
155
156 it("falls back to the wall clock for an empty string", () => {
157 const before = Date.now();
158 expect(parseCreatedAt("")).toBeGreaterThanOrEqual(before);
159 });
160
161 it("falls back to the wall clock for the year-1 zero value", () => {
162 const before = Date.now();
163 expect(parseCreatedAt("0001-01-01T00:00:00Z")).toBeGreaterThanOrEqual(before);
164 });
165 },
166 );