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
| 1 | import { |
| 2 | clearStoreCredentials, |
| 3 | CREDENTIALS_FILE, |
| 4 | deleteStoreCredentials, |
| 5 | LEGACY_CREDENTIALS_FILE, |
| 6 | isStateStoreCredentialsStale, |
| 7 | readStoreCredentials, |
| 8 | writeStoreCredentials, |
| 9 | } from "@alchemy.run/cloudflare/StateStore/CredentialsFile"; |
| 10 | import { describe, expect, it } from "@alchemy.run/test"; |
| 11 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 12 | import type { CredentialsStoreService } from "alchemy/Auth/Credentials"; |
| 13 | import { profileStateStoresDirPath } from "alchemy/Auth/Paths"; |
| 14 | import * as Effect from "effect/Effect"; |
| 15 | import * as Schema from "effect/Schema"; |
| 16 | |
| 17 | const ACCOUNT_A = "c8eeff0e4f5ebeeedc8d9af2013d7997"; |
| 18 | const ACCOUNT_B = "a1b2c3d4e5f60718293a4b5c6d7e8f90"; |
| 19 | |
| 20 | /** |
| 21 | * The cached Cloudflare state-store credentials encode an account (the `url` |
| 22 | * is a `*.workers.dev` subdomain). {@link isStateStoreCredentialsStale} guards |
| 23 | * against trusting a cache minted for a different account than the one now in |
| 24 | * use — the bug where deploys silently read/write state in a previously |
| 25 | * logged-in account. |
| 26 | */ |
| 27 | describe( |
| 28 | "isStateStoreCredentialsStale", |
| 29 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:statestore", "local"] }, |
| 30 | () => { |
| 31 | it("is fresh when the cached account matches the current account", () => { |
| 32 | expect( |
| 33 | isStateStoreCredentialsStale( |
| 34 | { url: "https://s.workers.dev", authToken: "t", accountId: ACCOUNT_A }, |
| 35 | ACCOUNT_A, |
| 36 | ), |
| 37 | ).toBe(false); |
| 38 | }); |
| 39 | |
| 40 | it("is stale when the cached account differs from the current account", () => { |
| 41 | expect( |
| 42 | isStateStoreCredentialsStale( |
| 43 | { url: "https://s.workers.dev", authToken: "t", accountId: ACCOUNT_A }, |
| 44 | ACCOUNT_B, |
| 45 | ), |
| 46 | ).toBe(true); |
| 47 | }); |
| 48 | |
| 49 | it("is stale for a legacy cache with no accountId", () => { |
| 50 | expect( |
| 51 | isStateStoreCredentialsStale({ url: "https://s.workers.dev", authToken: "t" }, ACCOUNT_A), |
| 52 | ).toBe(true); |
| 53 | }); |
| 54 | }, |
| 55 | ); |
| 56 | |
| 57 | /** |
| 58 | * A credentials store in memory, encoding through each schema like the disk |
| 59 | * store. Files are keyed by directory, so a scoped store is its own folder. |
| 60 | */ |
| 61 | const memoryStore = () => { |
| 62 | const files = new Map<string, unknown>(); |
| 63 | const make = (dirFor: (profile: string) => string): CredentialsStoreService => { |
| 64 | const key = (profile: string, provider: string) => `${dirFor(profile)}/${provider}`; |
| 65 | return { |
| 66 | // Yields after reading, so concurrent read-modify-writes interleave as |
| 67 | // they can on disk. |
| 68 | read: (profile, provider, schema) => |
| 69 | Effect.suspend(() => { |
| 70 | const encoded = files.get(key(profile, provider)); |
| 71 | return encoded === undefined |
| 72 | ? Effect.succeed(undefined) |
| 73 | : Schema.decodeUnknownEffect(schema)(encoded).pipe(Effect.orDie); |
| 74 | }).pipe(Effect.tap(() => Effect.yieldNow)), |
| 75 | write: (profile, provider, schema, credentials) => |
| 76 | Schema.encodeEffect(schema)(credentials).pipe( |
| 77 | Effect.orDie, |
| 78 | Effect.map((encoded) => void files.set(key(profile, provider), encoded)), |
| 79 | ), |
| 80 | delete: (profile, provider) => Effect.sync(() => void files.delete(key(profile, provider))), |
| 81 | deleteProfile: () => Effect.void, |
| 82 | scoped: make, |
| 83 | }; |
| 84 | }; |
| 85 | return { store: make((profile) => `credentials/${profile}`), files }; |
| 86 | }; |
| 87 | |
| 88 | const LEGACY = `credentials/default/${LEGACY_CREDENTIALS_FILE}`; |
| 89 | const CURRENT = `${profileStateStoresDirPath("default")}/${CREDENTIALS_FILE}`; |
| 90 | |
| 91 | const credentials = (url: string) => ({ url, authToken: "t", accountId: ACCOUNT_A }); |
| 92 | |
| 93 | describe( |
| 94 | "state-store credentials by store name", |
| 95 | { tags: ["unit", "provider:cloudflare", "provider:cloudflare:statestore", "local"] }, |
| 96 | () => { |
| 97 | it.effect("copies a legacy single-store file into the new file and leaves it in place", () => |
| 98 | Effect.gen(function* () { |
| 99 | const { store, files } = memoryStore(); |
| 100 | files.set(LEGACY, credentials("https://a.workers.dev")); |
| 101 | expect(yield* readStoreCredentials(store, "default", "alchemy-state-store")).toEqual( |
| 102 | credentials("https://a.workers.dev"), |
| 103 | ); |
| 104 | expect(yield* readStoreCredentials(store, "default", "team-a")).toBeUndefined(); |
| 105 | expect(files.get(CURRENT)).toEqual({ |
| 106 | stores: { "alchemy-state-store": credentials("https://a.workers.dev") }, |
| 107 | }); |
| 108 | expect(files.get(LEGACY)).toEqual(credentials("https://a.workers.dev")); |
| 109 | }).pipe(Effect.provide(NodeServices.layer)), |
| 110 | ); |
| 111 | |
| 112 | it.effect("ignores the legacy file once the new file exists", () => |
| 113 | Effect.gen(function* () { |
| 114 | const { store, files } = memoryStore(); |
| 115 | files.set(CURRENT, { stores: {} }); |
| 116 | files.set(LEGACY, credentials("https://a.workers.dev")); |
| 117 | expect( |
| 118 | yield* readStoreCredentials(store, "default", "alchemy-state-store"), |
| 119 | ).toBeUndefined(); |
| 120 | }).pipe(Effect.provide(NodeServices.layer)), |
| 121 | ); |
| 122 | |
| 123 | it.effect("keeps every store beside the others in one file", () => |
| 124 | Effect.gen(function* () { |
| 125 | const { store, files } = memoryStore(); |
| 126 | files.set(LEGACY, credentials("https://a.workers.dev")); |
| 127 | yield* writeStoreCredentials( |
| 128 | store, |
| 129 | "default", |
| 130 | "team-a", |
| 131 | credentials("https://b.workers.dev"), |
| 132 | ); |
| 133 | expect(yield* readStoreCredentials(store, "default", "alchemy-state-store")).toEqual( |
| 134 | credentials("https://a.workers.dev"), |
| 135 | ); |
| 136 | expect(yield* readStoreCredentials(store, "default", "team-a")).toEqual( |
| 137 | credentials("https://b.workers.dev"), |
| 138 | ); |
| 139 | expect([...files.keys()].sort()).toEqual([LEGACY, CURRENT].sort()); |
| 140 | }).pipe(Effect.provide(NodeServices.layer)), |
| 141 | ); |
| 142 | |
| 143 | it.effect("deleting one store keeps the others", () => |
| 144 | Effect.gen(function* () { |
| 145 | const { store } = memoryStore(); |
| 146 | yield* writeStoreCredentials( |
| 147 | store, |
| 148 | "default", |
| 149 | "team-a", |
| 150 | credentials("https://a.workers.dev"), |
| 151 | ); |
| 152 | yield* writeStoreCredentials( |
| 153 | store, |
| 154 | "default", |
| 155 | "team-b", |
| 156 | credentials("https://b.workers.dev"), |
| 157 | ); |
| 158 | yield* deleteStoreCredentials(store, "default", "team-a"); |
| 159 | expect(yield* readStoreCredentials(store, "default", "team-a")).toBeUndefined(); |
| 160 | expect(yield* readStoreCredentials(store, "default", "team-b")).toEqual( |
| 161 | credentials("https://b.workers.dev"), |
| 162 | ); |
| 163 | }).pipe(Effect.provide(NodeServices.layer)), |
| 164 | ); |
| 165 | |
| 166 | it.effect("clearing, as profile edit and logout do, drops every store for good", () => |
| 167 | Effect.gen(function* () { |
| 168 | const { store, files } = memoryStore(); |
| 169 | files.set(LEGACY, credentials("https://a.workers.dev")); |
| 170 | yield* writeStoreCredentials( |
| 171 | store, |
| 172 | "default", |
| 173 | "team-a", |
| 174 | credentials("https://b.workers.dev"), |
| 175 | ); |
| 176 | yield* clearStoreCredentials(store, "default"); |
| 177 | expect( |
| 178 | yield* readStoreCredentials(store, "default", "alchemy-state-store"), |
| 179 | ).toBeUndefined(); |
| 180 | expect(yield* readStoreCredentials(store, "default", "team-a")).toBeUndefined(); |
| 181 | expect(files.get(LEGACY)).toEqual(credentials("https://a.workers.dev")); |
| 182 | }).pipe(Effect.provide(NodeServices.layer)), |
| 183 | ); |
| 184 | |
| 185 | it.effect("concurrent writes for different stores keep both", () => |
| 186 | Effect.gen(function* () { |
| 187 | const { store } = memoryStore(); |
| 188 | yield* Effect.all( |
| 189 | ["team-a", "team-b", "team-c"].map((name) => |
| 190 | writeStoreCredentials( |
| 191 | store, |
| 192 | "default", |
| 193 | name, |
| 194 | credentials(`https://${name}.workers.dev`), |
| 195 | ), |
| 196 | ), |
| 197 | { concurrency: "unbounded" }, |
| 198 | ); |
| 199 | for (const name of ["team-a", "team-b", "team-c"]) { |
| 200 | expect(yield* readStoreCredentials(store, "default", name)).toEqual( |
| 201 | credentials(`https://${name}.workers.dev`), |
| 202 | ); |
| 203 | } |
| 204 | }).pipe(Effect.provide(NodeServices.layer)), |
| 205 | ); |
| 206 | }, |
| 207 | ); |