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 { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { |
| 4 | exportState, |
| 5 | InMemoryService, |
| 6 | State, |
| 7 | type ResourceState, |
| 8 | type StateService, |
| 9 | } from "@/State"; |
| 10 | |
| 11 | /** The store under test is provided as the `State` service for one call. */ |
| 12 | const withState = <A, E, R>(state: StateService, effect: Effect.Effect<A, E, R>) => |
| 13 | Effect.provideService(effect, State, Effect.succeed(state)); |
| 14 | |
| 15 | describe("exportState", { tags: ["unit", "local"] }, () => { |
| 16 | it.effect("exports every stack/stage/resource as one document", () => |
| 17 | Effect.gen(function* () { |
| 18 | const state = yield* InMemoryService({ |
| 19 | "app-east": { |
| 20 | dev: { |
| 21 | WebServer: resource("WebServer", { instanceId: "i-1" }), |
| 22 | Database: resource("Database", { arn: "arn:db" }), |
| 23 | }, |
| 24 | prod: { |
| 25 | WebServer: resource("WebServer", { instanceId: "i-2" }), |
| 26 | }, |
| 27 | }, |
| 28 | "app-west": { |
| 29 | dev: { |
| 30 | Bucket: resource("Bucket", { bucketName: "b-1" }), |
| 31 | }, |
| 32 | }, |
| 33 | }); |
| 34 | |
| 35 | const exported = yield* withState(state, exportState()); |
| 36 | |
| 37 | // Deterministic order: stack, then stage, then FQN. |
| 38 | expect(exported.resources.map((r) => `${r.stack}/${r.stage}/${r.fqn}`)).toEqual([ |
| 39 | "app-east/dev/Database", |
| 40 | "app-east/dev/WebServer", |
| 41 | "app-east/prod/WebServer", |
| 42 | "app-west/dev/Bucket", |
| 43 | ]); |
| 44 | |
| 45 | // Records are the same values `state.get` returns — props/attr intact. |
| 46 | const webServer = exported.resources.find( |
| 47 | (r) => r.stack === "app-east" && r.stage === "dev" && r.fqn === "WebServer", |
| 48 | ); |
| 49 | expect(webServer?.state).toEqual( |
| 50 | yield* state.get({ stack: "app-east", stage: "dev", fqn: "WebServer" }), |
| 51 | ); |
| 52 | }), |
| 53 | ); |
| 54 | |
| 55 | it.effect("filters by stack", () => |
| 56 | Effect.gen(function* () { |
| 57 | const state = yield* InMemoryService({ |
| 58 | "app-east": { dev: { A: resource("A", {}) } }, |
| 59 | "app-west": { dev: { B: resource("B", {}) } }, |
| 60 | }); |
| 61 | |
| 62 | const exported = yield* withState(state, exportState({ stack: "app-west" })); |
| 63 | |
| 64 | expect(exported.resources.map((r) => `${r.stack}/${r.fqn}`)).toEqual(["app-west/B"]); |
| 65 | }), |
| 66 | ); |
| 67 | |
| 68 | it.effect("filters by stack and stage", () => |
| 69 | Effect.gen(function* () { |
| 70 | const state = yield* InMemoryService({ |
| 71 | app: { |
| 72 | dev: { A: resource("A", {}) }, |
| 73 | prod: { B: resource("B", {}) }, |
| 74 | }, |
| 75 | }); |
| 76 | |
| 77 | const exported = yield* withState( |
| 78 | state, |
| 79 | exportState({ |
| 80 | stack: "app", |
| 81 | stage: "prod", |
| 82 | }), |
| 83 | ); |
| 84 | |
| 85 | expect(exported.resources.map((r) => `${r.stack}/${r.stage}/${r.fqn}`)).toEqual([ |
| 86 | "app/prod/B", |
| 87 | ]); |
| 88 | }), |
| 89 | ); |
| 90 | |
| 91 | it.effect("returns an empty document for an empty store", () => |
| 92 | Effect.gen(function* () { |
| 93 | const state = yield* InMemoryService({}); |
| 94 | expect(yield* withState(state, exportState())).toEqual({ resources: [] }); |
| 95 | }), |
| 96 | ); |
| 97 | |
| 98 | it.effect("tolerates a missing stack/stage filter target", () => |
| 99 | Effect.gen(function* () { |
| 100 | const state = yield* InMemoryService({ |
| 101 | app: { dev: { A: resource("A", {}) } }, |
| 102 | }); |
| 103 | expect(yield* withState(state, exportState({ stack: "nope" }))).toEqual({ |
| 104 | resources: [], |
| 105 | }); |
| 106 | expect(yield* withState(state, exportState({ stack: "app", stage: "nope" }))).toEqual({ |
| 107 | resources: [], |
| 108 | }); |
| 109 | }), |
| 110 | ); |
| 111 | |
| 112 | it.effect("skips records deleted between list and get", () => |
| 113 | Effect.gen(function* () { |
| 114 | const inner = yield* InMemoryService({ |
| 115 | app: { |
| 116 | dev: { |
| 117 | A: resource("A", {}), |
| 118 | B: resource("B", {}), |
| 119 | }, |
| 120 | }, |
| 121 | }); |
| 122 | // Simulate a concurrent destroy racing the export: `list` still |
| 123 | // returns the FQN but `get` comes back empty. |
| 124 | const racy = { |
| 125 | ...inner, |
| 126 | get: (request: { stack: string; stage: string; fqn: string }) => |
| 127 | request.fqn === "A" ? Effect.succeed(undefined) : inner.get(request), |
| 128 | }; |
| 129 | |
| 130 | const exported = yield* withState(racy, exportState()); |
| 131 | |
| 132 | expect(exported.resources.map((r) => r.fqn)).toEqual(["B"]); |
| 133 | }), |
| 134 | ); |
| 135 | }); |
| 136 | |
| 137 | const resource = (fqn: string, attr: Record<string, unknown>): ResourceState => ({ |
| 138 | resourceType: "test:resource", |
| 139 | namespace: undefined, |
| 140 | fqn, |
| 141 | logicalId: fqn, |
| 142 | instanceId: `instance-${fqn}`, |
| 143 | providerVersion: 1, |
| 144 | status: "created", |
| 145 | downstream: [], |
| 146 | bindings: [], |
| 147 | props: {}, |
| 148 | attr, |
| 149 | }); |