| 1 | import { afterEach, assert, describe, it } from "@effect/vitest" |
| 2 | import { Arbitrary, Effect, Schema } from "effect" |
| 3 | |
| 4 | describe("Arbitrary.configureGlobal", { concurrent: false }, () => { |
| 5 | afterEach(() => Arbitrary.configureGlobal({})) |
| 6 | |
| 7 | it.effect("uses global defaults and lets explicit options override individual fields", () => |
| 8 | Effect.gen(function*() { |
| 9 | Arbitrary.configureGlobal({ check: { runs: 1000 }, sample: { count: 3 } }) |
| 10 | const arbitrary = Arbitrary.Constant(true) |
| 11 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => true), { |
| 12 | _tag: "Passed", |
| 13 | runs: 1000, |
| 14 | discards: 0 |
| 15 | }) |
| 16 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => true, { runs: 2 }), { |
| 17 | _tag: "Passed", |
| 18 | runs: 2, |
| 19 | discards: 0 |
| 20 | }) |
| 21 | assert.deepStrictEqual(yield* Arbitrary.sampleEffect(arbitrary), [true, true, true]) |
| 22 | assert.deepStrictEqual(yield* Arbitrary.sampleEffect(arbitrary, { count: 0 }), []) |
| 23 | |
| 24 | Arbitrary.configureGlobal({ check: { runs: 2, maxDiscards: 0, seed: "global" } }) |
| 25 | const discarded = Arbitrary.filter(arbitrary, () => false) |
| 26 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(discarded, () => true, { runs: 1 }), { |
| 27 | _tag: "Exhausted", |
| 28 | runs: 0, |
| 29 | discards: 1, |
| 30 | seed: "global" |
| 31 | }) |
| 32 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => true, { runs: undefined }), { |
| 33 | _tag: "Passed", |
| 34 | runs: 2, |
| 35 | discards: 0 |
| 36 | }) |
| 37 | })) |
| 38 | |
| 39 | it.effect("resolves defaults at execution and keeps an active check unchanged", () => |
| 40 | Effect.gen(function*() { |
| 41 | const arbitrary = Arbitrary.Constant(true) |
| 42 | const check = Arbitrary.checkEffect(arbitrary, () => { |
| 43 | Arbitrary.configureGlobal({ check: { runs: 1 } }) |
| 44 | return true |
| 45 | }) |
| 46 | const sample = Arbitrary.sampleEffect(arbitrary) |
| 47 | Arbitrary.configureGlobal({ check: { runs: 3 }, sample: { count: 2 } }) |
| 48 | assert.deepStrictEqual(yield* sample, [true, true]) |
| 49 | assert.deepStrictEqual(yield* check, { _tag: "Passed", runs: 3, discards: 0 }) |
| 50 | assert.deepStrictEqual(yield* check, { _tag: "Passed", runs: 1, discards: 0 }) |
| 51 | })) |
| 52 | |
| 53 | it.effect("replaces configuration, copies supplied defaults, and restores built-in defaults", () => |
| 54 | Effect.gen(function*() { |
| 55 | const configuration = { check: { runs: 3 }, sample: { count: 2 } } |
| 56 | Arbitrary.configureGlobal(configuration) |
| 57 | configuration.check.runs = 8 |
| 58 | configuration.sample.count = 8 |
| 59 | const arbitrary = Arbitrary.Constant(true) |
| 60 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => true), { |
| 61 | _tag: "Passed", |
| 62 | runs: 3, |
| 63 | discards: 0 |
| 64 | }) |
| 65 | assert.strictEqual((yield* Arbitrary.sampleEffect(arbitrary)).length, 2) |
| 66 | Arbitrary.configureGlobal({ check: { runs: 4 } }) |
| 67 | assert.strictEqual((yield* Arbitrary.sampleEffect(arbitrary)).length, 10) |
| 68 | Arbitrary.configureGlobal({}) |
| 69 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => true), { |
| 70 | _tag: "Passed", |
| 71 | runs: 100, |
| 72 | discards: 0 |
| 73 | }) |
| 74 | })) |
| 75 | |
| 76 | it.effect("applies sample size and seed defaults while preserving undefined fallbacks", () => |
| 77 | Effect.gen(function*() { |
| 78 | const arbitrary = Arbitrary.schema(Schema.Array(Schema.Boolean)) |
| 79 | const options = { count: 5, size: 4, seed: "sample" } |
| 80 | const expected = yield* Arbitrary.sampleEffect(arbitrary, options) |
| 81 | Arbitrary.configureGlobal({ sample: options }) |
| 82 | assert.deepStrictEqual(yield* Arbitrary.sampleEffect(arbitrary, { seed: undefined }), expected) |
| 83 | assert.deepStrictEqual(yield* Arbitrary.sampleEffect(arbitrary, { size: 0 }), [[], [], [], [], []]) |
| 84 | Arbitrary.configureGlobal({ sample: { count: 1, maxDiscards: 0, seed: 0 } }) |
| 85 | const exit = yield* Arbitrary.sampleEffect(Arbitrary.filter(Arbitrary.Constant(true), () => false)).pipe( |
| 86 | Effect.flip |
| 87 | ) |
| 88 | assert.deepStrictEqual(exit, { _tag: "SampleError", generated: 0, discards: 1, seed: 0 }) |
| 89 | })) |
| 90 | |
| 91 | it.effect("applies checking defaults without changing replay", () => |
| 92 | Effect.gen(function*() { |
| 93 | const arbitrary = Arbitrary.schema(Schema.Array(Schema.Boolean)) |
| 94 | const options = { runs: 1, size: 10, maxShrinks: 0, seed: "check" } |
| 95 | const expected = yield* Arbitrary.checkEffect(arbitrary, () => false, options) |
| 96 | Arbitrary.configureGlobal({ check: options }) |
| 97 | assert.deepStrictEqual(yield* Arbitrary.checkEffect(arbitrary, () => false), expected) |
| 98 | assert.strictEqual(expected._tag, "Falsified") |
| 99 | if (expected._tag === "Falsified") { |
| 100 | Arbitrary.configureGlobal({ check: { runs: -1, size: -1, maxDiscards: -1, maxShrinks: -1 } }) |
| 101 | const replayed = yield* Arbitrary.checkEffect(arbitrary, () => false, { replay: expected.replay }) |
| 102 | assert.deepStrictEqual(replayed, expected) |
| 103 | } |
| 104 | })) |
| 105 | }) |