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, 04:45 UTC
| 1 | import { describe, expect, it } from "@alchemy.run/test"; |
| 2 | import * as Cause from "effect/Cause"; |
| 3 | import * as Context from "effect/Context"; |
| 4 | import * as Effect from "effect/Effect"; |
| 5 | import * as Exit from "effect/Exit"; |
| 6 | import * as Schema from "effect/Schema"; |
| 7 | import * as SchemaGetter from "effect/SchemaGetter"; |
| 8 | import { Workflow } from "effect/workflow"; |
| 9 | import { decodeResult, encodeResult } from "@/WorkflowEncoding.ts"; |
| 10 | |
| 11 | const workflow = <Success extends Schema.Top, Error extends Schema.Top = Schema.Never>( |
| 12 | success: Success, |
| 13 | error?: Error, |
| 14 | ) => |
| 15 | Workflow.make("Encoding", { |
| 16 | payload: {}, |
| 17 | success, |
| 18 | error: error ?? Schema.Never, |
| 19 | idempotencyKey: () => "test", |
| 20 | }); |
| 21 | |
| 22 | class Denied extends Schema.TaggedError<Denied>()("Denied", { |
| 23 | code: Schema.NumberFromString, |
| 24 | }) {} |
| 25 | |
| 26 | class Offset extends Context.Service<Offset, number>()("WorkflowEncoding/Offset") {} |
| 27 | |
| 28 | const transformed = Schema.NumberFromString.pipe( |
| 29 | Schema.decodeTo(Schema.Number, { |
| 30 | decode: SchemaGetter.transformEffect((value: number) => |
| 31 | Offset.pipe(Effect.map((offset) => value + offset)), |
| 32 | ), |
| 33 | encode: SchemaGetter.transformEffect((value: number) => |
| 34 | Offset.pipe(Effect.map((offset) => value - offset)), |
| 35 | ), |
| 36 | }), |
| 37 | ); |
| 38 | |
| 39 | describe("Workflow result encoding", { tags: ["unit", "local"] }, () => { |
| 40 | it.effect("preserves the wire format and returns fresh values on repeated decoding", () => |
| 41 | Effect.gen(function* () { |
| 42 | const definition = workflow(Schema.Struct({ items: Schema.Array(Schema.String) })); |
| 43 | const exit = Exit.succeed({ items: ["first"] }); |
| 44 | const codec = Schema.toCodecJson( |
| 45 | Schema.Exit(definition.successSchema, definition.errorSchema, Schema.Defect()), |
| 46 | ); |
| 47 | const expected = JSON.stringify(yield* Schema.encodeEffect(codec)(exit)); |
| 48 | for (let i = 0; i < 3; i++) { |
| 49 | const text = yield* encodeResult(definition, exit); |
| 50 | expect(text).toBe(expected); |
| 51 | const first = yield* decodeResult(definition, text); |
| 52 | const second = yield* decodeResult(definition, text); |
| 53 | expect(first.exit).toEqual(exit); |
| 54 | expect(second.exit).toEqual(exit); |
| 55 | expect(first).not.toBe(second); |
| 56 | if (Exit.isSuccess(first.exit) && Exit.isSuccess(second.exit)) { |
| 57 | expect(first.exit.value).not.toBe(second.exit.value); |
| 58 | } |
| 59 | } |
| 60 | }), |
| 61 | ); |
| 62 | |
| 63 | it.effect("preserves transformed successes and typed failures with a warm codec", () => |
| 64 | Effect.gen(function* () { |
| 65 | const definition = workflow( |
| 66 | Schema.Struct({ count: Schema.NumberFromString, date: Schema.DateFromString }), |
| 67 | Denied, |
| 68 | ); |
| 69 | const value = { count: 42, date: new Date("2026-01-02T03:04:05.000Z") }; |
| 70 | for (let i = 0; i < 3; i++) { |
| 71 | const success = yield* encodeResult(definition, Exit.succeed(value)); |
| 72 | expect(success).toContain('"count":"42"'); |
| 73 | expect((yield* decodeResult(definition, success)).exit).toEqual(Exit.succeed(value)); |
| 74 | const failure = yield* encodeResult(definition, Exit.fail(new Denied({ code: 7 }))); |
| 75 | expect(failure).toContain('"code":"7"'); |
| 76 | const decoded = yield* decodeResult(definition, failure); |
| 77 | expect(Exit.isFailure(decoded.exit)).toBe(true); |
| 78 | if (Exit.isFailure(decoded.exit)) { |
| 79 | const error = Cause.squash(decoded.exit.cause); |
| 80 | expect(error).toBeInstanceOf(Denied); |
| 81 | expect(error).toMatchObject({ _tag: "Denied", code: 7 }); |
| 82 | } |
| 83 | } |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | it.effect("preserves defects and interruption after caching a successful result", () => |
| 88 | Effect.gen(function* () { |
| 89 | const definition = workflow(Schema.String); |
| 90 | yield* encodeResult(definition, Exit.succeed("warm")); |
| 91 | for (let i = 0; i < 3; i++) { |
| 92 | const defect = yield* encodeResult(definition, Exit.die(new Error("broken"))); |
| 93 | const decoded = yield* decodeResult(definition, defect); |
| 94 | expect(Exit.isFailure(decoded.exit)).toBe(true); |
| 95 | if (Exit.isFailure(decoded.exit)) { |
| 96 | expect(Cause.hasDies(decoded.exit.cause)).toBe(true); |
| 97 | expect(Cause.squash(decoded.exit.cause)).toMatchObject({ message: "broken" }); |
| 98 | } |
| 99 | const interrupted = yield* encodeResult(definition, Exit.interrupt()); |
| 100 | const decodedInterrupt = yield* decodeResult(definition, interrupted); |
| 101 | expect(Exit.isFailure(decodedInterrupt.exit)).toBe(true); |
| 102 | if (Exit.isFailure(decodedInterrupt.exit)) { |
| 103 | expect(Cause.hasInterrupts(decodedInterrupt.exit.cause)).toBe(true); |
| 104 | } |
| 105 | } |
| 106 | }), |
| 107 | ); |
| 108 | |
| 109 | it.effect("keeps same-name definitions with different schema pairs independent", () => |
| 110 | Effect.gen(function* () { |
| 111 | const text = workflow(Schema.String, Schema.String); |
| 112 | const number = workflow(Schema.NumberFromString, Schema.String); |
| 113 | const numericError = workflow(Schema.String, Schema.NumberFromString); |
| 114 | for (let i = 0; i < 3; i++) { |
| 115 | expect( |
| 116 | (yield* decodeResult(text, yield* encodeResult(text, Exit.succeed("42")))).exit, |
| 117 | ).toEqual(Exit.succeed("42")); |
| 118 | expect( |
| 119 | (yield* decodeResult(number, yield* encodeResult(number, Exit.succeed(42)))).exit, |
| 120 | ).toEqual(Exit.succeed(42)); |
| 121 | const encoded = yield* encodeResult(numericError, Exit.fail(7)); |
| 122 | expect((yield* decodeResult(numericError, encoded)).exit).toEqual(Exit.fail(7)); |
| 123 | } |
| 124 | }), |
| 125 | ); |
| 126 | |
| 127 | it.effect("uses replacement schema references on an existing workflow definition", () => |
| 128 | Effect.gen(function* () { |
| 129 | const definition = workflow(Schema.String, Schema.String); |
| 130 | yield* encodeResult(definition, Exit.succeed("warm")); |
| 131 | // Workflow fields are readonly in TypeScript, but runtime adapters can replace them. |
| 132 | Object.assign(definition, { |
| 133 | successSchema: Schema.NumberFromString, |
| 134 | errorSchema: Schema.NumberFromString, |
| 135 | }); |
| 136 | const success = yield* encodeResult(definition, Exit.succeed(42)); |
| 137 | expect((yield* decodeResult(definition, success)).exit).toEqual(Exit.succeed(42)); |
| 138 | const failure = yield* encodeResult(definition, Exit.fail(7)); |
| 139 | expect((yield* decodeResult(definition, failure)).exit).toEqual(Exit.fail(7)); |
| 140 | }), |
| 141 | ); |
| 142 | |
| 143 | it.effect("evaluates cached transformations in each call's current context", () => |
| 144 | Effect.gen(function* () { |
| 145 | const definition = workflow(transformed); |
| 146 | for (const offset of [1, 2, 10]) { |
| 147 | const text = yield* encodeResult(definition, Exit.succeed(42)).pipe( |
| 148 | Effect.provideService(Offset, offset), |
| 149 | ); |
| 150 | expect(text).toContain(`"value":"${42 - offset}"`); |
| 151 | const result = yield* decodeResult(definition, text).pipe( |
| 152 | Effect.provideService(Offset, offset), |
| 153 | ); |
| 154 | expect(result.exit).toEqual(Exit.succeed(42)); |
| 155 | } |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | it.effect("still rejects invalid values after warming the codec", () => |
| 160 | Effect.gen(function* () { |
| 161 | const definition = workflow(Schema.String); |
| 162 | const valid = yield* encodeResult(definition, Exit.succeed("warm")); |
| 163 | const encoded = yield* Effect.exit(encodeResult(definition, Exit.succeed(42))); |
| 164 | expect(Exit.isFailure(encoded)).toBe(true); |
| 165 | const invalid = valid.replace('"warm"', "42"); |
| 166 | const decoded = yield* Effect.exit(decodeResult(definition, invalid)); |
| 167 | expect(Exit.isFailure(decoded)).toBe(true); |
| 168 | }), |
| 169 | ); |
| 170 | }); |