diff --git a/packages/oracles/src/oracle-property.ts b/packages/oracles/src/oracle-property.ts index c050745..0b14748 100644 --- a/packages/oracles/src/oracle-property.ts +++ b/packages/oracles/src/oracle-property.ts @@ -620,7 +620,7 @@ export interface GeneratedHistories { runs: number /** One fixed-seed campaign per seed, beside the random campaign. */ fixedSeeds: ReadonlyArray - constructed?: ReadonlyArray> + constructed?: NoInfer>> /** * How many bases each constructed history runs in each campaign: the first * `bases` draws of the arbitrary's sample at the campaign's seed plus the @@ -637,7 +637,7 @@ export interface GeneratedHistories { * run to run. A whole number of at least 0; 0 when absent. */ drawnBases?: number - fixed?: ReadonlyArray> + fixed?: NoInfer>> } /** @@ -701,26 +701,26 @@ export interface OraclePropertyOptions { | EnumeratedHistories | GeneratedHistories /** A short, unique test name for an enumerated history. */ - label?: (history: THistory) => string + label?: (history: NoInfer) => string /** The plan of concrete actions; required when Plan is not History. */ - resolve?: (history: THistory) => TPlan - model: (plan: TPlan, trace: ReadonlyArray) => TExpected + resolve?: (history: NoInfer) => TPlan + model: (plan: NoInfer, trace: ReadonlyArray) => TExpected /** The states the history entered. Defaults to the trace's `reach` values. */ reached?: ( - plan: TPlan, + plan: NoInfer, trace: ReadonlyArray, - expected: TExpected, + expected: NoInfer, ) => Iterable driver: ( - plan: TPlan, + plan: NoInfer, observe: (checkpoint: string, value: unknown) => void, ) => Promise | void check: ( - plan: TPlan, + plan: NoInfer, observations: ReadonlyArray, - expected: TExpected, + expected: NoInfer, ) => Verdict - calibrations?: ReadonlyArray> + calibrations?: NoInfer>> /** * Opt each probe-bound state into direct forced controls. Entirely unbound * states retain search with warnings during the consumer migration. @@ -730,7 +730,7 @@ export interface OraclePropertyOptions { * Whether the history enters each probe-bound state, from the plan alone: * a prediction never sees the trace, so it cannot follow production. */ - predict?: (plan: TPlan) => Readonly> + predict?: (plan: NoInfer) => Readonly> /** * Each registered test's limit in milliseconds; 120 seconds by default, and * none at 0 or less. Each unit of a campaign stops shrinking at half of it. diff --git a/packages/oracles/tests/config-oracle.property.test.ts b/packages/oracles/tests/config-oracle.property.test.ts index 184accd..eec4dc3 100644 --- a/packages/oracles/tests/config-oracle.property.test.ts +++ b/packages/oracles/tests/config-oracle.property.test.ts @@ -404,7 +404,7 @@ oracleProperty>({ { name: selectionChange, enters: [selectionChange], - build: (generated): History => [ + build: (generated) => [ { do: `read`, reader: `replay requested` }, { do: `environment`, replay: `ORACLE`, calibration: `none` }, { do: `configure`, prefix: `ORACLE`, registry: false }, diff --git a/packages/oracles/tests/property-inference.test-d.ts b/packages/oracles/tests/property-inference.test-d.ts new file mode 100644 index 0000000..3a6809f --- /dev/null +++ b/packages/oracles/tests/property-inference.test-d.ts @@ -0,0 +1,432 @@ +import { expectTypeOf } from 'vitest' +import fc from 'fast-check' +import { oracleProperty, pass } from '../src/index.js' +import type { Observation } from '../src/index.js' + +type VariantHistory = + | { + kind: `a` + mode: `fast` | `safe` + mask: [boolean, boolean] + items: Array + } + | { + kind: `b` + count: number + pair: [`left` | `right`, [number, number]] + } + +const variantArbitrary: fc.Arbitrary = fc.oneof( + fc.record({ + kind: fc.constant(`a` as const), + mode: fc.constantFrom(`fast` as const, `safe` as const), + mask: fc.tuple(fc.boolean(), fc.boolean()), + items: fc.array(fc.integer()), + }), + fc.record({ + kind: fc.constant(`b` as const), + count: fc.integer(), + pair: fc.tuple( + fc.constantFrom(`left` as const, `right` as const), + fc.tuple(fc.integer(), fc.integer()), + ), + }), +) + +const law = { + statement: `type inference regression`, + authority: `oracleProperty type signature`, + limits: `compile-time only`, +} + +// Type-level regressions checked by `tsc --noEmit` (not executed at runtime). +export function checkOraclePropertyInference(): void { + // 1. Generated histories with discriminated unions and fixed-length tuples: + // `fixed[].history` and `constructed[].build` get contextual typing from + // `arbitrary` without interfering with `THistory` inference. + oracleProperty({ + id: `type.generated`, + law, + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + states: [`a-fast`, `b-left`], + constructed: [ + { + name: `a-fast`, + enters: [`a-fast`], + build: (base) => + base.kind === `a` + ? { ...base, mode: `fast`, mask: [true, false] } + : { + kind: `a`, + mode: `fast`, + mask: [true, false], + items: [base.count], + }, + }, + { + name: `b-left`, + enters: [`b-left`], + build: (_base) => ({ + kind: `b`, + count: 2, + pair: [`left`, [0, 1]], + }), + }, + { + name: `block-body`, + enters: [`a-fast`], + build: (base) => { + if (base.kind === `a`) { + return { ...base, mode: `safe`, mask: [false, true] } + } + return { kind: `b`, count: base.count, pair: [`right`, [1, 2]] } + }, + }, + ], + fixed: [ + { + name: `fixed-a`, + history: { + kind: `a`, + mode: `safe`, + mask: [false, true], + items: [1, 2], + }, + }, + { + name: `fixed-b`, + history: { + kind: `b`, + count: 3, + pair: [`right`, [4, 5]], + }, + }, + ], + }, + label: (history) => { + expectTypeOf(history).toEqualTypeOf() + return history.kind + }, + model: (plan, trace) => { + expectTypeOf(plan).toEqualTypeOf() + expectTypeOf(trace).toEqualTypeOf>() + return plan.kind === `a` + ? { tag: plan.mode, flags: plan.mask } + : { tag: plan.pair[0], count: plan.count } + }, + reached: (plan, _trace, expected) => { + expectTypeOf(plan).toEqualTypeOf() + expectTypeOf(expected.tag).toEqualTypeOf< + `fast` | `safe` | `left` | `right` + >() + expectTypeOf(expected.flags).toEqualTypeOf< + [boolean, boolean] | undefined + >() + expectTypeOf(expected.count).toEqualTypeOf() + return [plan.kind, expected.tag] + }, + driver: (plan, observe) => { + expectTypeOf(plan).toEqualTypeOf() + observe(`kind`, plan.kind) + }, + check: (plan, _observations, expected) => { + expectTypeOf(plan).toEqualTypeOf() + expectTypeOf(expected.tag).toEqualTypeOf< + `fast` | `safe` | `left` | `right` + >() + expectTypeOf(expected.flags).toEqualTypeOf< + [boolean, boolean] | undefined + >() + expectTypeOf(expected.count).toEqualTypeOf() + return pass + }, + predict: (plan) => { + expectTypeOf(plan).toEqualTypeOf() + return { 'a-fast': plan.kind === `a` && plan.mode === `fast` } + }, + calibrations: [ + { + name: `corrupt-calibration`, + law: `type.generated`, + corrupt: (trace, plan) => { + expectTypeOf(plan).toEqualTypeOf() + return trace + }, + }, + { + name: `driver-calibration`, + law: `type.generated`, + driver: (plan, observe) => { + expectTypeOf(plan).toEqualTypeOf() + observe(`kind`, plan.kind) + }, + }, + ], + }) + + // 2. With `resolve`: `THistory` comes from `histories.arbitrary`, `TPlan` + // comes from `resolve`, and downstream callbacks + `calibrations` receive + // `TPlan` without polluting inference. + oracleProperty({ + id: `type.resolved`, + law, + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + constructed: [ + { + name: `c`, + enters: [], + build: (base) => + base.kind === `a` + ? { ...base, mode: `fast`, mask: [true, true] } + : { ...base, pair: [`left`, [0, 0]] }, + }, + ], + fixed: [ + { + name: `f`, + history: { kind: `b`, count: 0, pair: [`left`, [1, 2]] }, + }, + ], + }, + resolve: (history) => { + expectTypeOf(history).toEqualTypeOf() + const bounds: [number, number] = + history.kind === `a` ? [0, history.items.length] : history.pair[1] + return { + resolvedKind: history.kind, + bounds, + } + }, + model: (plan) => { + expectTypeOf(plan).toEqualTypeOf<{ + resolvedKind: `a` | `b` + bounds: [number, number] + }>() + return plan.bounds[1] - plan.bounds[0] + }, + reached: (plan, _trace, expected) => { + expectTypeOf(plan.resolvedKind).toEqualTypeOf<`a` | `b`>() + expectTypeOf(expected).toEqualTypeOf() + return [plan.resolvedKind] + }, + driver: (plan, observe) => { + expectTypeOf(plan.bounds).toEqualTypeOf<[number, number]>() + observe(`kind`, plan.resolvedKind) + }, + check: (plan, _trace, expected) => { + expectTypeOf(plan.bounds).toEqualTypeOf<[number, number]>() + expectTypeOf(expected).toEqualTypeOf() + return pass + }, + predict: (plan) => { + expectTypeOf(plan.resolvedKind).toEqualTypeOf<`a` | `b`>() + return { ok: plan.resolvedKind === `a` } + }, + calibrations: [ + { + name: `corrupt-resolved`, + law: `type.resolved`, + corrupt: (trace, plan) => { + expectTypeOf(plan.bounds).toEqualTypeOf<[number, number]>() + return trace + }, + }, + { + name: `driver-resolved`, + law: `type.resolved`, + driver: (plan, observe) => { + expectTypeOf(plan.resolvedKind).toEqualTypeOf<`a` | `b`>() + observe(`bounds`, plan.bounds) + }, + }, + ], + }) + + // 3. Enumerated histories (plain array and `{ enumerated }`). + const enumeratedList: ReadonlyArray = [ + { kind: `a`, mode: `fast`, mask: [true, false], items: [] }, + { kind: `b`, count: 1, pair: [`left`, [0, 1]] }, + ] + oracleProperty({ + id: `type.enumerated`, + law, + histories: { enumerated: enumeratedList, states: [`seen`] }, + label: (history) => { + expectTypeOf(history).toEqualTypeOf() + return history.kind + }, + model: (plan) => plan.kind, + driver: (plan, observe) => { + expectTypeOf(plan).toEqualTypeOf() + observe(`kind`, plan.kind) + }, + check: (plan, _trace, expected) => { + expectTypeOf(plan).toEqualTypeOf() + expectTypeOf(expected).toEqualTypeOf<`a` | `b`>() + return pass + }, + }) + + // 4. Explicit type arguments remain backwards-compatible, including 0-arg + // `build` callbacks. + oracleProperty({ + id: `type.explicit`, + law, + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + constructed: [ + { + name: `zero-arg`, + enters: [], + build: () => ({ kind: `b`, count: 0, pair: [`right`, [0, 1]] }), + }, + ], + fixed: [ + { + name: `fixed`, + history: { kind: `a`, mode: `fast`, mask: [true, false], items: [] }, + }, + ], + }, + model: (plan) => plan.kind === `a`, + driver: () => {}, + check: (_plan, _trace, expected) => { + expectTypeOf(expected).toEqualTypeOf() + return pass + }, + }) + + // 5. Negative checks: invalid union literals, wrong tuple lengths, and + // mismatched callback/calibration parameter types are still rejected. + oracleProperty({ + id: `type.error.constructed-discriminant`, + law, + // @ts-expect-error invalid union literal returned from constructed[].build + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + constructed: [ + { + name: `bad-discriminant`, + enters: [], + build: (base) => + base.kind === `a` + ? { ...base, mode: `invalid`, mask: [true, false] } + : base, + }, + ], + }, + model: (plan) => plan.kind, + driver: () => {}, + check: () => pass, + }) + + oracleProperty({ + id: `type.error.constructed-tuple`, + law, + // @ts-expect-error wrong tuple length returned from constructed[].build + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + constructed: [ + { + name: `bad-tuple`, + enters: [], + build: (base) => + base.kind === `a` ? { ...base, mode: `fast`, mask: [true] } : base, + }, + ], + }, + model: (plan) => plan.kind, + driver: () => {}, + check: () => pass, + }) + + oracleProperty({ + id: `type.error.fixed-discriminant`, + law, + // @ts-expect-error invalid union literal in fixed[].history + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + fixed: [ + { + name: `bad-fixed-discriminant`, + history: { + kind: `a`, + mode: `invalid`, + mask: [true, false], + items: [], + }, + }, + ], + }, + model: (plan) => plan.kind, + driver: () => {}, + check: () => pass, + }) + + oracleProperty({ + id: `type.error.fixed-tuple`, + law, + // @ts-expect-error wrong tuple length in fixed[].history + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + fixed: [ + { + name: `bad-fixed-tuple`, + history: { + kind: `a`, + mode: `fast`, + mask: [true], + items: [], + }, + }, + ], + }, + model: (plan) => plan.kind, + driver: () => {}, + check: () => pass, + }) + + oracleProperty({ + id: `type.error.callbacks`, + law, + histories: { + arbitrary: variantArbitrary, + runs: 10, + fixedSeeds: [1], + }, + model: (plan) => plan.kind, + // @ts-expect-error mismatched driver parameter type without resolve + driver: (_plan: { unrelated: string }) => {}, + check: () => pass, + calibrations: [ + { + name: `bad-calibration-corrupt`, + law: `type.error.callbacks`, + // @ts-expect-error mismatched calibration plan parameter + corrupt: (_trace, _plan: { unrelated: string }) => undefined, + }, + { + name: `bad-calibration-driver`, + law: `type.error.callbacks`, + // @ts-expect-error mismatched calibration driver parameter + driver: (_plan: { unrelated: string }) => {}, + }, + ], + }) +} diff --git a/packages/oracles/tests/route-oracle.property.test.ts b/packages/oracles/tests/route-oracle.property.test.ts index 8565c61..fe2eb48 100644 --- a/packages/oracles/tests/route-oracle.property.test.ts +++ b/packages/oracles/tests/route-oracle.property.test.ts @@ -383,7 +383,7 @@ oracleProperty>({ { name: builtAfterRoute, enters: [builtAfterRoute], - build: (generated): History => ({ + build: (generated) => ({ ...generated, calls: [ { call: `route`, route: `fixed seed` }, @@ -395,7 +395,7 @@ oracleProperty>({ { name: lostDeclaration, enters: [lostDeclaration], - build: (generated): History => ({ + build: (generated) => ({ ...generated, calls: [ { call: `property options` }, @@ -416,7 +416,7 @@ oracleProperty>({ { name: declaredFixedSeed, enters: [declaredFixedSeed], - build: (generated): History => ({ + build: (generated) => ({ ...generated, calls: [ { call: `random parameters` }, diff --git a/packages/oracles/tests/within-turns-oracle.property.test.ts b/packages/oracles/tests/within-turns-oracle.property.test.ts index f781454..247531d 100644 --- a/packages/oracles/tests/within-turns-oracle.property.test.ts +++ b/packages/oracles/tests/within-turns-oracle.property.test.ts @@ -141,7 +141,7 @@ oracleProperty({ { name: alreadyNoTurns, enters: [alreadyNoTurns], - build: (generated): History => ({ + build: (generated) => ({ ...generated, settles: `already`, turns: 0, @@ -150,7 +150,7 @@ oracleProperty({ { name: noTurnCount, enters: [noTurnCount], - build: (generated): History => ({ ...generated, turns: 1.5 }), + build: (generated) => ({ ...generated, turns: 1.5 }), }, ], // A fractional count once waited two turns, and a negative one none.