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 { afterAll, assert, describe, expect, layer } from "@effect/vitest" |
| 2 | import { Clock, Context, Effect, Layer } from "effect" |
| 3 | import { TestClock } from "effect/testing" |
| 4 | |
| 5 | describe("buildPerTest", { concurrent: false }, () => { |
| 6 | let sharedBuilds = 0 |
| 7 | let nextPerTestId = 0 |
| 8 | const releasedPerTestIds: Array<number> = [] |
| 9 | |
| 10 | class Shared extends Context.Service<Shared, "shared">()("BuildPerTestShared") { |
| 11 | static readonly layer = Layer.effect(Shared)( |
| 12 | Effect.sync(() => { |
| 13 | sharedBuilds++ |
| 14 | return "shared" as const |
| 15 | }) |
| 16 | ) |
| 17 | } |
| 18 | |
| 19 | class PerTest extends Context.Service<PerTest, { readonly id: number }>()("BuildPerTestPerTest") { |
| 20 | static readonly layer = Layer.effect(PerTest)( |
| 21 | Effect.flatMap(Shared, () => |
| 22 | Effect.acquireRelease( |
| 23 | Effect.sync(() => ({ id: ++nextPerTestId })), |
| 24 | ({ id }) => |
| 25 | Effect.sync(() => { |
| 26 | releasedPerTestIds.push(id) |
| 27 | }) |
| 28 | )) |
| 29 | ) |
| 30 | } |
| 31 | |
| 32 | layer(Shared.layer)("shared", (it) => { |
| 33 | it.layer(PerTest.layer, { buildPerTest: true })("per test", (it) => { |
| 34 | it.effect("builds the layer for the test", () => |
| 35 | Effect.gen(function*() { |
| 36 | const perTest = yield* PerTest |
| 37 | |
| 38 | assert.strictEqual(perTest.id, 1) |
| 39 | assert.deepStrictEqual(releasedPerTestIds, []) |
| 40 | })) |
| 41 | |
| 42 | it.effect("rebuilds the layer once the previous test released it", () => |
| 43 | Effect.gen(function*() { |
| 44 | const perTest = yield* PerTest |
| 45 | |
| 46 | assert.strictEqual(perTest.id, 2) |
| 47 | assert.deepStrictEqual(releasedPerTestIds, [1]) |
| 48 | })) |
| 49 | |
| 50 | it.layer(Layer.empty)((it) => { |
| 51 | it.effect.each([3, 4])("rebuilds the layer for nested test %s", (id) => |
| 52 | Effect.gen(function*() { |
| 53 | const perTest = yield* PerTest |
| 54 | |
| 55 | assert.strictEqual(perTest.id, id) |
| 56 | })) |
| 57 | }) |
| 58 | }) |
| 59 | |
| 60 | afterAll(() => { |
| 61 | expect(sharedBuilds).toEqual(1) |
| 62 | expect(releasedPerTestIds).toEqual([1, 2, 3, 4]) |
| 63 | }) |
| 64 | }) |
| 65 | }) |
| 66 | |
| 67 | layer(Layer.empty, { buildPerTest: true })("top-level buildPerTest", (it) => { |
| 68 | it.effect.each([1, 2])("provides new test services for test %s", () => |
| 69 | Effect.gen(function*() { |
| 70 | assert.strictEqual(yield* Clock.currentTimeMillis, 0) |
| 71 | yield* TestClock.adjust("1 second") |
| 72 | })) |
| 73 | }) |