@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 166 lines 6.28 KB
Raw
1 import { describe, expect } from "@alchemy.run/test";
2 import * as Test from "@alchemy.run/test/Alchemy";
3 import * as Effect from "effect/Effect";
4 import * as Layer from "effect/Layer";
5 /**
6 * `--force` must refresh stale bound attributes.
7 *
8 * A forced deploy upgrades every noop to an update, so each upstream's
9 * `reconcile` re-runs and may produce FRESH attributes (that is the point of
10 * `--force`: re-converge against real cloud state). Consumers referencing an
11 * upstream's attrs — through props or binding data — must therefore observe
12 * the forced reconcile's output, not the attribute snapshot persisted in
13 * state. Previously `resolveResource` still treated forced-noop upstreams as
14 * stable and baked their persisted attrs into consumers' plan props, so a
15 * consumer kept stale values even under `--force`.
16 */
17 import { apply } from "@/Apply";
18 import { provideFreshArtifactStore } from "@/Artifacts";
19 import * as Plan from "@/Plan";
20 import * as Stack from "@/Stack";
21 import { Stage } from "@/Stage";
22 import { InMemoryService, State, type ResourceState } from "@/State";
23 import { BindingTarget, TestLayers, TestResource } from "./test.resources.ts";
24
25 const { test } = Test.make({
26 providers: TestLayers(),
27 state: Layer.effect(
28 State,
29 Effect.sync(() => InMemoryService({})),
30 ),
31 });
32
33 const STAGE = "test";
34
35 const makeHarness = (name: string) => {
36 const store: Record<string, Record<string, Record<string, any>>> = {};
37 const stateLayer = Layer.effect(
38 State,
39 Effect.sync(() => InMemoryService(store)),
40 );
41 const compile = (effect: Effect.Effect<any, any, any>) =>
42 (effect as Effect.Effect<any, any, never>).pipe(
43 Stack.make({
44 name,
45 providers: TestLayers() as Layer.Layer<any, never, any>,
46 state: stateLayer,
47 }),
48 );
49 const deploy = (
50 effect: Effect.Effect<any, any, any>,
51 options?: Plan.MakePlanOptions,
52 ): Effect.Effect<any, any, never> =>
53 compile(effect).pipe(
54 Effect.flatMap((compiled: any) =>
55 Plan.make(compiled, options).pipe(Effect.flatMap(apply), Effect.provide(compiled.services)),
56 ),
57 Effect.provide(Layer.succeed(Stage, STAGE)),
58 provideFreshArtifactStore,
59 ) as unknown as Effect.Effect<any, any, never>;
60 const plan = (
61 effect: Effect.Effect<any, any, any>,
62 options?: Plan.MakePlanOptions,
63 ): Effect.Effect<any, any, never> =>
64 compile(effect).pipe(
65 Effect.flatMap((compiled: any) =>
66 Plan.make(compiled, options).pipe(Effect.provide(compiled.services)),
67 ),
68 Effect.provide(Layer.succeed(Stage, STAGE)),
69 provideFreshArtifactStore,
70 ) as unknown as Effect.Effect<any, any, never>;
71 const getRow = (fqn: string) => Effect.sync(() => store[name]?.[STAGE]?.[fqn] as ResourceState);
72 const setRow = (fqn: string, value: ResourceState) =>
73 Effect.sync(() => void (store[name][STAGE][fqn] = value));
74 return { deploy, plan, getRow, setRow };
75 };
76
77 describe("--force refreshes stale bound attrs", { tags: ["unit", "local"] }, () => {
78 test(
79 "a consumer's props observe the forced upstream reconcile, not stale state",
80 Effect.gen(function* () {
81 const { deploy, getRow, setRow } = makeHarness("force-props");
82 const program = Effect.gen(function* () {
83 const upstream = yield* TestResource("Upstream", { string: "fresh" });
84 const consumer = yield* TestResource("Consumer", {
85 string: upstream.string,
86 });
87 return consumer;
88 });
89
90 yield* deploy(program);
91
92 // Simulate drift: the persisted attr snapshot is stale relative to
93 // what the provider's reconcile would produce today.
94 const row = yield* getRow("Upstream");
95 yield* setRow("Upstream", {
96 ...row,
97 attr: { ...(row.attr as any), string: "stale" },
98 } as ResourceState);
99
100 yield* deploy(program, { force: true });
101
102 // The forced upstream reconcile produced "fresh" again...
103 expect(((yield* getRow("Upstream")).attr as any).string).toBe("fresh");
104 // ...and the consumer observed IT — not the stale persisted snapshot.
105 expect(((yield* getRow("Consumer")).attr as any).string).toBe("fresh");
106 expect(((yield* getRow("Consumer")).props as any).string).toBe("fresh");
107 }),
108 );
109
110 test(
111 "binding data observes the forced upstream reconcile, not stale state",
112 Effect.gen(function* () {
113 const { deploy, getRow, setRow } = makeHarness("force-bindings");
114 const program = Effect.gen(function* () {
115 const upstream = yield* TestResource("Upstream", { string: "fresh" });
116 const host = yield* BindingTarget("Host", { name: "host" });
117 yield* host.bind("Cap", { env: { UP: upstream.string } });
118 return host;
119 });
120
121 yield* deploy(program);
122
123 const row = yield* getRow("Upstream");
124 yield* setRow("Upstream", {
125 ...row,
126 attr: { ...(row.attr as any), string: "stale" },
127 } as ResourceState);
128
129 yield* deploy(program, { force: true });
130
131 // BindingTarget's reconcile merges binding env into its attrs — the
132 // bound value must come from the forced reconcile's fresh output.
133 expect(((yield* getRow("Host")).attr as any).env).toEqual({
134 UP: "fresh",
135 });
136 // The persisted binding row carries the fresh payload too.
137 expect((yield* getRow("Host")).bindings).toEqual([
138 { sid: "Cap", data: { env: { UP: "fresh" } } },
139 ]);
140 }),
141 );
142
143 test(
144 "an unforced no-change redeploy stays all-noop (control)",
145 Effect.gen(function* () {
146 const { deploy, plan } = makeHarness("force-control");
147 const program = Effect.gen(function* () {
148 const upstream = yield* TestResource("Upstream", { string: "fresh" });
149 const host = yield* BindingTarget("Host", { name: "host" });
150 yield* host.bind("Cap", { env: { UP: upstream.string } });
151 return host;
152 });
153 yield* deploy(program);
154
155 // The force fix must not destabilize ordinary planning.
156 const p: any = yield* plan(program);
157 expect(p.resources.Upstream.action).toBe("noop");
158 expect(p.resources.Host.action).toBe("noop");
159
160 // And a forced plan upgrades everything to an update.
161 const forced: any = yield* plan(program, { force: true });
162 expect(forced.resources.Upstream.action).toBe("update");
163 expect(forced.resources.Host.action).toBe("update");
164 }),
165 );
166 });