@systemfsoftware/effect-spec-runtime 1.3.3 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,5 @@
1
1
  # @systemfsoftware/effect-spec-runtime
2
2
 
3
- # @systemfsoftware/effect-spec-runtime@1.3.3
3
+ # @systemfsoftware/effect-spec-runtime@1.4.0
4
4
 
5
- Render JSON text through `Schema.fromJsonString` instead of calling `JSON.stringify` directly, so the package passes the `global-json` check. Output is unchanged for JSON-serialisable values.
5
+ Adds `sharedLayer(layer)`, exported from the package entry and from `Suite`. Called at module top level, it returns a layer the whole test file shares: built once before the file's first case and released once when the file ends, both with no time limit. A suite's shared layer (`Suite.openShared`, `Suite.openSharedCase`) now runs on the same mechanism: it is built in the suite's setup before its first case starts, with no time limit, rather than by the first case inside that case's timeout, and released with no time limit. A failed shared build fails the setup hook with the build's error, so the cases that use it fail without running, and a shared layer read before its build ran dies with `SharedLayerNotBuilt`, exported from `Suite`. Adds the `TestFile` reference and its `TestFileInfo` type. While a shared layer builds, `TestFile` holds `Option.some` of the running file as Vitest identifies it: `name`, the path from the Vitest root with `/` separators; `rootName`, the root directory's name; and `projectName`, the Vitest project name or `undefined`. Otherwise it holds `Option.none()`.
package/README.md CHANGED
@@ -19,7 +19,7 @@ A registered explored case runs `explore` first and `exploreOnce` as the fork's
19
19
 
20
20
  ## Suite
21
21
 
22
- `Suite.open` registers a suite with no layer, `Suite.openShared` adds a layer shared by the whole suite, `Suite.openCase` adds a layer acquired fresh around every case, and `Suite.openSharedCase` does both. The shared layer is built once, by the first case that runs, every case of the suite sees those same services, and they are released when the suite ends; state a case must not share belongs in the case layer:
22
+ `Suite.open` registers a suite with no layer, `Suite.openShared` adds a layer shared by the whole suite, `Suite.openCase` adds a layer acquired fresh around every case, and `Suite.openSharedCase` does both. The shared layer is built once, in the suite's setup before its first case starts, with no time limit; every case of the suite sees those same services, and they are released, again with no time limit, when the suite ends. A failed build fails the suite's setup with the build's error, so its cases fail without running, and a shared layer read before its build ran dies with `SharedLayerNotBuilt`. State a case must not share belongs in the case layer:
23
23
 
24
24
  ```ts
25
25
  Suite.openShared(bindings, config, { layer: DatabaseFixture }, (register) => {
@@ -45,6 +45,12 @@ Suite.openShared(bindings, config, { layer: DatabaseFixture }, (register) => {
45
45
 
46
46
  An explored case's kernel run re-provides the test's binding onto the program (so the checks the kernel runs count for this test), scopes the run, and reports a failure with its seed and decision path. Its second run takes the same path through `KernelCase.exploreOnce`.
47
47
 
48
+ ## sharedLayer
49
+
50
+ `sharedLayer(layer)`, called at module top level, returns a layer that the whole test file shares: it is built once before the file's first case, in setup with no time limit, and released once when the file ends. It takes and returns a plain `Layer`, so it goes wherever a layer goes, and it runs on the same mechanism as a suite's shared layer.
51
+
52
+ While a shared layer builds, the `TestFile` reference holds `Option.some` of the running test file as Vitest identifies it: `name`, its path from the Vitest root with `/` separators; `rootName`, the root directory's name; and `projectName`, the Vitest project name or `undefined`. A layer can name what it creates after the file that created it. Outside a shared build it holds `Option.none()`.
53
+
48
54
  ## TaskRef
49
55
 
50
56
  `TaskRef.provideTaskRef(effect, ctx)` makes the running Vitest task context available to a case body:
package/dist/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- import { Context, Effect, Layer, Scope as Scope$1, Tracer } from "effect";
1
+ import { Context as Context$1, Effect, Layer, Schema, Scope as Scope$1, Tracer } from "effect";
2
2
  import { Expect, Vitest } from "@systemfsoftware/vitest";
3
3
  import { Asserted } from "@systemfsoftware/vitest/integration";
4
4
  import { TestOptions } from "vitest";
5
+ import * as Context from "effect/Context";
6
+ import * as Option from "effect/Option";
5
7
  import * as Scope from "effect/Scope";
6
8
  declare namespace KernelCase_d_exports {
7
9
  export { LiveCase, announceLive, caseProgram, explore, exploreOnce, liveCase };
@@ -52,8 +54,14 @@ interface SpanRecorder {
52
54
  declare const make: () => SpanRecorder;
53
55
  type RegisterMode = 'run' | 'skip' | 'only';
54
56
  type DescribeMode = 'describe' | 'skip' | 'only';
57
+ declare const SharedLayerNotBuilt_base: Schema.Class<SharedLayerNotBuilt, Schema.TaggedStruct<"SharedLayerNotBuilt", {}>, import("effect/Cause").YieldableError & {
58
+ readonly brand: unique symbol;
59
+ }>;
60
+ declare class SharedLayerNotBuilt extends SharedLayerNotBuilt_base {
61
+ get message(): string;
62
+ }
55
63
  declare namespace Suite_d_exports {
56
- export { Bindings, CaseLive, Config, DescribeMode, LiveCase, Options, RegisterFn, RegisterMode, Scenario, Shared, open, openCase, openShared, openSharedCase };
64
+ export { Bindings, CaseLive, Config, DescribeMode, LiveCase, Options, RegisterFn, RegisterMode, Scenario, Shared, SharedLayerNotBuilt, open, openCase, openShared, openSharedCase, sharedLayer };
57
65
  }
58
66
  type Options = Pick<TestOptions, 'tags'> & Partial<TestOptions>;
59
67
  interface Bindings {
@@ -65,8 +73,8 @@ interface Config {
65
73
  readonly options: Options | undefined;
66
74
  readonly live?: LiveCase;
67
75
  }
68
- interface Shared<R> {
69
- readonly layer: Layer.Layer<R>;
76
+ interface Shared<R, E = never> {
77
+ readonly layer: Layer.Layer<R, E>;
70
78
  }
71
79
  /**
72
80
  * A registered case: the runner's own `expect` arrives as the argument, so a case's assertion
@@ -89,17 +97,18 @@ declare const open: {
89
97
  <B, E, C>(config: Config, use: (register: RegisterFn<B, E, Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
90
98
  <B, E, C>(bindings: Bindings, config: Config, use: (register: RegisterFn<B, E, Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
91
99
  };
100
+ export declare const sharedLayer: <RShared, EShared>(layer: Layer.Layer<RShared, EShared>) => Layer.Layer<RShared>;
92
101
  declare const openShared: {
93
- <B, E, RShared, C>(config: Config, shared: Shared<RShared>, use: (register: RegisterFn<B, E, RShared | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
94
- <B, E, RShared, C>(bindings: Bindings, config: Config, shared: Shared<RShared>, use: (register: RegisterFn<B, E, RShared | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
102
+ <B, E, RShared, EShared, C>(config: Config, shared: Shared<RShared, EShared>, use: (register: RegisterFn<B, E, RShared | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
103
+ <B, E, RShared, EShared, C>(bindings: Bindings, config: Config, shared: Shared<RShared, EShared>, use: (register: RegisterFn<B, E, RShared | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
95
104
  };
96
105
  declare const openCase: {
97
106
  <B, E, RFresh, RFreshReq extends Scope$1.Scope, C>(config: Config, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RFresh | RFreshReq | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
98
107
  <B, E, RFresh, RFreshReq extends Scope$1.Scope, C>(bindings: Bindings, config: Config, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RFresh | RFreshReq | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
99
108
  };
100
109
  declare const openSharedCase: {
101
- <B, E, RShared, RFresh, RFreshReq extends RShared, C>(config: Config, shared: Shared<RShared>, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RShared | RFresh | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
102
- <B, E, RShared, RFresh, RFreshReq extends RShared, C>(bindings: Bindings, config: Config, shared: Shared<RShared>, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RShared | RFresh | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
110
+ <B, E, RShared, EShared, RFresh, RFreshReq extends RShared, C>(config: Config, shared: Shared<RShared, EShared>, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RShared | RFresh | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): (bindings: Bindings) => void;
111
+ <B, E, RShared, EShared, RFresh, RFreshReq extends RShared, C>(bindings: Bindings, config: Config, shared: Shared<RShared, EShared>, caseLayer: Layer.Layer<RFresh, never, RFreshReq>, use: (register: RegisterFn<B, E, RShared | RFresh | Scope$1.Scope>, propIt: Vitest.MethodsNonLive<never>) => C): void;
103
112
  };
104
113
  declare namespace TaskRef_service_d_exports {
105
114
  export { RawVitestTaskRef, VitestTaskContext, VitestTaskRef, provideTaskRef };
@@ -110,14 +119,20 @@ interface VitestTaskContext<Ann = unknown> {
110
119
  readonly annotations?: readonly Ann[] | undefined;
111
120
  } | undefined;
112
121
  }
113
- declare const VitestTaskRef: Context.Reference<VitestTaskContext | null>;
122
+ declare const VitestTaskRef: Context$1.Reference<VitestTaskContext | null>;
114
123
  /**
115
124
  * The task context as the fork's run provides it: built on the fork's own key, so a case lane that goes
116
125
  * through `provideTaskRef` and a property lane the fork runs itself read the same context.
117
126
  */
118
- declare const RawVitestTaskRef: Context.Reference<VitestTaskContext | null>;
127
+ declare const RawVitestTaskRef: Context$1.Reference<VitestTaskContext | null>;
119
128
  declare const provideTaskRef: {
120
129
  <Ctx>(ctx: Ctx): <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, E, R>;
121
130
  <A, E, R, Ctx>(effect: Effect.Effect<A, E, R>, ctx: Ctx): Effect.Effect<A, E, R>;
122
131
  };
123
- export { KernelCase_d_exports as KernelCase, SpanRecorder_d_exports as SpanRecorder, Suite_d_exports as Suite, TaskRef_service_d_exports as TaskRef };
132
+ interface TestFileInfo {
133
+ readonly name: string;
134
+ readonly projectName: string | undefined;
135
+ readonly rootName: string;
136
+ }
137
+ export declare const TestFile: Context.Reference<Option.Option<TestFileInfo>>;
138
+ export { KernelCase_d_exports as KernelCase, SpanRecorder_d_exports as SpanRecorder, Suite_d_exports as Suite, TaskRef_service_d_exports as TaskRef, type TestFileInfo };
package/dist/index.mjs CHANGED
@@ -1,11 +1,13 @@
1
1
  import { t as __exportAll } from "./rolldown-runtime-D3KUYPsf.mjs";
2
2
  import { Kernel } from "@systemfsoftware/effect-sim-kernel";
3
3
  import { createSpanRecorder, providedWorkspaceRoot, replayOfText, testIdentityOf, throwFailureRecord } from "@systemfsoftware/vitest/failure";
4
- import { Cause, Config, Context, Effect, Exit, Layer, Match, Option, Result, Schema, Scope, Tracer } from "effect";
4
+ import { Cause, Config, Context as Context$1, Effect, Exit, Layer, Match, Option as Option$1, Result, Schema, Scope, Tracer } from "effect";
5
5
  import { dual } from "effect/Function";
6
6
  import { VitestTestContext, vitestTestContextKey } from "@systemfsoftware/vitest";
7
7
  import { captureRunBinding } from "@systemfsoftware/vitest/integration";
8
- import { afterAll, describe } from "vitest";
8
+ import { TestRunner, afterAll, beforeAll, describe } from "vitest";
9
+ import * as Context from "effect/Context";
10
+ import * as Option from "effect/Option";
9
11
  // src/KernelFailure.schema.ts
10
12
  /**
11
13
  * The kernel's own failure — a deadlock, an escape, a wait it cannot observe, or a runaway run — as a failure
@@ -23,16 +25,16 @@ var TaskRef_service_exports = /* @__PURE__ */ __exportAll({
23
25
  VitestTaskRef: () => VitestTaskRef,
24
26
  provideTaskRef: () => provideTaskRef
25
27
  });
26
- const VitestTaskRef = Context.Reference("@systemfsoftware/effect-spec-runtime/VitestTask", { defaultValue: () => null });
28
+ const VitestTaskRef = Context$1.Reference("@systemfsoftware/effect-spec-runtime/VitestTask", { defaultValue: () => null });
27
29
  /**
28
30
  * The task context as the fork's run provides it: built on the fork's own key, so a case lane that goes
29
31
  * through `provideTaskRef` and a property lane the fork runs itself read the same context.
30
32
  */
31
- const RawVitestTaskRef = Context.Reference(vitestTestContextKey, { defaultValue: () => null });
33
+ const RawVitestTaskRef = Context$1.Reference(vitestTestContextKey, { defaultValue: () => null });
32
34
  const isPlainTaskContext = (ctx) => typeof ctx === "object" && ctx !== null;
33
35
  const isTaskContext = (ctx) => typeof ctx === "object" ? ctx !== null : typeof ctx === "function";
34
- const readTaskContext = (ctx) => Option.getOrNull(Option.liftPredicate(ctx, isPlainTaskContext));
35
- const readRawTaskContext = (ctx) => Option.getOrNull(Option.liftPredicate(ctx, isTaskContext));
36
+ const readTaskContext = (ctx) => Option$1.getOrNull(Option$1.liftPredicate(ctx, isPlainTaskContext));
37
+ const readRawTaskContext = (ctx) => Option$1.getOrNull(Option$1.liftPredicate(ctx, isTaskContext));
36
38
  const provideTaskRefImpl = (effect, ctx) => effect.pipe(Effect.provideService(VitestTaskRef, readTaskContext(ctx)), Effect.provideService(RawVitestTaskRef, readRawTaskContext(ctx)));
37
39
  const provideTaskRef = dual(2, provideTaskRefImpl);
38
40
  // src/KernelCase.ts
@@ -74,11 +76,11 @@ const announceLive = (reason) => Effect.gen(function* () {
74
76
  const task = yield* RawVitestTaskRef;
75
77
  return yield* announce(task, reason);
76
78
  });
77
- const dispatchAnnotate = (annotate, message) => Option.match(Option.fromNullishOr(annotate), {
79
+ const dispatchAnnotate = (annotate, message) => Option$1.match(Option$1.fromNullishOr(annotate), {
78
80
  onNone: () => Effect.void,
79
81
  onSome: (record) => Effect.promise(() => Promise.resolve(record(message)))
80
82
  });
81
- const announce = (task, reason) => Option.match(Option.fromNullishOr(task), {
83
+ const announce = (task, reason) => Option$1.match(Option$1.fromNullishOr(task), {
82
84
  onNone: () => Effect.void,
83
85
  onSome: (context) => dispatchAnnotate(context.annotate, `live case: ${reason}`)
84
86
  });
@@ -139,7 +141,7 @@ const seedsToRun = (seed, budget) => {
139
141
  if (seed === void 0) return seedList(budget);
140
142
  return [seed];
141
143
  };
142
- const readReplayValue = () => Effect.runPromise(Effect.map(Config.option(Config.String("CONFORMANCE_REPLAY")), Option.getOrUndefined));
144
+ const readReplayValue = () => Effect.runPromise(Effect.map(Config.option(Config.String("CONFORMANCE_REPLAY")), Option$1.getOrUndefined));
143
145
  const replayRun = (program, replay, path) => {
144
146
  const run = recorded(program);
145
147
  return Kernel.run(run.program, {
@@ -213,7 +215,7 @@ const selectDescribe = (mode) => ({
213
215
  only: describe.only,
214
216
  describe
215
217
  })[mode];
216
- const invokeDescribeImpl = (mode, suiteName, suiteOpts, fn) => Option.match(Option.fromUndefinedOr(suiteOpts), {
218
+ const invokeDescribeImpl = (mode, suiteName, suiteOpts, fn) => Option$1.match(Option$1.fromUndefinedOr(suiteOpts), {
217
219
  onNone: () => {
218
220
  selectDescribe(mode)(suiteName, fn);
219
221
  },
@@ -273,12 +275,35 @@ const exploredOnceProgram = (program) => exploredWith(exploreOnce, program);
273
275
  */
274
276
  const selectCaseRunnerImpl = (methodsIt, mode, live) => pickLane(caseLanes(methodsIt, live), mode);
275
277
  const selectCaseRunner = dual(3, selectCaseRunnerImpl);
278
+ // src/SharedLayer.schema.ts
279
+ var SharedLayerNotBuilt = class extends Schema.TaggedError()("SharedLayerNotBuilt", {}) {
280
+ get message() {
281
+ return "A shared layer was read before its build ran";
282
+ }
283
+ };
284
+ // src/TestFile.ts
285
+ const TestFile = Context.Reference("@systemfsoftware/effect-spec-runtime/TestFile", { defaultValue: Option.none });
286
+ const rootNameOf = (filepath, name) => {
287
+ const root = filepath.slice(0, filepath.length - name.length - 1);
288
+ return root.slice(root.lastIndexOf("/") + 1);
289
+ };
290
+ const projectNameOf = (projectName) => projectName === "" ? void 0 : projectName;
291
+ const testFileInfo = (file) => {
292
+ const name = file.name.replaceAll("\\", "/");
293
+ return {
294
+ name,
295
+ projectName: projectNameOf(file.projectName),
296
+ rootName: rootNameOf(file.filepath, name)
297
+ };
298
+ };
276
299
  // src/Suite.ts
277
300
  var Suite_exports = /* @__PURE__ */ __exportAll({
301
+ SharedLayerNotBuilt: () => SharedLayerNotBuilt,
278
302
  open: () => open,
279
303
  openCase: () => openCase,
280
304
  openShared: () => openShared,
281
- openSharedCase: () => openSharedCase
305
+ openSharedCase: () => openSharedCase,
306
+ sharedLayer: () => sharedLayer
282
307
  });
283
308
  const caseEffectOf = (body, env, live) => {
284
309
  if (live !== void 0) return liveCase(body, env, live.reason);
@@ -336,20 +361,25 @@ const openImpl = (bindings, config, use) => {
336
361
  });
337
362
  };
338
363
  const open = dual(3, openImpl);
339
- /**
340
- * The suite's layer, built once by the first case that needs it and released when the suite
341
- * ends, so every case of the suite sees the same services. The build runs on its own root
342
- * fiber: the first case's fiber ends with that case, and the suite's resources must not.
343
- */
344
- const suiteLayerOf = (shared) => {
364
+ const sharedLayerOf = (shared) => {
345
365
  const scope = Scope.makeUnsafe();
346
- const built = Effect.runSync(Effect.cached(Effect.flatten(Effect.promise(() => Effect.runPromiseExit(Layer.buildWithScope(shared.layer, scope))))));
366
+ const testFile = Option$1.some(testFileInfo(TestRunner.getCurrentSuite().file));
367
+ let built;
368
+ beforeAll(async () => {
369
+ const exit = await Effect.runPromiseExit(Layer.buildWithScope(shared.layer, scope).pipe(Effect.provideService(TestFile, testFile)));
370
+ if (Exit.isFailure(exit)) {
371
+ await Effect.runPromise(Scope.close(scope, Exit.void).pipe(Effect.ignore));
372
+ throw Cause.squash(exit.cause);
373
+ }
374
+ built = exit.value;
375
+ }, UNTIMED.timeout);
347
376
  afterAll(() => Effect.runPromise(Scope.close(scope, Exit.void)), UNTIMED.timeout);
348
- return Layer.unwrap(Effect.map(built, Layer.succeedContext));
377
+ return Layer.unwrap(Effect.suspend(() => built === void 0 ? Effect.die(new SharedLayerNotBuilt()) : Effect.succeed(Layer.succeedContext(built))));
349
378
  };
379
+ const sharedLayer = (layer) => sharedLayerOf({ layer });
350
380
  const openSharedImpl = (bindings, config, shared, use) => {
351
381
  invokeDescribe(config.describe, config.name, config.options, () => {
352
- use(registrarFor(bindings.it, config, suiteLayerOf(shared)), bindings.it);
382
+ use(registrarFor(bindings.it, config, sharedLayerOf(shared)), bindings.it);
353
383
  });
354
384
  };
355
385
  const openShared = dual(4, openSharedImpl);
@@ -361,8 +391,8 @@ const openCaseImpl = (bindings, config, caseLayer, use) => {
361
391
  const openCase = dual(4, openCaseImpl);
362
392
  const openSharedCaseImpl = (bindings, config, shared, caseLayer, use) => {
363
393
  invokeDescribe(config.describe, config.name, config.options, () => {
364
- use(registrarFor(bindings.it, config, Layer.fresh(caseLayer).pipe(Layer.provideMerge(suiteLayerOf(shared)))), bindings.it);
394
+ use(registrarFor(bindings.it, config, Layer.fresh(caseLayer).pipe(Layer.provideMerge(sharedLayerOf(shared)))), bindings.it);
365
395
  });
366
396
  };
367
397
  const openSharedCase = dual(5, openSharedCaseImpl);
368
- export { KernelCase_exports as KernelCase, SpanRecorder_exports as SpanRecorder, Suite_exports as Suite, TaskRef_service_exports as TaskRef };
398
+ export { KernelCase_exports as KernelCase, SpanRecorder_exports as SpanRecorder, Suite_exports as Suite, TaskRef_service_exports as TaskRef, TestFile, sharedLayer };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@systemfsoftware/effect-spec-runtime",
3
3
  "license": "Apache-2.0",
4
- "version": "1.3.3",
4
+ "version": "1.4.0",
5
5
  "author": "Ryan Lee <drdgvhbh@gmail.com>",
6
6
  "repository": {
7
7
  "type": "git",
@@ -37,9 +37,9 @@
37
37
  "devDependencies": {
38
38
  "@microsoft/api-extractor": "^7.60.1",
39
39
  "@systemfsoftware/arethetypeswrong-cli": "^4.2.0",
40
- "@systemfsoftware/oxlint-config-recommended": "^9.0.0",
40
+ "@systemfsoftware/oxlint-config-recommended": "^9.0.2",
41
41
  "@systemfsoftware/tsconfig": "^2.1.0",
42
- "@systemfsoftware/tsdown-config": "^0.1.3",
42
+ "@systemfsoftware/tsdown-config": "^0.2.0",
43
43
  "@systemfsoftware/vitest": "^4.2.1",
44
44
  "@systemfsoftware/vitest-config": "^0.1.4",
45
45
  "@types/node": "^26.6.6",