@devries-cameron20 / t3code Mirror · read-only
Fork 0

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

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 06:00 UTC

main
ts 128 lines 4.43 KB
Raw
1 import * as GitHub from "@alchemy.run/github";
2 import { expect } from "@alchemy.run/test";
3 import * as Test from "@alchemy.run/test/Alchemy";
4 import * as Actions from "@distilled.cloud/github/actions";
5 import * as Provider from "alchemy/Provider";
6 import { destroy } from "alchemy/RemovalPolicy";
7 import * as Config from "effect/Config";
8 import * as Effect from "effect/Effect";
9 import * as Redacted from "effect/Redacted";
10 import { MinimumLogLevel } from "effect/References";
11 import { gh } from "./scope.ts";
12
13 const { test } = Test.make({ providers: GitHub.providers() });
14
15 const logLevel = Effect.provideService(MinimumLogLevel, process.env.DEBUG ? "Debug" : "Info");
16
17 // Deploying a secret needs an owner + repository the token can write to —
18 // the dedicated test org (never a real one). Set GITHUB_TEST_OWNER="" to
19 // skip.
20 const owner = process.env.GITHUB_TEST_OWNER ?? "alchemy-run-test";
21 const repository = process.env.GITHUB_TEST_REPOSITORY ?? "test-repo";
22
23 // GitHub never returns a secret's value, only its metadata — so `getRepoSecret`
24 // succeeding (vs. 404) is how we assert presence/absence out-of-band.
25 const secretExists = (name: string) =>
26 Effect.gen(function* () {
27 return yield* gh(
28 Actions.getRepoSecret({
29 owner,
30 repo: repository,
31 secret_name: name,
32 }),
33 ).pipe(
34 Effect.as(true),
35 Effect.catchTag("NotFound", () => Effect.succeed(false)),
36 );
37 });
38
39 test.provider.skipIf(!owner)(
40 "Secrets resolves Config values before wrapping them as Redacted",
41 (stack) =>
42 Effect.gen(function* () {
43 yield* stack.destroy();
44
45 const [secret] = yield* stack.deploy(
46 Effect.gen(function* () {
47 // `Repository` defaults to `retain`: ensure the host repo exists
48 // (another suite may have deleted/recreated it mid-run) without
49 // ever deleting it on destroy. Reconcile is idempotent.
50 yield* GitHub.Repository("Repo", {
51 owner,
52 name: repository,
53 description: "alchemy-effect secret test host",
54 visibility: "private",
55 autoInit: true,
56 });
57
58 return yield* GitHub.Secrets({
59 owner,
60 repository,
61 secrets: {
62 ALCHEMY_CONFIG_SECRET: Config.succeed("hunter2"),
63 },
64 });
65 }),
66 );
67
68 // A Config value resolves to "hunter2", gets wrapped as Redacted, and is
69 // encrypted + uploaded successfully — proven by a fresh `updatedAt`.
70 expect(secret.updatedAt).toEqual(expect.any(String));
71
72 // The secret really lives in GitHub now.
73 expect(yield* secretExists("ALCHEMY_CONFIG_SECRET")).toBe(true);
74
75 yield* stack.destroy();
76
77 // ...and is gone after destroy.
78 expect(yield* secretExists("ALCHEMY_CONFIG_SECRET")).toBe(false);
79 }).pipe(logLevel),
80 {
81 tags: ["provider:github", "provider:github:repository", "provider:github:secret", "live"],
82 timeout: 120_000,
83 },
84 );
85
86 // `list()` for GitHub.Secret is non-listable (pattern (e) in
87 // processes/list-support.md): secrets are keyed by their parent
88 // (owner/repository[, environment]/name) which arrive as props, there is no
89 // ambient owner/repo scope, and GitHub exposes no account-wide enumeration —
90 // only list-secrets *within* a specific repo. So `list()` always returns `[]`,
91 // even when a secret exists in the cloud.
92 test.provider.skipIf(!owner)(
93 "list returns an empty array for non-enumerable GitHub secrets",
94 (stack) =>
95 Effect.gen(function* () {
96 yield* stack.destroy();
97
98 // Prove `list()` still returns `[]` even with a real secret deployed
99 // (it is not enumerable without scope).
100 yield* stack.deploy(
101 Effect.gen(function* () {
102 return yield* GitHub.Secret("ListSecret", {
103 owner,
104 repository,
105 name: "ALCHEMY_LIST_TEST",
106 value: Redacted.make("list-test-value"),
107 }).pipe(destroy());
108 }),
109 );
110
111 // The secret is really in GitHub even though `list()` can't see it.
112 expect(yield* secretExists("ALCHEMY_LIST_TEST")).toBe(true);
113
114 const provider = yield* Provider.findProvider(GitHub.Secret);
115 const all = yield* provider.list();
116
117 expect(all).toEqual([]);
118
119 yield* stack.destroy();
120
121 // ...and is gone after destroy.
122 expect(yield* secretExists("ALCHEMY_LIST_TEST")).toBe(false);
123 }).pipe(logLevel),
124 {
125 tags: ["provider:github", "provider:github:secret", "live"],
126 timeout: 120_000,
127 },
128 );