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

main
ts 184 lines 6.14 KB
Raw
1 import { generateKeyPairSync } from "node:crypto";
2 import * as Hetzner from "@alchemy.run/hetzner";
3 import { expect } from "@alchemy.run/test";
4 import * as Test from "@alchemy.run/test/Alchemy";
5 import * as sshKeys from "@distilled.cloud/hetzner/ssh_keys";
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
11 const { test } = Test.make({ providers: Hetzner.providers() });
12
13 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
14
15 const hasHetznerCreds = !!process.env.HCLOUD_TOKEN;
16
17 const generatePublicKey = (comment: string) =>
18 Effect.sync(() => {
19 const { publicKey } = generateKeyPairSync("ed25519");
20 const der = publicKey.export({ type: "spki", format: "der" });
21 const raw = der.subarray(der.length - 32);
22 const type = Buffer.from("ssh-ed25519");
23 const u32 = (n: number) => {
24 const buf = Buffer.alloc(4);
25 buf.writeUInt32BE(n);
26 return buf;
27 };
28 const payload = Buffer.concat([u32(type.length), type, u32(raw.length), raw]);
29 return `ssh-ed25519 ${payload.toString("base64")} ${comment}`;
30 });
31
32 const waitUntilGone = (id: number) =>
33 sshKeys.getSshKey({ id }).pipe(
34 Effect.as("found" as const),
35 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
36 Effect.repeat({
37 schedule: Schedule.spaced("1 second"),
38 until: (status) => status === "gone",
39 times: 10,
40 }),
41 );
42
43 test.provider.skipIf(!hasHetznerCreds)(
44 "create, update, and delete an ssh key",
45 (stack) =>
46 Effect.gen(function* () {
47 yield* stack.destroy();
48
49 const publicKey = yield* generatePublicKey("alchemy-sshkey-crud");
50
51 const created = yield* stack.deploy(
52 Effect.gen(function* () {
53 return yield* Hetzner.SshKey("DeployKey", {
54 publicKey,
55 labels: { env: "test" },
56 });
57 }),
58 );
59
60 expect(created.id).toEqual(expect.any(Number));
61 expect(created.name).toEqual(expect.any(String));
62 expect(created.fingerprint).toEqual(expect.any(String));
63 expect(created.publicKey).toContain("ssh-ed25519");
64 expect(created.created).toEqual(expect.any(String));
65 expect(created.labels).toMatchObject({ env: "test" });
66
67 const fetched = yield* sshKeys.getSshKey({ id: created.id });
68 expect(fetched.ssh_key.id).toEqual(created.id);
69 expect(fetched.ssh_key.name).toEqual(created.name);
70 expect(fetched.ssh_key.fingerprint).toEqual(created.fingerprint);
71 expect(fetched.ssh_key.labels.env).toEqual("test");
72
73 const updated = yield* stack.deploy(
74 Effect.gen(function* () {
75 return yield* Hetzner.SshKey("DeployKey", {
76 name: `${created.name.slice(0, 55)}-renamed`,
77 publicKey,
78 labels: { env: "prod", role: "deploy" },
79 });
80 }),
81 );
82
83 expect(updated.id).toEqual(created.id);
84 expect(updated.fingerprint).toEqual(created.fingerprint);
85 expect(updated.name).toEqual(`${created.name.slice(0, 55)}-renamed`);
86 expect(updated.labels).toMatchObject({ env: "prod", role: "deploy" });
87
88 const refetched = yield* sshKeys.getSshKey({ id: updated.id });
89 expect(refetched.ssh_key.name).toEqual(updated.name);
90 expect(refetched.ssh_key.labels.env).toEqual("prod");
91 expect(refetched.ssh_key.labels.role).toEqual("deploy");
92
93 yield* stack.destroy();
94
95 const gone = yield* waitUntilGone(created.id);
96 expect(gone).toEqual("gone");
97 }).pipe(logLevel),
98 {
99 tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:sshkey", "live"],
100 timeout: 120_000,
101 },
102 );
103
104 test.provider.skipIf(!hasHetznerCreds)(
105 "replace when public key changes",
106 (stack) =>
107 Effect.gen(function* () {
108 yield* stack.destroy();
109
110 const firstKey = yield* generatePublicKey("alchemy-sshkey-replace-a");
111 const secondKey = yield* generatePublicKey("alchemy-sshkey-replace-b");
112
113 const created = yield* stack.deploy(
114 Effect.gen(function* () {
115 return yield* Hetzner.SshKey("ReplaceKey", {
116 publicKey: firstKey,
117 });
118 }),
119 );
120
121 expect(created.publicKey).toContain("ssh-ed25519");
122
123 const replaced = yield* stack.deploy(
124 Effect.gen(function* () {
125 return yield* Hetzner.SshKey("ReplaceKey", {
126 name: created.name,
127 publicKey: secondKey,
128 });
129 }),
130 );
131
132 expect(replaced.id).not.toEqual(created.id);
133 expect(replaced.fingerprint).not.toEqual(created.fingerprint);
134 expect(replaced.name).toEqual(created.name);
135
136 const fetched = yield* sshKeys.getSshKey({ id: replaced.id });
137 expect(fetched.ssh_key.id).toEqual(replaced.id);
138 expect(fetched.ssh_key.fingerprint).toEqual(replaced.fingerprint);
139
140 const oldGone = yield* waitUntilGone(created.id);
141 expect(oldGone).toEqual("gone");
142
143 yield* stack.destroy();
144
145 const gone = yield* waitUntilGone(replaced.id);
146 expect(gone).toEqual("gone");
147 }).pipe(logLevel),
148 {
149 tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:sshkey", "live"],
150 timeout: 180_000,
151 },
152 );
153
154 test.provider.skipIf(!hasHetznerCreds)(
155 "list enumerates the deployed ssh key",
156 (stack) =>
157 Effect.gen(function* () {
158 yield* stack.destroy();
159
160 const publicKey = yield* generatePublicKey("alchemy-sshkey-list");
161
162 const deployed = yield* stack.deploy(
163 Effect.gen(function* () {
164 return yield* Hetzner.SshKey("ListKey", { publicKey });
165 }),
166 );
167
168 const provider = yield* Provider.findProvider(Hetzner.SshKey);
169 const all = yield* provider.list();
170 const found = all.find((key) => key.id === deployed.id);
171 expect(found).toBeDefined();
172 expect(found?.name).toEqual(deployed.name);
173 expect(found?.fingerprint).toEqual(deployed.fingerprint);
174
175 yield* stack.destroy();
176
177 const gone = yield* waitUntilGone(deployed.id);
178 expect(gone).toEqual("gone");
179 }).pipe(logLevel),
180 {
181 tags: ["provider:hetzner", "provider:hetzner:service", "provider:hetzner:sshkey", "live"],
182 timeout: 120_000,
183 },
184 );