The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as NodeCrypto from "node:crypto"; |
| 2 | import { describe, expect } from "@alchemy.run/test"; |
| 3 | import * as Test from "@alchemy.run/test/Alchemy"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Redacted from "effect/Redacted"; |
| 6 | import { KeyPair, KeyPairProvider } from "@/KeyPair"; |
| 7 | import * as Provider from "@/Provider"; |
| 8 | import { inMemoryState } from "@/State"; |
| 9 | |
| 10 | const { test } = Test.make({ |
| 11 | providers: KeyPairProvider(), |
| 12 | state: inMemoryState(), |
| 13 | }); |
| 14 | |
| 15 | const assertPemKeyPair = (attrs: { privateKey: Redacted.Redacted<string>; publicKey: string }) => { |
| 16 | const priv = Redacted.value(attrs.privateKey); |
| 17 | expect(priv).toMatch(/^-----BEGIN PRIVATE KEY-----/); |
| 18 | expect(priv).toMatch(/-----END PRIVATE KEY-----/); |
| 19 | expect(attrs.publicKey).toMatch(/^-----BEGIN PUBLIC KEY-----/); |
| 20 | expect(attrs.publicKey).toMatch(/-----END PUBLIC KEY-----/); |
| 21 | // Node throws if the PEM is malformed. |
| 22 | NodeCrypto.createPrivateKey(priv); |
| 23 | NodeCrypto.createPublicKey(attrs.publicKey); |
| 24 | }; |
| 25 | |
| 26 | describe("Alchemy.KeyPair", { tags: ["unit", "local"] }, () => { |
| 27 | test.provider("mints an ed25519 keypair by default", (stack) => |
| 28 | Effect.gen(function* () { |
| 29 | const attrs = yield* stack.deploy( |
| 30 | Effect.gen(function* () { |
| 31 | return yield* KeyPair("ed25519-default"); |
| 32 | }), |
| 33 | ); |
| 34 | expect(attrs.algorithm).toBe("ed25519"); |
| 35 | assertPemKeyPair(attrs); |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | test.provider("mints an rsa keypair when requested", (stack) => |
| 40 | Effect.gen(function* () { |
| 41 | const attrs = yield* stack.deploy( |
| 42 | Effect.gen(function* () { |
| 43 | return yield* KeyPair("rsa-key", { |
| 44 | algorithm: "rsa", |
| 45 | modulusLength: 2048, |
| 46 | }); |
| 47 | }), |
| 48 | ); |
| 49 | expect(attrs.algorithm).toBe("rsa"); |
| 50 | assertPemKeyPair(attrs); |
| 51 | }), |
| 52 | ); |
| 53 | |
| 54 | test.provider("mints an ec keypair when requested", (stack) => |
| 55 | Effect.gen(function* () { |
| 56 | const attrs = yield* stack.deploy( |
| 57 | Effect.gen(function* () { |
| 58 | return yield* KeyPair("ec-key", { |
| 59 | algorithm: "ec", |
| 60 | namedCurve: "P-256", |
| 61 | }); |
| 62 | }), |
| 63 | ); |
| 64 | expect(attrs.algorithm).toBe("ec"); |
| 65 | assertPemKeyPair(attrs); |
| 66 | }), |
| 67 | ); |
| 68 | |
| 69 | test.provider("preserves the keypair across deploys", (stack) => |
| 70 | Effect.gen(function* () { |
| 71 | const program = Effect.gen(function* () { |
| 72 | return yield* KeyPair("stable-key"); |
| 73 | }); |
| 74 | |
| 75 | const first = yield* stack.deploy(program); |
| 76 | const second = yield* stack.deploy(program); |
| 77 | |
| 78 | expect(Redacted.value(second.privateKey)).toBe(Redacted.value(first.privateKey)); |
| 79 | expect(second.publicKey).toBe(first.publicKey); |
| 80 | }), |
| 81 | ); |
| 82 | |
| 83 | test.provider("list returns [] for the non-listable keypair", (stack) => |
| 84 | Effect.gen(function* () { |
| 85 | yield* stack.destroy(); |
| 86 | |
| 87 | yield* stack.deploy( |
| 88 | Effect.gen(function* () { |
| 89 | return yield* KeyPair("list-key"); |
| 90 | }), |
| 91 | ); |
| 92 | |
| 93 | const provider = yield* Provider.findProvider(KeyPair); |
| 94 | expect(yield* provider.list()).toEqual([]); |
| 95 | |
| 96 | yield* stack.destroy(); |
| 97 | }), |
| 98 | ); |
| 99 | }); |