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 Cause from "effect/Cause"; |
| 3 | import * as Config from "effect/Config"; |
| 4 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 5 | import * as Context from "effect/Context"; |
| 6 | import * as Effect from "effect/Effect"; |
| 7 | import * as Exit from "effect/Exit"; |
| 8 | import * as Layer from "effect/Layer"; |
| 9 | import * as Redacted from "effect/Redacted"; |
| 10 | import * as Output from "@/Output"; |
| 11 | import { ref as makeRef } from "@/Ref"; |
| 12 | import type { ResourceLike } from "@/Resource"; |
| 13 | import { Stack } from "@/Stack"; |
| 14 | import { Stage } from "@/Stage"; |
| 15 | import { inMemoryState } from "@/State/InMemoryState"; |
| 16 | import type { ResourceState } from "@/State/ResourceState"; |
| 17 | |
| 18 | const provideState = <A, E, R>(effect: Effect.Effect<A, E, R>) => |
| 19 | effect.pipe(Effect.provide(inMemoryState())); |
| 20 | |
| 21 | const fakeResource = <T extends string, A extends object>( |
| 22 | type: T, |
| 23 | fqn: string, |
| 24 | logicalId: string = fqn, |
| 25 | ): ResourceLike<T, any, A> => |
| 26 | ({ Type: type, FQN: fqn, LogicalId: logicalId, Namespace: undefined }) as any; |
| 27 | |
| 28 | describe("Output.evaluate", { tags: ["unit", "local"] }, () => { |
| 29 | describe("primitives and plain values", () => { |
| 30 | it.effect("returns primitive values as-is", () => |
| 31 | provideState( |
| 32 | Effect.gen(function* () { |
| 33 | expect(yield* Output.evaluate(42, {})).toBe(42); |
| 34 | expect(yield* Output.evaluate("hello", {})).toBe("hello"); |
| 35 | expect(yield* Output.evaluate(true, {})).toBe(true); |
| 36 | expect(yield* Output.evaluate(null, {})).toBe(null); |
| 37 | expect(yield* Output.evaluate(undefined, {})).toBe(undefined); |
| 38 | }), |
| 39 | ), |
| 40 | ); |
| 41 | |
| 42 | it.effect("recursively evaluates plain objects", () => |
| 43 | provideState( |
| 44 | Effect.gen(function* () { |
| 45 | const result = yield* Output.evaluate({ a: 1, b: { c: "x" } }, {}); |
| 46 | expect(result).toEqual({ a: 1, b: { c: "x" } }); |
| 47 | }), |
| 48 | ), |
| 49 | ); |
| 50 | |
| 51 | it.effect("recursively evaluates arrays", () => |
| 52 | provideState( |
| 53 | Effect.gen(function* () { |
| 54 | const result = yield* Output.evaluate([1, "two", { k: 3 }], {}); |
| 55 | expect(result).toEqual([1, "two", { k: 3 }]); |
| 56 | }), |
| 57 | ), |
| 58 | ); |
| 59 | }); |
| 60 | |
| 61 | describe("Redacted", () => { |
| 62 | it.effect("preserves Redacted values at the top level", () => |
| 63 | provideState( |
| 64 | Effect.gen(function* () { |
| 65 | const secret = Redacted.make("hunter2"); |
| 66 | const result = yield* Output.evaluate(secret, {}); |
| 67 | expect(Redacted.isRedacted(result)).toBe(true); |
| 68 | expect(Redacted.value(result as Redacted.Redacted<string>)).toBe("hunter2"); |
| 69 | }), |
| 70 | ), |
| 71 | ); |
| 72 | |
| 73 | it.effect("preserves Redacted values nested inside an object", () => |
| 74 | provideState( |
| 75 | Effect.gen(function* () { |
| 76 | const secret = Redacted.make("hunter2"); |
| 77 | const result = yield* Output.evaluate({ value: secret, name: "x" }, {}); |
| 78 | expect(result.name).toBe("x"); |
| 79 | expect(Redacted.isRedacted(result.value)).toBe(true); |
| 80 | expect(Redacted.value(result.value)).toBe("hunter2"); |
| 81 | }), |
| 82 | ), |
| 83 | ); |
| 84 | |
| 85 | it.effect("preserves Redacted values nested inside an array", () => |
| 86 | provideState( |
| 87 | Effect.gen(function* () { |
| 88 | const secret = Redacted.make("hunter2"); |
| 89 | const [result] = yield* Output.evaluate([secret], {}); |
| 90 | expect(Redacted.isRedacted(result)).toBe(true); |
| 91 | expect(Redacted.value(result)).toBe("hunter2"); |
| 92 | }), |
| 93 | ), |
| 94 | ); |
| 95 | }); |
| 96 | |
| 97 | describe("Config", () => { |
| 98 | it.effect("resolves a Config value at the top level", () => |
| 99 | provideState( |
| 100 | Effect.gen(function* () { |
| 101 | const result = yield* Output.evaluate(Config.succeed(1337), {}); |
| 102 | expect(result).toBe(1337); |
| 103 | }), |
| 104 | ), |
| 105 | ); |
| 106 | |
| 107 | it.effect("resolves a Config value nested inside an object", () => |
| 108 | provideState( |
| 109 | Effect.gen(function* () { |
| 110 | const result = yield* Output.evaluate( |
| 111 | { port: Config.succeed(8080), host: "localhost" }, |
| 112 | {}, |
| 113 | ); |
| 114 | expect(result).toEqual({ port: 8080, host: "localhost" }); |
| 115 | }), |
| 116 | ), |
| 117 | ); |
| 118 | |
| 119 | it.effect("resolves a Config value nested inside an array", () => |
| 120 | provideState( |
| 121 | Effect.gen(function* () { |
| 122 | const [result] = yield* Output.evaluate([Config.succeed(42)], {}); |
| 123 | expect(result).toBe(42); |
| 124 | }), |
| 125 | ), |
| 126 | ); |
| 127 | |
| 128 | it.effect("resolves a Config against the ConfigProvider environment", () => |
| 129 | provideState( |
| 130 | Effect.gen(function* () { |
| 131 | const result = yield* Output.evaluate( |
| 132 | { port: Config.Number("PORT").pipe(Config.withDefault(1337)) }, |
| 133 | {}, |
| 134 | ).pipe( |
| 135 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: { PORT: "8080" } }))), |
| 136 | ); |
| 137 | expect(result).toEqual({ port: 8080 }); |
| 138 | }), |
| 139 | ), |
| 140 | ); |
| 141 | |
| 142 | it.effect("a Config resolving to a Redacted keeps it wrapped", () => |
| 143 | provideState( |
| 144 | Effect.gen(function* () { |
| 145 | const result = yield* Output.evaluate(Config.succeed(Redacted.make("hunter2")), {}); |
| 146 | expect(Redacted.isRedacted(result)).toBe(true); |
| 147 | expect(Redacted.value(result as unknown as Redacted.Redacted<string>)).toBe("hunter2"); |
| 148 | }), |
| 149 | ), |
| 150 | ); |
| 151 | }); |
| 152 | |
| 153 | describe("LiteralExpr", () => { |
| 154 | it.effect("evaluates Output.literal(value)", () => |
| 155 | provideState( |
| 156 | Effect.gen(function* () { |
| 157 | const expr = Output.literal("foo"); |
| 158 | expect(yield* Output.evaluate(expr, {})).toBe("foo"); |
| 159 | }), |
| 160 | ), |
| 161 | ); |
| 162 | |
| 163 | it.effect("evaluates a literal nested within an object", () => |
| 164 | provideState( |
| 165 | Effect.gen(function* () { |
| 166 | const result = yield* Output.evaluate({ greeting: Output.literal("hi") }, {}); |
| 167 | expect(result).toEqual({ greeting: "hi" }); |
| 168 | }), |
| 169 | ), |
| 170 | ); |
| 171 | }); |
| 172 | |
| 173 | describe("ResourceExpr", () => { |
| 174 | it.effect("resolves to the upstream value keyed by FQN", () => |
| 175 | provideState( |
| 176 | Effect.gen(function* () { |
| 177 | const src = fakeResource("Test.Bucket", "MyBucket"); |
| 178 | const expr = Output.of(src); |
| 179 | const result = yield* Output.evaluate(expr, { MyBucket: { name: "my-bucket" } }); |
| 180 | expect(result).toEqual({ name: "my-bucket" }); |
| 181 | }), |
| 182 | ), |
| 183 | ); |
| 184 | |
| 185 | it.effect("fails with MissingSourceError when upstream is absent", () => |
| 186 | provideState( |
| 187 | Effect.gen(function* () { |
| 188 | const src = fakeResource("Test.Bucket", "Missing"); |
| 189 | const expr = Output.of(src); |
| 190 | const exit = yield* Effect.exit(Output.evaluate(expr, {})); |
| 191 | expect(Exit.isFailure(exit)).toBe(true); |
| 192 | if (Exit.isFailure(exit)) { |
| 193 | const failure = exit.cause.toJSON() as any; |
| 194 | expect(JSON.stringify(failure)).toContain("MissingSourceError"); |
| 195 | } |
| 196 | }), |
| 197 | ), |
| 198 | ); |
| 199 | |
| 200 | it.effect("evaluates a raw resource (isResource branch)", () => |
| 201 | provideState( |
| 202 | Effect.gen(function* () { |
| 203 | const src = fakeResource("Test.Bucket", "RawBucket"); |
| 204 | const result = yield* Output.evaluate(src as any, { RawBucket: { ok: true } }); |
| 205 | expect(result).toEqual({ ok: true }); |
| 206 | }), |
| 207 | ), |
| 208 | ); |
| 209 | |
| 210 | it.effect("raw resource with missing upstream fails", () => |
| 211 | provideState( |
| 212 | Effect.gen(function* () { |
| 213 | const src = fakeResource("Test.Bucket", "Gone"); |
| 214 | const exit = yield* Effect.exit(Output.evaluate(src as any, {})); |
| 215 | expect(Exit.isFailure(exit)).toBe(true); |
| 216 | }), |
| 217 | ), |
| 218 | ); |
| 219 | |
| 220 | it("classifies resource expressions when a stable kind shadows the discriminator", () => { |
| 221 | const src = fakeResource("Test.Database", "Database"); |
| 222 | const expr = new Output.ResourceExpr(src, { kind: "postgresql" }); |
| 223 | |
| 224 | expect((expr as any).kind).toBe("postgresql"); |
| 225 | expect(Output.isResourceExpr(expr)).toBe(true); |
| 226 | }); |
| 227 | }); |
| 228 | |
| 229 | describe("PropExpr", () => { |
| 230 | it.effect("accesses a property on a resource expression", () => |
| 231 | provideState( |
| 232 | Effect.gen(function* () { |
| 233 | const src = fakeResource<"Test.Bucket", { name: string }>("Test.Bucket", "B"); |
| 234 | const expr = Output.of(src) as any; |
| 235 | const result = yield* Output.evaluate(expr.name, { B: { name: "the-name" } }); |
| 236 | expect(result).toBe("the-name"); |
| 237 | }), |
| 238 | ), |
| 239 | ); |
| 240 | |
| 241 | it.effect("returns undefined when accessing missing property", () => |
| 242 | provideState( |
| 243 | Effect.gen(function* () { |
| 244 | const src = fakeResource("Test.Bucket", "B2"); |
| 245 | const expr = Output.of(src) as any; |
| 246 | const result = yield* Output.evaluate(expr.missing, { B2: { other: 1 } }); |
| 247 | expect(result).toBeUndefined(); |
| 248 | }), |
| 249 | ), |
| 250 | ); |
| 251 | |
| 252 | it.effect("supports nested property access", () => |
| 253 | provideState( |
| 254 | Effect.gen(function* () { |
| 255 | const src = fakeResource("Test.Bucket", "B3"); |
| 256 | const expr = Output.of(src) as any; |
| 257 | const result = yield* Output.evaluate(expr.nested.deep, { |
| 258 | B3: { nested: { deep: "value" } }, |
| 259 | }); |
| 260 | expect(result).toBe("value"); |
| 261 | }), |
| 262 | ), |
| 263 | ); |
| 264 | }); |
| 265 | |
| 266 | describe("ApplyExpr (map)", () => { |
| 267 | it.effect("applies a synchronous function over a literal", () => |
| 268 | provideState( |
| 269 | Effect.gen(function* () { |
| 270 | const expr = Output.map(Output.literal(2), (n) => n * 3); |
| 271 | expect(yield* Output.evaluate(expr, {})).toBe(6); |
| 272 | }), |
| 273 | ), |
| 274 | ); |
| 275 | |
| 276 | it.effect("composes multiple maps", () => |
| 277 | provideState( |
| 278 | Effect.gen(function* () { |
| 279 | const expr = Output.literal(2).pipe( |
| 280 | Output.map((n: number) => n + 1), |
| 281 | Output.map((n: number) => n * 10), |
| 282 | ); |
| 283 | expect(yield* Output.evaluate(expr, {})).toBe(30); |
| 284 | }), |
| 285 | ), |
| 286 | ); |
| 287 | |
| 288 | it.effect("maps over a resource attribute", () => |
| 289 | provideState( |
| 290 | Effect.gen(function* () { |
| 291 | const src = fakeResource("Test.Bucket", "B4"); |
| 292 | const expr = (Output.of(src) as any).name.pipe( |
| 293 | Output.map((s: string) => s.toUpperCase()), |
| 294 | ); |
| 295 | const result = yield* Output.evaluate(expr, { B4: { name: "abc" } }); |
| 296 | expect(result).toBe("ABC"); |
| 297 | }), |
| 298 | ), |
| 299 | ); |
| 300 | }); |
| 301 | |
| 302 | describe("EffectExpr (mapEffect)", () => { |
| 303 | it.effect("evaluates an effectful transformation", () => |
| 304 | provideState( |
| 305 | Effect.gen(function* () { |
| 306 | const expr = Output.literal(5).pipe( |
| 307 | Output.mapEffect((n: number) => Effect.succeed(n * 2)), |
| 308 | ); |
| 309 | expect(yield* Output.evaluate(expr, {})).toBe(10); |
| 310 | }), |
| 311 | ), |
| 312 | ); |
| 313 | |
| 314 | it.effect("chains multiple effectful transformations", () => |
| 315 | provideState( |
| 316 | Effect.gen(function* () { |
| 317 | const expr = Output.literal("a").pipe( |
| 318 | Output.mapEffect((s: string) => Effect.succeed(s + "b")), |
| 319 | Output.mapEffect((s) => Effect.succeed(s + "c")), |
| 320 | ); |
| 321 | expect(yield* Output.evaluate(expr, {})).toBe("abc"); |
| 322 | }), |
| 323 | ), |
| 324 | ); |
| 325 | }); |
| 326 | |
| 327 | describe("FlatMapExpr (flatMap)", () => { |
| 328 | it.effect("flattens an Output returned from the function", () => |
| 329 | provideState( |
| 330 | Effect.gen(function* () { |
| 331 | const expr = Output.literal(5).pipe(Output.flatMap((n: number) => Output.literal(n * 2))); |
| 332 | expect(yield* Output.evaluate(expr, {})).toBe(10); |
| 333 | }), |
| 334 | ), |
| 335 | ); |
| 336 | |
| 337 | it.effect("supports the data-first form", () => |
| 338 | provideState( |
| 339 | Effect.gen(function* () { |
| 340 | const expr = Output.flatMap(Output.literal("a"), (s: string) => Output.literal(s + "b")); |
| 341 | expect(yield* Output.evaluate(expr, {})).toBe("ab"); |
| 342 | }), |
| 343 | ), |
| 344 | ); |
| 345 | |
| 346 | it.effect("chains multiple flatMaps", () => |
| 347 | provideState( |
| 348 | Effect.gen(function* () { |
| 349 | const expr = Output.literal(1).pipe( |
| 350 | Output.flatMap((n: number) => Output.literal(n + 1)), |
| 351 | Output.flatMap((n: number) => Output.literal(n * 10)), |
| 352 | ); |
| 353 | expect(yield* Output.evaluate(expr, {})).toBe(20); |
| 354 | }), |
| 355 | ), |
| 356 | ); |
| 357 | |
| 358 | it.effect("flatMaps into another resource's output", () => |
| 359 | provideState( |
| 360 | Effect.gen(function* () { |
| 361 | const a = fakeResource("Test.A", "FA"); |
| 362 | const b = fakeResource("Test.B", "FB"); |
| 363 | const expr = (Output.of(a) as any).name.pipe( |
| 364 | Output.flatMap(() => (Output.of(b) as any).name), |
| 365 | ); |
| 366 | const result = yield* Output.evaluate(expr, { |
| 367 | FA: { name: "a-name" }, |
| 368 | FB: { name: "b-name" }, |
| 369 | }); |
| 370 | expect(result).toBe("b-name"); |
| 371 | }), |
| 372 | ), |
| 373 | ); |
| 374 | |
| 375 | it.effect("can flatMap into a non-Output literal value", () => |
| 376 | provideState( |
| 377 | Effect.gen(function* () { |
| 378 | const expr = Output.literal(3).pipe( |
| 379 | Output.flatMap((n: number) => Output.asOutput(n + 1)), |
| 380 | ); |
| 381 | expect(yield* Output.evaluate(expr, {})).toBe(4); |
| 382 | }), |
| 383 | ), |
| 384 | ); |
| 385 | |
| 386 | it("tracks only the source expression as upstream", () => { |
| 387 | const a = fakeResource("Test.A", "UA"); |
| 388 | const expr = (Output.of(a) as any).name.pipe( |
| 389 | Output.flatMap((s: string) => Output.literal(s)), |
| 390 | ); |
| 391 | expect(Object.keys(Output.upstream(expr))).toEqual(["UA"]); |
| 392 | }); |
| 393 | }); |
| 394 | |
| 395 | describe("method-style combinators on a proxied Output", () => { |
| 396 | // Resource outputs are Proxies, so `.map(fn)` / `.mapEffect(fn)` / |
| 397 | // `.flatMap(fn)` / `.apply(fn)` / `.effect(fn)` must be callable as |
| 398 | // methods (not just via `Output.map(output, fn)` / `.pipe(...)`). |
| 399 | const resourceOutput = () => { |
| 400 | const src = fakeResource<"Test.Bucket", { name: string }>("Test.Bucket", "M"); |
| 401 | return (Output.of(src) as any).name as Output.Output<string>; |
| 402 | }; |
| 403 | |
| 404 | it.effect(".map(fn) builds an ApplyExpr", () => |
| 405 | provideState( |
| 406 | Effect.gen(function* () { |
| 407 | const expr = (resourceOutput() as any).map((s: string) => s.toUpperCase()); |
| 408 | expect(Output.isApplyExpr(expr)).toBe(true); |
| 409 | expect(yield* Output.evaluate(expr, { M: { name: "abc" } })).toBe("ABC"); |
| 410 | }), |
| 411 | ), |
| 412 | ); |
| 413 | |
| 414 | it.effect(".apply(fn) builds an ApplyExpr", () => |
| 415 | provideState( |
| 416 | Effect.gen(function* () { |
| 417 | const expr = (resourceOutput() as any).apply((s: string) => s.toUpperCase()); |
| 418 | expect(Output.isApplyExpr(expr)).toBe(true); |
| 419 | expect(yield* Output.evaluate(expr, { M: { name: "abc" } })).toBe("ABC"); |
| 420 | }), |
| 421 | ), |
| 422 | ); |
| 423 | |
| 424 | it.effect(".mapEffect(fn) builds an EffectExpr", () => |
| 425 | provideState( |
| 426 | Effect.gen(function* () { |
| 427 | const expr = (resourceOutput() as any).mapEffect((s: string) => Effect.succeed(s + "!")); |
| 428 | expect(Output.isEffectExpr(expr)).toBe(true); |
| 429 | expect(yield* Output.evaluate(expr, { M: { name: "abc" } })).toBe("abc!"); |
| 430 | }), |
| 431 | ), |
| 432 | ); |
| 433 | |
| 434 | it.effect(".effect(fn) builds an EffectExpr", () => |
| 435 | provideState( |
| 436 | Effect.gen(function* () { |
| 437 | const expr = (resourceOutput() as any).effect((s: string) => Effect.succeed(s + "!")); |
| 438 | expect(Output.isEffectExpr(expr)).toBe(true); |
| 439 | expect(yield* Output.evaluate(expr, { M: { name: "abc" } })).toBe("abc!"); |
| 440 | }), |
| 441 | ), |
| 442 | ); |
| 443 | |
| 444 | it.effect(".flatMap(fn) builds a FlatMapExpr", () => |
| 445 | provideState( |
| 446 | Effect.gen(function* () { |
| 447 | const expr = (resourceOutput() as any).flatMap((s: string) => Output.literal(s + "?")); |
| 448 | expect(Output.isFlatMapExpr(expr)).toBe(true); |
| 449 | expect(yield* Output.evaluate(expr, { M: { name: "abc" } })).toBe("abc?"); |
| 450 | }), |
| 451 | ), |
| 452 | ); |
| 453 | }); |
| 454 | |
| 455 | describe("AllExpr", () => { |
| 456 | it.effect("evaluates all wrapped outputs in parallel", () => |
| 457 | provideState( |
| 458 | Effect.gen(function* () { |
| 459 | const expr = Output.all(Output.literal(1), Output.literal("two"), Output.literal(true)); |
| 460 | const result = yield* Output.evaluate(expr, {}); |
| 461 | expect(result).toEqual([1, "two", true]); |
| 462 | }), |
| 463 | ), |
| 464 | ); |
| 465 | |
| 466 | it.effect("evaluates all with resource expressions", () => |
| 467 | provideState( |
| 468 | Effect.gen(function* () { |
| 469 | const a = fakeResource("Test.A", "A"); |
| 470 | const b = fakeResource("Test.B", "B"); |
| 471 | const expr = Output.all(Output.of(a), Output.of(b)); |
| 472 | const result = yield* Output.evaluate(expr, { A: { x: 1 }, B: { y: 2 } }); |
| 473 | expect(result).toEqual([{ x: 1 }, { y: 2 }]); |
| 474 | }), |
| 475 | ), |
| 476 | ); |
| 477 | }); |
| 478 | |
| 479 | describe("RefExpr", () => { |
| 480 | const provideStackStage = <A, E, R>( |
| 481 | effect: Effect.Effect<A, E, R>, |
| 482 | stack = "myStack", |
| 483 | stage = "myStage", |
| 484 | ) => |
| 485 | effect.pipe( |
| 486 | Effect.provide( |
| 487 | Layer.mergeAll(Layer.succeed(Stack, { name: stack } as any), Layer.succeed(Stage, stage)), |
| 488 | ), |
| 489 | ); |
| 490 | |
| 491 | it.effect("resolves a Ref against in-memory state", () => |
| 492 | Effect.gen(function* () { |
| 493 | const initial = { |
| 494 | myStack: { |
| 495 | myStage: { |
| 496 | myResource: { |
| 497 | fqn: "myResource", |
| 498 | attr: { hello: "world" }, |
| 499 | } as unknown as ResourceState, |
| 500 | }, |
| 501 | }, |
| 502 | }; |
| 503 | const r = makeRef<ResourceLike>("myResource"); |
| 504 | const expr = Output.of(r); |
| 505 | const result = yield* provideStackStage( |
| 506 | Output.evaluate(expr, {}).pipe(Effect.provide(inMemoryState(initial))), |
| 507 | ); |
| 508 | expect(result).toEqual({ hello: "world" }); |
| 509 | }), |
| 510 | ); |
| 511 | |
| 512 | it.effect("uses explicit stack/stage when provided on the ref", () => |
| 513 | Effect.gen(function* () { |
| 514 | const initial = { |
| 515 | otherStack: { |
| 516 | otherStage: { |
| 517 | someResource: { fqn: "someResource", attr: { v: 1 } } as unknown as ResourceState, |
| 518 | }, |
| 519 | }, |
| 520 | }; |
| 521 | const r = makeRef<ResourceLike>("someResource", { |
| 522 | stack: "otherStack", |
| 523 | stage: "otherStage", |
| 524 | }); |
| 525 | const expr = Output.of(r); |
| 526 | // No Stack/Stage layers needed since the ref carries them. |
| 527 | const result = yield* Output.evaluate(expr, {}).pipe( |
| 528 | Effect.provide(inMemoryState(initial)), |
| 529 | ); |
| 530 | expect(result).toEqual({ v: 1 }); |
| 531 | }), |
| 532 | ); |
| 533 | |
| 534 | it.effect("fails with InvalidReferenceError when ref target is missing", () => |
| 535 | Effect.gen(function* () { |
| 536 | const r = makeRef<ResourceLike>("ghost", { stack: "s", stage: "t" }); |
| 537 | const expr = Output.of(r); |
| 538 | const exit = yield* Effect.exit( |
| 539 | Output.evaluate(expr, {}).pipe(Effect.provide(inMemoryState())), |
| 540 | ); |
| 541 | expect(Exit.isFailure(exit)).toBe(true); |
| 542 | if (Exit.isFailure(exit)) { |
| 543 | expect(JSON.stringify(exit.cause.toJSON())).toContain("InvalidReferenceError"); |
| 544 | } |
| 545 | }), |
| 546 | ); |
| 547 | |
| 548 | it.effect("PropExpr on a Ref reads the attribute from persisted state", () => |
| 549 | Effect.gen(function* () { |
| 550 | const initial = { |
| 551 | myStack: { |
| 552 | myStage: { |
| 553 | shared: { |
| 554 | fqn: "shared", |
| 555 | attr: { url: "https://example.com", name: "shared" }, |
| 556 | } as unknown as ResourceState, |
| 557 | }, |
| 558 | }, |
| 559 | }; |
| 560 | const r = makeRef<ResourceLike>("shared"); |
| 561 | const expr = (Output.of(r) as any).url as Output.Output<string>; |
| 562 | const result = yield* provideStackStage( |
| 563 | Output.evaluate(expr, {}).pipe(Effect.provide(inMemoryState(initial))), |
| 564 | ); |
| 565 | expect(result).toBe("https://example.com"); |
| 566 | }), |
| 567 | ); |
| 568 | }); |
| 569 | |
| 570 | describe("StackRefExpr", () => { |
| 571 | const provideStage = <A, E, R>(effect: Effect.Effect<A, E, R>, stage = "myStage") => |
| 572 | effect.pipe(Effect.provide(Layer.succeed(Stage, stage))); |
| 573 | |
| 574 | it.effect("Output.stackRef resolves to the persisted stack output", () => |
| 575 | Effect.gen(function* () { |
| 576 | const expr = yield* Output.stackRef<{ url: string }>("Backend"); |
| 577 | const result = yield* provideStage( |
| 578 | Output.evaluate(expr, {}).pipe( |
| 579 | Effect.provide( |
| 580 | inMemoryState({}, { Backend: { myStage: { url: "https://api.example.com" } } }), |
| 581 | ), |
| 582 | ), |
| 583 | ); |
| 584 | expect(result).toEqual({ url: "https://api.example.com" }); |
| 585 | }), |
| 586 | ); |
| 587 | |
| 588 | it.effect("explicit stage overrides the ambient Stage", () => |
| 589 | Effect.gen(function* () { |
| 590 | const expr = yield* Output.stackRef<{ url: string }>("Backend", { stage: "prod" }); |
| 591 | // No Stage layer is provided — the ref carries it explicitly. |
| 592 | const result = yield* Output.evaluate(expr, {}).pipe( |
| 593 | Effect.provide( |
| 594 | inMemoryState({}, { Backend: { prod: { url: "https://prod.example.com" } } }), |
| 595 | ), |
| 596 | ); |
| 597 | expect(result).toEqual({ url: "https://prod.example.com" }); |
| 598 | }), |
| 599 | ); |
| 600 | |
| 601 | it.effect("PropExpr off a stackRef reads a single attribute", () => |
| 602 | Effect.gen(function* () { |
| 603 | const backend = yield* Output.stackRef<{ url: string }>("Backend"); |
| 604 | const expr = (backend as any).url as Output.Output<string>; |
| 605 | const result = yield* provideStage( |
| 606 | Output.evaluate(expr, {}).pipe( |
| 607 | Effect.provide( |
| 608 | inMemoryState({}, { Backend: { myStage: { url: "https://api.example.com" } } }), |
| 609 | ), |
| 610 | ), |
| 611 | ); |
| 612 | expect(result).toBe("https://api.example.com"); |
| 613 | }), |
| 614 | ); |
| 615 | |
| 616 | it.effect( |
| 617 | "fails with InvalidReferenceError when the target stack/stage has no persisted output", |
| 618 | () => |
| 619 | Effect.gen(function* () { |
| 620 | const expr = yield* Output.stackRef<{ url: string }>("Backend", { stage: "ghost" }); |
| 621 | const exit = yield* Effect.exit( |
| 622 | Output.evaluate(expr, {}).pipe(Effect.provide(inMemoryState())), |
| 623 | ); |
| 624 | expect(Exit.isFailure(exit)).toBe(true); |
| 625 | if (Exit.isFailure(exit)) { |
| 626 | const err = Cause.squash(exit.cause) as Output.InvalidReferenceError; |
| 627 | expect(err._tag).toBe("InvalidReferenceError"); |
| 628 | expect(err.stack).toBe("Backend"); |
| 629 | expect(err.stage).toBe("ghost"); |
| 630 | } |
| 631 | }), |
| 632 | ); |
| 633 | }); |
| 634 | |
| 635 | describe("composition", () => { |
| 636 | it.effect("evaluates outputs nested inside arrays and objects", () => |
| 637 | provideState( |
| 638 | Effect.gen(function* () { |
| 639 | const a = fakeResource("Test.A", "RA"); |
| 640 | const b = fakeResource("Test.B", "RB"); |
| 641 | const value = { |
| 642 | list: [Output.of(a), Output.literal("lit")], |
| 643 | nested: { |
| 644 | prop: (Output.of(b) as any).name.pipe(Output.map((s: string) => `name=${s}`)), |
| 645 | }, |
| 646 | scalar: 42, |
| 647 | }; |
| 648 | const result = yield* Output.evaluate(value, { RA: { foo: "f" }, RB: { name: "bee" } }); |
| 649 | expect(result).toEqual({ |
| 650 | list: [{ foo: "f" }, "lit"], |
| 651 | nested: { prop: "name=bee" }, |
| 652 | scalar: 42, |
| 653 | }); |
| 654 | }), |
| 655 | ), |
| 656 | ); |
| 657 | }); |
| 658 | }); |
| 659 | |
| 660 | describe("Output.interpolate", { tags: ["unit", "local"] }, () => { |
| 661 | it.effect("interpolates literal values into a template", () => |
| 662 | provideState( |
| 663 | Effect.gen(function* () { |
| 664 | const expr = Output.interpolate`hello ${Output.literal("world")}!`; |
| 665 | expect(yield* Output.evaluate(expr, {})).toBe("hello world!"); |
| 666 | }), |
| 667 | ), |
| 668 | ); |
| 669 | |
| 670 | it.effect("interpolates resource attributes", () => |
| 671 | provideState( |
| 672 | Effect.gen(function* () { |
| 673 | const src = fakeResource("Test.Bucket", "Buck"); |
| 674 | // @ts-expect-error |
| 675 | const name = Output.of(src).name; |
| 676 | const expr = Output.interpolate`s3://${name}/key`; |
| 677 | const result = yield* Output.evaluate(expr, { Buck: { name: "my-bucket" } }); |
| 678 | expect(result).toBe("s3://my-bucket/key"); |
| 679 | }), |
| 680 | ), |
| 681 | ); |
| 682 | |
| 683 | it.effect("renders nullish args as empty strings", () => |
| 684 | provideState( |
| 685 | Effect.gen(function* () { |
| 686 | const expr = Output.interpolate`a${Output.literal(null)}b${Output.literal(undefined)}c`; |
| 687 | expect(yield* Output.evaluate(expr, {})).toBe("abc"); |
| 688 | }), |
| 689 | ), |
| 690 | ); |
| 691 | |
| 692 | it("derives its binding id from the template and arguments only", () => { |
| 693 | // The runtime bundler reprints function source, so a binding id that |
| 694 | // embeds the mapper's source differs between deploy and runtime. |
| 695 | const src = fakeResource("Test.Service", "Web"); |
| 696 | // @ts-expect-error |
| 697 | const service = Output.of(src).serviceName; |
| 698 | const expr = Output.interpolate`http://${service}:${Output.literal(80)}`; |
| 699 | expect(String(expr[Symbol.for("nodejs.util.inspect.custom")]())).toBe( |
| 700 | "interpolate(http://${Web.serviceName}:${80})", |
| 701 | ); |
| 702 | }); |
| 703 | }); |
| 704 | |
| 705 | describe("Output coercion guard", { tags: ["unit", "local"] }, () => { |
| 706 | // These guard against the long-standing footgun where coercing an |
| 707 | // unresolved Output silently produced a placeholder — the inspect |
| 708 | // string for string-hints, or NaN for number-hints. Both let bogus |
| 709 | // values flow into resource props and ultimately into the cloud. |
| 710 | |
| 711 | it("throws on template-literal interpolation of a raw Output", () => { |
| 712 | const src = fakeResource("Test.Bucket", "Buck"); |
| 713 | // @ts-expect-error — synthetic prop access |
| 714 | const name = Output.of(src).name; |
| 715 | expect(() => `${name}`).toThrow(/Output\.interpolate/); |
| 716 | }); |
| 717 | |
| 718 | it("throws on string concatenation with a raw Output", () => { |
| 719 | const src = fakeResource("Test.Bucket", "Buck"); |
| 720 | // @ts-expect-error — synthetic prop access |
| 721 | const name = Output.of(src).name; |
| 722 | expect(() => "s3://" + name).toThrow(/Output\.interpolate/); |
| 723 | }); |
| 724 | |
| 725 | it("throws on number coercion of a raw Output", () => { |
| 726 | const src = fakeResource("Test.Bucket", "Buck"); |
| 727 | // @ts-expect-error — synthetic prop access |
| 728 | const name = Output.of(src).name; |
| 729 | expect(() => +name).toThrow(/Output\.(interpolate|map)/); |
| 730 | }); |
| 731 | |
| 732 | it("throws on arithmetic with a raw Output", () => { |
| 733 | const src = fakeResource("Test.Bucket", "Buck"); |
| 734 | // @ts-expect-error — synthetic prop access |
| 735 | const name = Output.of(src).name; |
| 736 | expect(() => (name as unknown as number) * 2).toThrow(/Output\.(interpolate|map)/); |
| 737 | }); |
| 738 | }); |
| 739 | |
| 740 | describe( |
| 741 | "Redacted stack-output serialization (regression #598)", |
| 742 | { tags: ["unit", "local"] }, |
| 743 | () => { |
| 744 | // A resource whose attribute is a `Redacted<string>` — e.g. `Random.text`. |
| 745 | // When such an attribute flows |
| 746 | // into a Stack output it is JSON-serialized for persistence to state / |
| 747 | // Doppler / GH secrets. `JSON.stringify(Redacted)` returns the literal |
| 748 | // string "<redacted>", so a publisher reading the output would send |
| 749 | // `Bearer <redacted>` instead of the real token. The fix is to unwrap with |
| 750 | // `Output.map(Redacted.value)` before returning it from the stack. |
| 751 | const SECRET = "1486c434bd35732a185d1712c587ddfafd9e1c8d7a94fb15cf6ece51128"; |
| 752 | const redactedResource = () => { |
| 753 | const src = fakeResource<"Alchemy.Random", { text: Redacted.Redacted<string> }>( |
| 754 | "Alchemy.Random", |
| 755 | "AuthTokenValue", |
| 756 | ); |
| 757 | return (Output.of(src) as any).text as Output.Output<Redacted.Redacted<string>>; |
| 758 | }; |
| 759 | const env = { AuthTokenValue: { text: Redacted.make(SECRET) } }; |
| 760 | |
| 761 | it.effect('a raw Redacted output serializes to the literal "<redacted>" (the bug)', () => |
| 762 | provideState( |
| 763 | Effect.gen(function* () { |
| 764 | const result = yield* Output.evaluate(redactedResource(), env); |
| 765 | // The evaluated value is still a Redacted, and persisting it as a |
| 766 | // stack output (JSON) loses the real token. |
| 767 | expect(Redacted.isRedacted(result)).toBe(true); |
| 768 | expect(JSON.stringify({ authToken: result })).toBe('{"authToken":"<redacted>"}'); |
| 769 | }), |
| 770 | ), |
| 771 | ); |
| 772 | |
| 773 | it.effect("Output.map(Redacted.value) emits the real token (the fix)", () => |
| 774 | provideState( |
| 775 | Effect.gen(function* () { |
| 776 | const expr = redactedResource().pipe(Output.map(Redacted.value)); |
| 777 | const result = yield* Output.evaluate(expr, env); |
| 778 | expect(result).toBe(SECRET); |
| 779 | expect(JSON.stringify({ authToken: result })).toBe(`{"authToken":"${SECRET}"}`); |
| 780 | }), |
| 781 | ), |
| 782 | ); |
| 783 | }, |
| 784 | ); |
| 785 | |
| 786 | describe("Output.isOutput / isExpr", { tags: ["unit", "local"] }, () => { |
| 787 | it("identifies Output expressions", () => { |
| 788 | expect(Output.isOutput(Output.literal(1))).toBe(true); |
| 789 | expect(Output.isOutput(Output.all(Output.literal(1)))).toBe(true); |
| 790 | expect(Output.isExpr(Output.literal(1))).toBe(true); |
| 791 | }); |
| 792 | |
| 793 | it("rejects non-Output values", () => { |
| 794 | expect(Output.isOutput(1)).toBeFalsy(); |
| 795 | expect(Output.isOutput("x")).toBeFalsy(); |
| 796 | expect(Output.isOutput(null)).toBeFalsy(); |
| 797 | expect(Output.isOutput(undefined)).toBeFalsy(); |
| 798 | expect(Output.isOutput({})).toBeFalsy(); |
| 799 | expect(Output.isOutput([])).toBeFalsy(); |
| 800 | }); |
| 801 | }); |
| 802 | |
| 803 | describe("Output.asOutput", { tags: ["unit", "local"] }, () => { |
| 804 | it.effect("wraps a plain value as a literal Output", () => |
| 805 | provideState( |
| 806 | Effect.gen(function* () { |
| 807 | const o = Output.asOutput("foo"); |
| 808 | expect(Output.isOutput(o)).toBe(true); |
| 809 | expect(yield* Output.evaluate(o, {})).toBe("foo"); |
| 810 | }), |
| 811 | ), |
| 812 | ); |
| 813 | |
| 814 | it.effect("wraps an Effect as an EffectExpr", () => |
| 815 | provideState( |
| 816 | Effect.gen(function* () { |
| 817 | const o = Output.asOutput(Effect.succeed(123)); |
| 818 | expect(Output.isOutput(o)).toBe(true); |
| 819 | expect(yield* Output.evaluate(o, {})).toBe(123); |
| 820 | }), |
| 821 | ), |
| 822 | ); |
| 823 | |
| 824 | it("returns the same Output if already an Output", () => { |
| 825 | const o = Output.literal("x"); |
| 826 | expect(Output.asOutput(o)).toBe(o); |
| 827 | }); |
| 828 | }); |
| 829 | |
| 830 | describe("Output.upstream / hasOutputs / resolveUpstream", { tags: ["unit", "local"] }, () => { |
| 831 | it("returns upstream resources from a ResourceExpr", () => { |
| 832 | const src = fakeResource("Test.A", "FQN-A"); |
| 833 | const expr = Output.of(src); |
| 834 | const up = Output.upstream(expr); |
| 835 | expect(Object.keys(up)).toEqual(["FQN-A"]); |
| 836 | }); |
| 837 | |
| 838 | it("returns upstream resources from a PropExpr", () => { |
| 839 | const src = fakeResource("Test.A", "FQN-A"); |
| 840 | const expr = (Output.of(src) as any).foo; |
| 841 | expect(Object.keys(Output.upstream(expr))).toEqual(["FQN-A"]); |
| 842 | }); |
| 843 | |
| 844 | it("merges upstream resources from AllExpr", () => { |
| 845 | const a = fakeResource("Test.A", "A"); |
| 846 | const b = fakeResource("Test.B", "B"); |
| 847 | const expr = Output.all(Output.of(a), Output.of(b)); |
| 848 | expect(Object.keys(Output.upstream(expr)).sort()).toEqual(["A", "B"]); |
| 849 | }); |
| 850 | |
| 851 | it("returns empty upstream for literals", () => { |
| 852 | expect(Output.upstream(Output.literal(1))).toEqual({}); |
| 853 | }); |
| 854 | |
| 855 | it("treats a raw Resource passed directly as an upstream dependency", () => { |
| 856 | const src = fakeResource("Test.A", "RawA"); |
| 857 | expect(Object.keys(Output.upstream(src as any))).toEqual(["RawA"]); |
| 858 | }); |
| 859 | |
| 860 | it("upstreamAny detects a raw Resource passed directly as a prop value", () => { |
| 861 | const a = fakeResource("Test.A", "FQN-A"); |
| 862 | const b = fakeResource("Test.B", "FQN-B"); |
| 863 | const props = { image: a, network: b }; |
| 864 | expect(Object.keys(Output.upstreamAny(props)).sort()).toEqual(["FQN-A", "FQN-B"]); |
| 865 | }); |
| 866 | |
| 867 | it("upstreamAny detects raw Resources nested in arrays/objects", () => { |
| 868 | const a = fakeResource("Test.A", "FQN-A"); |
| 869 | const b = fakeResource("Test.B", "FQN-B"); |
| 870 | const props = { volumes: [{ source: a }], env: { ref: b } }; |
| 871 | expect(Object.keys(Output.upstreamAny(props)).sort()).toEqual(["FQN-A", "FQN-B"]); |
| 872 | }); |
| 873 | |
| 874 | it("upstreamAny detects raw Resource at the top level", () => { |
| 875 | const a = fakeResource("Test.A", "Top"); |
| 876 | expect(Object.keys(Output.upstreamAny(a))).toEqual(["Top"]); |
| 877 | }); |
| 878 | |
| 879 | it("resolveUpstream picks up raw Resources alongside Output expressions", () => { |
| 880 | const a = fakeResource("Test.A", "A1"); |
| 881 | const b = fakeResource("Test.B", "B1"); |
| 882 | const result = Output.resolveUpstream({ raw: a, via: Output.of(b) }); |
| 883 | expect(Object.keys(result).sort()).toEqual(["A1", "B1"]); |
| 884 | }); |
| 885 | |
| 886 | it("hasOutputs is true when an object contains an Output referencing a resource", () => { |
| 887 | const src = fakeResource("Test.A", "X"); |
| 888 | expect(Output.hasOutputs({ k: Output.of(src) })).toBe(true); |
| 889 | }); |
| 890 | |
| 891 | it("hasOutputs is false for plain values", () => { |
| 892 | expect(Output.hasOutputs({ k: 1, b: "x" })).toBe(false); |
| 893 | expect(Output.hasOutputs([1, 2, 3])).toBe(false); |
| 894 | }); |
| 895 | |
| 896 | it("resolveUpstream walks arrays and objects to gather resources", () => { |
| 897 | const a = fakeResource("Test.A", "RA"); |
| 898 | const b = fakeResource("Test.B", "RB"); |
| 899 | const result = Output.resolveUpstream({ |
| 900 | arr: [Output.of(a)], |
| 901 | nested: { prop: Output.of(b) }, |
| 902 | scalar: 1, |
| 903 | }); |
| 904 | expect(Object.keys(result).sort()).toEqual(["RA", "RB"]); |
| 905 | }); |
| 906 | }); |
| 907 | |
| 908 | // effect ≥4.0.0-beta.103's Context is self-referential (cacheRoot points back |
| 909 | // at itself), which sent the prop walkers into unbounded recursion during |
| 910 | // `Plan.make` of the Cloudflare state-store bootstrap stack (#1082). The rule |
| 911 | // pinned here: Resources/Outputs are dependencies, plain data (arrays, plain |
| 912 | // objects) is traversed to find them, and every other value — ANY class |
| 913 | // instance, effect's or otherwise — is a leaf. Plus WeakSet cycle guards so |
| 914 | // cyclic plain data terminates. |
| 915 | describe( |
| 916 | "upstream walkers: cycles and non-plain leaves (#1082)", |
| 917 | { tags: ["unit", "local"] }, |
| 918 | () => { |
| 919 | it("upstreamAny terminates on a cyclic plain object and still finds resources", () => { |
| 920 | const a = fakeResource("Test.A", "CY-A"); |
| 921 | const cyclic: any = { name: "cycle" }; |
| 922 | cyclic.self = cyclic; |
| 923 | const props = { config: cyclic, dep: Output.of(a) }; |
| 924 | expect(Object.keys(Output.upstreamAny(props))).toEqual(["CY-A"]); |
| 925 | }); |
| 926 | |
| 927 | it("resolveUpstream terminates on mutually-cyclic objects and arrays", () => { |
| 928 | const x: any = { tag: "x" }; |
| 929 | const y: any = { tag: "y", x }; |
| 930 | x.y = y; |
| 931 | const arr: any[] = [x]; |
| 932 | x.arr = arr; |
| 933 | expect(Output.resolveUpstream({ x, y, arr })).toEqual({}); |
| 934 | }); |
| 935 | |
| 936 | it("a shared (diamond) sub-object still contributes its resources", () => { |
| 937 | const a = fakeResource("Test.A", "DI-A"); |
| 938 | const shared = { dep: Output.of(a) }; |
| 939 | const result = Output.upstreamAny({ left: shared, right: shared }); |
| 940 | expect(Object.keys(result)).toEqual(["DI-A"]); |
| 941 | }); |
| 942 | |
| 943 | it("treats Effect, Layer, and Context values as leaves", () => { |
| 944 | const effect = Effect.succeed(1); |
| 945 | const layer = Layer.succeed(Stage, "test"); |
| 946 | const context = Context.empty(); |
| 947 | expect(Output.upstreamAny({ effect, layer, context })).toEqual({}); |
| 948 | expect(Output.resolveUpstream({ effect, layer, context })).toEqual({}); |
| 949 | expect(Output.hasOutputs({ effect, layer, context })).toBe(false); |
| 950 | }); |
| 951 | |
| 952 | it("traverses null-prototype objects and treats functions as leaves", () => { |
| 953 | const a = fakeResource("Test.A", "NP-A"); |
| 954 | const nullProto = Object.assign(Object.create(null), { dep: Output.of(a) }); |
| 955 | expect(Object.keys(Output.upstreamAny({ nullProto }))).toEqual(["NP-A"]); |
| 956 | // A function in props is a leaf — a closure capturing a resource is not |
| 957 | // a declared dependency. |
| 958 | expect(Output.upstreamAny({ fn: () => a })).toEqual({}); |
| 959 | }); |
| 960 | |
| 961 | it("treats any class instance as a leaf (never walks its internals)", () => { |
| 962 | const a = fakeResource("Test.A", "CL-A"); |
| 963 | class SdkConfig { |
| 964 | // A dependency smuggled inside a foreign class instance is invisible |
| 965 | // by design — the engine can't evaluate or persist through it. |
| 966 | dep = Output.of(a); |
| 967 | date = new Date(0); |
| 968 | } |
| 969 | expect(Output.upstreamAny({ config: new SdkConfig() })).toEqual({}); |
| 970 | // ...but the same dependency in plain data right next to it is found. |
| 971 | expect( |
| 972 | Object.keys(Output.upstreamAny({ config: new SdkConfig(), dep: Output.of(a) })), |
| 973 | ).toEqual(["CL-A"]); |
| 974 | }); |
| 975 | |
| 976 | it("mimics a Worker `exports` entry (constructor Effect + services Context)", () => { |
| 977 | const a = fakeResource("Test.A", "EX-A"); |
| 978 | const props = { |
| 979 | name: "worker", |
| 980 | exports: { |
| 981 | Store: { kind: "durableObject", constructor: Effect.void, services: Context.empty() }, |
| 982 | }, |
| 983 | dep: Output.of(a), |
| 984 | }; |
| 985 | expect(Object.keys(Output.upstreamAny(props))).toEqual(["EX-A"]); |
| 986 | }); |
| 987 | |
| 988 | it.effect("evaluate passes Effect/Layer/Context through by identity", () => |
| 989 | provideState( |
| 990 | Effect.gen(function* () { |
| 991 | const effect = Effect.succeed(1); |
| 992 | const layer = Layer.succeed(Stage, "test"); |
| 993 | const context = Context.empty(); |
| 994 | const result = yield* Output.evaluate({ effect, layer, context }, {}); |
| 995 | expect(result.effect).toBe(effect); |
| 996 | expect(result.layer).toBe(layer); |
| 997 | expect(result.context).toBe(context); |
| 998 | }), |
| 999 | ), |
| 1000 | ); |
| 1001 | |
| 1002 | it.effect("evaluate passes Date/Redacted through by identity (prototype intact)", () => |
| 1003 | provideState( |
| 1004 | Effect.gen(function* () { |
| 1005 | const date = new Date(0); |
| 1006 | const secret = Redacted.make("hunter2"); |
| 1007 | const result = yield* Output.evaluate({ date, secret }, {}); |
| 1008 | expect(result.date).toBe(date); |
| 1009 | expect(result.secret).toBe(secret); |
| 1010 | }), |
| 1011 | ), |
| 1012 | ); |
| 1013 | |
| 1014 | it.effect("evaluate still resolves Config values (Configs are Effects)", () => |
| 1015 | provideState( |
| 1016 | Effect.gen(function* () { |
| 1017 | const result = yield* Output.evaluate({ value: Config.succeed("resolved") }, {}); |
| 1018 | expect(result.value).toBe("resolved"); |
| 1019 | }), |
| 1020 | ), |
| 1021 | ); |
| 1022 | |
| 1023 | it.effect("evaluate resolves outputs at every nesting depth of plain data", () => |
| 1024 | provideState( |
| 1025 | Effect.gen(function* () { |
| 1026 | const src = fakeResource("Test.A", "EV-A"); |
| 1027 | const out = (Output.of(src) as any).name; |
| 1028 | const result = yield* Output.evaluate( |
| 1029 | { |
| 1030 | layers: [{ config: { hosts: [{ url: out }, { url: "static" }] } }], |
| 1031 | matrix: [[out]], |
| 1032 | mixed: [1, "x", { deep: [out] }, null], |
| 1033 | nullProto: Object.assign(Object.create(null), { ref: out }), |
| 1034 | }, |
| 1035 | { "EV-A": { name: "resolved-name" } }, |
| 1036 | ); |
| 1037 | expect(result.layers[0].config.hosts[0].url).toBe("resolved-name"); |
| 1038 | expect(result.layers[0].config.hosts[1].url).toBe("static"); |
| 1039 | expect(result.matrix[0][0]).toBe("resolved-name"); |
| 1040 | expect(result.mixed).toEqual([1, "x", { deep: ["resolved-name"] }, null]); |
| 1041 | expect(result.nullProto.ref).toBe("resolved-name"); |
| 1042 | }), |
| 1043 | ), |
| 1044 | ); |
| 1045 | |
| 1046 | it.effect("evaluate cuts cyclic plain data but resolves siblings", () => |
| 1047 | provideState( |
| 1048 | Effect.gen(function* () { |
| 1049 | const src = fakeResource("Test.A", "CY-EV"); |
| 1050 | const out = (Output.of(src) as any).name; |
| 1051 | const cyclic: any = { tag: "c" }; |
| 1052 | cyclic.self = cyclic; |
| 1053 | const arr: any[] = [cyclic]; |
| 1054 | cyclic.arr = arr; |
| 1055 | const result = yield* Output.evaluate( |
| 1056 | { cyc: cyclic, url: out }, |
| 1057 | { "CY-EV": { name: "sibling" } }, |
| 1058 | ); |
| 1059 | expect(result.url).toBe("sibling"); |
| 1060 | expect(result.cyc.tag).toBe("c"); |
| 1061 | expect(result.cyc.self).toBeUndefined(); |
| 1062 | expect(result.cyc.arr[0]).toBeUndefined(); |
| 1063 | }), |
| 1064 | ), |
| 1065 | ); |
| 1066 | |
| 1067 | it.effect("evaluate preserves diamonds — shared objects evaluate in both positions", () => |
| 1068 | provideState( |
| 1069 | Effect.gen(function* () { |
| 1070 | const src = fakeResource("Test.A", "DI-EV"); |
| 1071 | const shared = { url: (Output.of(src) as any).name }; |
| 1072 | const result = yield* Output.evaluate( |
| 1073 | { left: shared, right: shared, list: [shared] }, |
| 1074 | { "DI-EV": { name: "diamond" } }, |
| 1075 | ); |
| 1076 | expect(result.left).toEqual({ url: "diamond" }); |
| 1077 | expect(result.right).toEqual({ url: "diamond" }); |
| 1078 | expect(result.list[0]).toEqual({ url: "diamond" }); |
| 1079 | }), |
| 1080 | ), |
| 1081 | ); |
| 1082 | }, |
| 1083 | ); |
| 1084 | |
| 1085 | describe("Output.toEnvKey / toUpper", { tags: ["unit", "local"] }, () => { |
| 1086 | it("uppercases strings", () => { |
| 1087 | expect(Output.toUpper("hello")).toBe("HELLO"); |
| 1088 | }); |
| 1089 | |
| 1090 | it("joins id + suffix and replaces dashes with underscores", () => { |
| 1091 | expect(Output.toEnvKey("my-bucket", "name")).toBe("MY_BUCKET_NAME"); |
| 1092 | expect(Output.toEnvKey("svc", "api-key")).toBe("SVC_API_KEY"); |
| 1093 | }); |
| 1094 | }); |