The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Effect from "effect/Effect"; |
| 3 | import { InMemoryService, syncState, type ResourceState, type StateService } from "@/State"; |
| 4 | |
| 5 | describe("syncState", { tags: ["unit", "local"] }, () => { |
| 6 | it.effect("copies source resources and overwrites matching destination resources", () => |
| 7 | Effect.gen(function* () { |
| 8 | const sourceA = resource("resource-a", { value: "source-a" }); |
| 9 | const sourceB = resource("resource-b", { value: "source-b" }); |
| 10 | const destinationA = resource("resource-a", { value: "destination-a" }); |
| 11 | |
| 12 | const source = yield* InMemoryService({ |
| 13 | app: { dev: { "resource-a": sourceA, "resource-b": sourceB } }, |
| 14 | }); |
| 15 | const destination = yield* InMemoryService({ app: { dev: { "resource-a": destinationA } } }); |
| 16 | |
| 17 | yield* syncState(source, destination); |
| 18 | |
| 19 | yield* expectStage(destination, "app", "dev", { |
| 20 | "resource-a": sourceA, |
| 21 | "resource-b": sourceB, |
| 22 | }); |
| 23 | }), |
| 24 | ); |
| 25 | |
| 26 | it.effect("deletes resources from destination when they are absent from source", () => |
| 27 | Effect.gen(function* () { |
| 28 | const source = yield* InMemoryService({ |
| 29 | app: { dev: { "resource-a": resource("resource-a", { value: "source-a" }) } }, |
| 30 | }); |
| 31 | const destination = yield* InMemoryService({ |
| 32 | app: { |
| 33 | dev: { |
| 34 | "resource-a": resource("resource-a", { value: "destination-a" }), |
| 35 | "resource-b": resource("resource-b", { value: "destination-b" }), |
| 36 | }, |
| 37 | prod: { "resource-c": resource("resource-c", { value: "destination-c" }) }, |
| 38 | }, |
| 39 | oldApp: { dev: { "resource-d": resource("resource-d", { value: "destination-d" }) } }, |
| 40 | }); |
| 41 | |
| 42 | yield* syncState(source, destination); |
| 43 | |
| 44 | yield* expectStage(destination, "app", "dev", { |
| 45 | "resource-a": resource("resource-a", { value: "source-a" }), |
| 46 | }); |
| 47 | yield* expectStage(destination, "app", "prod", {}); |
| 48 | yield* expectStage(destination, "oldApp", "dev", {}); |
| 49 | expect(yield* destination.listStacks()).toEqual(["app"]); |
| 50 | }), |
| 51 | ); |
| 52 | }); |
| 53 | |
| 54 | const resource = (fqn: string, attr: Record<string, unknown>): ResourceState => ({ |
| 55 | resourceType: "test:resource", |
| 56 | namespace: undefined, |
| 57 | fqn, |
| 58 | logicalId: fqn, |
| 59 | instanceId: `instance-${fqn}`, |
| 60 | providerVersion: 1, |
| 61 | status: "created", |
| 62 | downstream: [], |
| 63 | bindings: [], |
| 64 | props: {}, |
| 65 | attr, |
| 66 | }); |
| 67 | |
| 68 | const listStage = Effect.fn(function* (state: StateService, stack: string, stage: string) { |
| 69 | const fqns = yield* state.list({ stack, stage }); |
| 70 | const entries = yield* Effect.forEach( |
| 71 | fqns, |
| 72 | Effect.fn(function* (fqn) { |
| 73 | return [fqn, yield* state.get({ stack, stage, fqn })] as const; |
| 74 | }), |
| 75 | ); |
| 76 | return Object.fromEntries(entries); |
| 77 | }); |
| 78 | |
| 79 | const expectStage = Effect.fn(function* ( |
| 80 | state: StateService, |
| 81 | stack: string, |
| 82 | stage: string, |
| 83 | expected: Record<string, ResourceState>, |
| 84 | ) { |
| 85 | expect(yield* listStage(state, stack, stage)).toEqual(expected); |
| 86 | }); |