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

main
ts 132 lines 4.33 KB
Raw
1 import * as GCP from "@alchemy.run/gcp";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as oslogin from "@distilled.cloud/gcp/oslogin_v1";
5 import * as Effect from "effect/Effect";
6 import { MinimumLogLevel } from "effect/References";
7 import * as Schedule from "effect/Schedule";
8
9 const { test } = Test.make({ providers: GCP.providers() });
10
11 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
12
13 const KEY1 = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIN6Ot81wrURgF58/jKCFQgEzJFjD39ibwfpeC7JLoS6d";
14 const KEY2 = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIBpVICT7tXAjpo6pXw/44Wm+DYcQRexT7J8nwS9/XtnL";
15
16 const EXPIRY_A = "4102444800000000";
17 const EXPIRY_B = "4133980800000000";
18
19 // OS Login keys belong to the authenticated account. A service account
20 // must name itself (`users/me` fails with UserCredentialMismatch), so set
21 // GCP_TEST_OSLOGIN_USER to the test credentials' email.
22 const user = process.env.GCP_TEST_OSLOGIN_USER?.trim();
23
24 const waitUntilGone = (name: string) =>
25 oslogin.getUsersSshPublicKeys({ name }).pipe(
26 Effect.as("found" as const),
27 Effect.catchTag("NotFound", () => Effect.succeed("gone" as const)),
28 Effect.repeat({
29 schedule: Schedule.spaced("1 second"),
30 until: (status) => status === "gone",
31 times: 10,
32 }),
33 );
34
35 test.provider(
36 "users/me with service-account credentials fails with UserCredentialMismatch",
37 (stack) =>
38 Effect.gen(function* () {
39 yield* stack.destroy();
40
41 const error = yield* Effect.flip(
42 oslogin.getUsersSshPublicKeys({
43 name: "users/me/sshPublicKeys/alchemy-missing-fingerprint",
44 }),
45 );
46 expect(error._tag).toEqual("UserCredentialMismatch");
47
48 yield* stack.destroy();
49 }).pipe(logLevel),
50 { tags: ["provider:gcp", "provider:gcp:oslogin", "live"], timeout: 90_000 },
51 );
52
53 test.provider.skipIf(!user || !!process.env.FAST)(
54 "create, update, and delete an SSH public key",
55 (stack) =>
56 Effect.gen(function* () {
57 yield* stack.destroy();
58
59 const created = yield* stack.deploy(
60 Effect.gen(function* () {
61 return yield* GCP.OSLogin.UsersSshPublicKey("Laptop", {
62 user,
63 key: KEY1,
64 expirationTimeUsec: EXPIRY_A,
65 });
66 }),
67 );
68
69 expect(created.name.length).toBeGreaterThan(0);
70 expect(created.fingerprint.length).toBeGreaterThan(0);
71 expect(created.user).toEqual(user);
72 expect(created.key).toEqual(KEY1);
73 expect(created.expirationTimeUsec).toEqual(EXPIRY_A);
74
75 const fetched = yield* oslogin.getUsersSshPublicKeys({
76 name: created.name,
77 });
78 expect(fetched.name).toEqual(created.name);
79 expect(fetched.fingerprint).toEqual(created.fingerprint);
80 expect(fetched.key).toEqual(KEY1);
81 expect(fetched.expirationTimeUsec).toEqual(EXPIRY_A);
82
83 const updated = yield* stack.deploy(
84 Effect.gen(function* () {
85 return yield* GCP.OSLogin.UsersSshPublicKey("Laptop", {
86 user,
87 key: KEY1,
88 expirationTimeUsec: EXPIRY_B,
89 });
90 }),
91 );
92
93 expect(updated.name).toEqual(created.name);
94 expect(updated.fingerprint).toEqual(created.fingerprint);
95 expect(updated.expirationTimeUsec).toEqual(EXPIRY_B);
96
97 const fetchedUpdate = yield* oslogin.getUsersSshPublicKeys({
98 name: updated.name,
99 });
100 expect(fetchedUpdate.expirationTimeUsec).toEqual(EXPIRY_B);
101 expect(fetchedUpdate.key).toEqual(KEY1);
102
103 const replaced = yield* stack.deploy(
104 Effect.gen(function* () {
105 return yield* GCP.OSLogin.UsersSshPublicKey("Laptop", {
106 user,
107 key: KEY2,
108 expirationTimeUsec: EXPIRY_B,
109 });
110 }),
111 );
112
113 expect(replaced.fingerprint).not.toEqual(created.fingerprint);
114 expect(replaced.key).toEqual(KEY2);
115
116 const fetchedReplace = yield* oslogin.getUsersSshPublicKeys({
117 name: replaced.name,
118 });
119 expect(fetchedReplace.fingerprint).toEqual(replaced.fingerprint);
120 expect(fetchedReplace.key).toEqual(KEY2);
121
122 yield* stack.destroy();
123
124 const gone = yield* waitUntilGone(replaced.name);
125 expect(gone).toEqual("gone");
126 }).pipe(logLevel),
127 {
128 tags: ["provider:gcp", "provider:gcp:oslogin", "live"],
129 timeout: 90_000,
130 exclusive: "gcp:oslogin",
131 },
132 );