@shivaedev/test-story 0.24.0 → 0.25.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 +22 -0
- package/README.md +93 -69
- package/dist/client/react.d.ts +28 -0
- package/dist/client/react.d.ts.map +1 -0
- package/dist/client/react.js +108 -0
- package/dist/client/react.js.map +1 -0
- package/dist/server/domEnvironment.config.d.ts.map +1 -1
- package/dist/server/domEnvironment.config.js +7 -1
- package/dist/server/domEnvironment.config.js.map +1 -1
- package/dist/server/fakeHttpService.d.ts +0 -2
- package/dist/server/fakeHttpService.d.ts.map +1 -1
- package/dist/server/fakeHttpService.js +1 -3
- package/dist/server/fakeHttpService.js.map +1 -1
- package/dist/server/internal/compose.d.ts.map +1 -1
- package/dist/server/internal/compose.js +1 -0
- package/dist/server/internal/compose.js.map +1 -1
- package/dist/server/internal/domDocument.d.ts +4 -0
- package/dist/server/internal/domDocument.d.ts.map +1 -0
- package/dist/server/internal/domDocument.js +13 -0
- package/dist/server/internal/domDocument.js.map +1 -0
- package/dist/server/internal/fakeHttpControls.d.ts +2 -2
- package/dist/server/internal/fakeHttpControls.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpControls.js +14 -6
- package/dist/server/internal/fakeHttpControls.js.map +1 -1
- package/dist/server/internal/fakeHttpReceive.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpReceive.js +2 -7
- package/dist/server/internal/fakeHttpReceive.js.map +1 -1
- package/dist/server/internal/fakeHttpScripts.d.ts +1 -1
- package/dist/server/internal/fakeHttpScripts.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpScripts.js +2 -4
- package/dist/server/internal/fakeHttpScripts.js.map +1 -1
- package/dist/server/internal/hookInference.d.ts +1 -12
- package/dist/server/internal/hookInference.d.ts.map +1 -1
- package/dist/server/internal/installOutbound.d.ts.map +1 -1
- package/dist/server/internal/installOutbound.js +1 -1
- package/dist/server/internal/installOutbound.js.map +1 -1
- package/dist/server/internal/seed.d.ts +1 -0
- package/dist/server/internal/seed.d.ts.map +1 -1
- package/dist/server/internal/seed.js.map +1 -1
- package/dist/server/internal/storyIt.d.ts.map +1 -1
- package/dist/server/internal/storyIt.js +4 -2
- package/dist/server/internal/storyIt.js.map +1 -1
- package/dist/server/routeOutbound.d.ts +3 -1
- package/dist/server/routeOutbound.d.ts.map +1 -1
- package/dist/server/routeOutbound.js +3 -2
- package/dist/server/routeOutbound.js.map +1 -1
- package/dist/server/storyKit.d.ts +2 -4
- package/dist/server/storyKit.d.ts.map +1 -1
- package/dist/server/storyKit.js.map +1 -1
- package/package.json +24 -6
- package/src/client/react.ts +142 -0
- package/src/server/domEnvironment.config.ts +7 -1
- package/src/server/fakeHttpService.ts +1 -5
- package/src/server/internal/compose.ts +1 -0
- package/src/server/internal/domDocument.ts +15 -0
- package/src/server/internal/fakeHttpControls.ts +14 -9
- package/src/server/internal/fakeHttpReceive.ts +2 -6
- package/src/server/internal/fakeHttpScripts.ts +2 -4
- package/src/server/internal/hookInference.ts +1 -19
- package/src/server/internal/installOutbound.ts +1 -1
- package/src/server/internal/seed.ts +1 -0
- package/src/server/internal/storyIt.ts +4 -2
- package/src/server/routeOutbound.ts +3 -2
- package/src/server/storyKit.ts +2 -15
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@shivaedev/test-story",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.25.0",
|
|
4
4
|
"description": "Tests that read like stories over the real engine: a story kit per engine with its own it, traits per feature, and a failure that prints the story, where it stopped and the engine's state",
|
|
5
5
|
"homepage": "https://github.com/ShivaeDev/platform/tree/main/packages/test-story#readme",
|
|
6
6
|
"bugs": {
|
|
@@ -28,47 +28,65 @@
|
|
|
28
28
|
"types": "./dist/*.d.ts",
|
|
29
29
|
"default": "./dist/*.js"
|
|
30
30
|
},
|
|
31
|
-
"./package.json": "./package.json"
|
|
32
|
-
"./server/internal/*": null
|
|
31
|
+
"./package.json": "./package.json"
|
|
33
32
|
},
|
|
34
33
|
"files": [
|
|
35
34
|
"dist",
|
|
36
35
|
"src",
|
|
37
36
|
"!src/**/*.test.ts",
|
|
37
|
+
"!src/**/*.test.tsx",
|
|
38
38
|
"!src/**/*.spec.ts",
|
|
39
|
+
"!src/**/*.spec.tsx",
|
|
39
40
|
"!src/**/test-support",
|
|
40
41
|
"CHANGELOG.md",
|
|
41
42
|
"README.md"
|
|
42
43
|
],
|
|
43
44
|
"dependencies": {
|
|
44
|
-
"@shivaedev/test": "0.
|
|
45
|
-
"@shivaedev/test-sql": "0.
|
|
45
|
+
"@shivaedev/test": "0.11.0",
|
|
46
|
+
"@shivaedev/test-sql": "0.12.0",
|
|
46
47
|
"dom-accessibility-api": "0.7.1"
|
|
47
48
|
},
|
|
48
49
|
"devDependencies": {
|
|
50
|
+
"@effect/atom-react": "4.0.6",
|
|
49
51
|
"@effect/platform-node": "4.0.6",
|
|
50
52
|
"@effect/vitest": "4.0.6",
|
|
51
|
-
"@shivaedev/quality": "0.
|
|
53
|
+
"@shivaedev/quality": "0.46.0",
|
|
52
54
|
"@types/node": "24.19.2",
|
|
55
|
+
"@types/react": "19.3.0",
|
|
56
|
+
"@types/react-dom": "19.3.0",
|
|
53
57
|
"effect": "4.0.6",
|
|
54
58
|
"happy-dom": "21.0.4",
|
|
59
|
+
"react": "19.3.1",
|
|
60
|
+
"react-dom": "19.3.1",
|
|
55
61
|
"typescript": "7.0.2",
|
|
56
62
|
"vitest": "5.0.5"
|
|
57
63
|
},
|
|
58
64
|
"peerDependencies": {
|
|
65
|
+
"@effect/atom-react": "4.0.6",
|
|
59
66
|
"@effect/platform-node": "4.0.6",
|
|
60
67
|
"@effect/sql-pg": "4.0.6",
|
|
61
68
|
"@effect/vitest": "4.0.6",
|
|
62
69
|
"effect": "4.0.6",
|
|
63
70
|
"happy-dom": "21.0.4",
|
|
71
|
+
"react": "19.3.1",
|
|
72
|
+
"react-dom": "19.3.1",
|
|
64
73
|
"vitest": "5.0.5"
|
|
65
74
|
},
|
|
66
75
|
"peerDependenciesMeta": {
|
|
76
|
+
"@effect/atom-react": {
|
|
77
|
+
"optional": true
|
|
78
|
+
},
|
|
67
79
|
"@effect/sql-pg": {
|
|
68
80
|
"optional": true
|
|
69
81
|
},
|
|
70
82
|
"happy-dom": {
|
|
71
83
|
"optional": true
|
|
84
|
+
},
|
|
85
|
+
"react": {
|
|
86
|
+
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"react-dom": {
|
|
89
|
+
"optional": true
|
|
72
90
|
}
|
|
73
91
|
},
|
|
74
92
|
"engines": {
|
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
import { RegistryContext } from "@effect/atom-react";
|
|
2
|
+
import { Clock, Effect, Exit, Scope } from "effect";
|
|
3
|
+
import * as AtomRegistry from "effect/reactivity/AtomRegistry";
|
|
4
|
+
import { act, createElement, type ReactNode } from "react";
|
|
5
|
+
import { createRoot, type RootOptions } from "react-dom/client";
|
|
6
|
+
import { beforeEach, expect, vi } from "vitest";
|
|
7
|
+
import { eventually as retry } from "@shivaedev/test/eventually.ts";
|
|
8
|
+
import { withLive } from "@shivaedev/test/withLive.ts";
|
|
9
|
+
|
|
10
|
+
type CaughtErrorHandler = NonNullable<RootOptions["onCaughtError"]>;
|
|
11
|
+
|
|
12
|
+
export interface ReactWarningsReadout {
|
|
13
|
+
readonly caughtErrors: readonly unknown[];
|
|
14
|
+
readonly consoleErrors: readonly (readonly unknown[])[];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface ReactWarnings {
|
|
18
|
+
readonly expectCaughtError: (matches: (error: unknown) => boolean) => CaughtErrorHandler;
|
|
19
|
+
readonly onCaughtError: CaughtErrorHandler;
|
|
20
|
+
readonly read: () => ReactWarningsReadout;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const acquireReactWarnings = Effect.acquireRelease(
|
|
24
|
+
Effect.sync(() => {
|
|
25
|
+
const errors = vi.spyOn(console, "error").mockImplementation(() => undefined);
|
|
26
|
+
let consoleErrors: readonly (readonly unknown[])[] | undefined;
|
|
27
|
+
const caughtErrors: unknown[] = [];
|
|
28
|
+
const expectations: { caught: boolean }[] = [];
|
|
29
|
+
const capture: ReactWarnings = {
|
|
30
|
+
expectCaughtError: (matches) => {
|
|
31
|
+
const expectation = { caught: false };
|
|
32
|
+
expectations.push(expectation);
|
|
33
|
+
return (error) => {
|
|
34
|
+
if (matches(error)) {
|
|
35
|
+
expectation.caught = true;
|
|
36
|
+
} else {
|
|
37
|
+
caughtErrors.push(error);
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
},
|
|
41
|
+
onCaughtError: (error) => {
|
|
42
|
+
caughtErrors.push(error);
|
|
43
|
+
},
|
|
44
|
+
read: () => ({ caughtErrors: [...caughtErrors], consoleErrors: consoleErrors ?? errors.mock.calls.map((args) => [...args]) }),
|
|
45
|
+
};
|
|
46
|
+
return {
|
|
47
|
+
capture,
|
|
48
|
+
expectations,
|
|
49
|
+
restore: () => {
|
|
50
|
+
consoleErrors = capture.read().consoleErrors;
|
|
51
|
+
errors.mockRestore();
|
|
52
|
+
},
|
|
53
|
+
};
|
|
54
|
+
}),
|
|
55
|
+
({ restore }) => Effect.sync(restore),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
export const captureReactWarnings = Effect.map(acquireReactWarnings, ({ capture }) => capture);
|
|
59
|
+
|
|
60
|
+
export function expectNoReactWarnings() {
|
|
61
|
+
let current: Effect.Success<typeof acquireReactWarnings>;
|
|
62
|
+
beforeEach(({ onTestFinished }) => {
|
|
63
|
+
const scope = Effect.runSync(Scope.make());
|
|
64
|
+
current = Effect.runSync(Effect.provideService(acquireReactWarnings, Scope.Scope, scope));
|
|
65
|
+
const { capture, expectations } = current;
|
|
66
|
+
onTestFinished(() => {
|
|
67
|
+
Effect.runSync(Scope.close(scope, Exit.void));
|
|
68
|
+
const { consoleErrors, caughtErrors } = capture.read();
|
|
69
|
+
expect(consoleErrors).toEqual([]);
|
|
70
|
+
expect(caughtErrors, "Unexpected caught React errors").toEqual([]);
|
|
71
|
+
expect(
|
|
72
|
+
expectations.every(({ caught }) => caught),
|
|
73
|
+
"Expected caught React error did not occur",
|
|
74
|
+
).toBe(true);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
return {
|
|
78
|
+
expectCaughtError(matches: (error: unknown) => boolean): NonNullable<RootOptions["onCaughtError"]> {
|
|
79
|
+
return current.capture.expectCaughtError(matches);
|
|
80
|
+
},
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
export function eventually(assert: () => void) {
|
|
84
|
+
return withLive(
|
|
85
|
+
Effect.gen(function* () {
|
|
86
|
+
const started = yield* Clock.currentTimeMillis;
|
|
87
|
+
return yield* retry(Effect.try({ catch: (error) => error, try: assert }), {
|
|
88
|
+
timeout: "3 seconds",
|
|
89
|
+
// Responses and React updates must settle together inside act.
|
|
90
|
+
wait: Effect.promise(() =>
|
|
91
|
+
act(async () => {
|
|
92
|
+
await new Promise<void>((resolve) => setTimeout(resolve, 10));
|
|
93
|
+
}),
|
|
94
|
+
),
|
|
95
|
+
}).pipe(
|
|
96
|
+
Effect.catch((error) =>
|
|
97
|
+
Effect.gen(function* () {
|
|
98
|
+
const elapsed = (yield* Clock.currentTimeMillis) - started;
|
|
99
|
+
return yield* Effect.fail(
|
|
100
|
+
new Error(`Assertion did not pass after ${elapsed}ms: ${error instanceof Error ? error.message : String(error)}`, { cause: error }),
|
|
101
|
+
);
|
|
102
|
+
}),
|
|
103
|
+
),
|
|
104
|
+
);
|
|
105
|
+
}),
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
export interface Rendered {
|
|
109
|
+
readonly container: HTMLDivElement;
|
|
110
|
+
readonly registry: AtomRegistry.AtomRegistry;
|
|
111
|
+
readonly render: (node: ReactNode) => Promise<void>;
|
|
112
|
+
readonly unmount: () => Promise<void>;
|
|
113
|
+
}
|
|
114
|
+
export async function render(node: ReactNode, options?: RootOptions): Promise<Rendered> {
|
|
115
|
+
const registry = AtomRegistry.make();
|
|
116
|
+
const container = document.createElement("div");
|
|
117
|
+
document.body.append(container);
|
|
118
|
+
const root = createRoot(container, options);
|
|
119
|
+
let mounted = true;
|
|
120
|
+
async function unmount() {
|
|
121
|
+
if (!mounted) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
mounted = false;
|
|
125
|
+
try {
|
|
126
|
+
await act(async () => root.unmount());
|
|
127
|
+
} finally {
|
|
128
|
+
registry.dispose();
|
|
129
|
+
container.remove();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
async function update(next: ReactNode) {
|
|
133
|
+
await act(async () => root.render(createElement(RegistryContext.Provider, { value: registry }, next)));
|
|
134
|
+
}
|
|
135
|
+
try {
|
|
136
|
+
await update(node);
|
|
137
|
+
} catch (error) {
|
|
138
|
+
await unmount();
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
return { container, registry, render: update, unmount };
|
|
142
|
+
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createRequire } from "node:module";
|
|
2
2
|
import { GlobalWindow } from "happy-dom";
|
|
3
|
+
import WindowBrowserContext from "happy-dom/lib/window/WindowBrowserContext.js";
|
|
3
4
|
import { type Environment, populateGlobal } from "vitest/runtime";
|
|
4
5
|
|
|
5
6
|
const domEnvironment: Environment = {
|
|
@@ -28,7 +29,12 @@ const domEnvironment: Environment = {
|
|
|
28
29
|
}
|
|
29
30
|
Object.defineProperty(global, "window", { configurable: true, value: win, writable: true });
|
|
30
31
|
Object.defineProperty(win.document, "defaultView", { configurable: true, get: () => win });
|
|
31
|
-
|
|
32
|
+
function freshDocument() {
|
|
33
|
+
new WindowBrowserContext(win).getBrowserContext()?.cookieContainer.clearCookies();
|
|
34
|
+
win.localStorage.clear();
|
|
35
|
+
win.sessionStorage.clear();
|
|
36
|
+
}
|
|
37
|
+
Object.defineProperty(global, Symbol.for("test-story.dom"), { configurable: true, value: freshDocument });
|
|
32
38
|
return {
|
|
33
39
|
async teardown(target) {
|
|
34
40
|
try {
|
|
@@ -75,15 +75,13 @@ export const fakeHttpService = Effect.fnUntraced(function* (options: FakeHttpSer
|
|
|
75
75
|
const controls = fakeHttpControls(tell, () => clock.currentTimeMillisUnsafe());
|
|
76
76
|
const server = createServer((request, response) => fakeHttpReceive(request, response, options, requests, controls, tell));
|
|
77
77
|
const url = yield* loopbackServer(server);
|
|
78
|
-
yield* Effect.addFinalizer(
|
|
78
|
+
yield* Effect.addFinalizer(controls.close);
|
|
79
79
|
function checkRoute(route: string) {
|
|
80
80
|
if (!Object.hasOwn(options.routes, route)) {
|
|
81
81
|
throw new Error(`Unknown fake HTTP route "${route}".\nhelp: Use a name from fakeHttpService's routes when scripting a reply or hold.`);
|
|
82
82
|
}
|
|
83
83
|
}
|
|
84
84
|
return {
|
|
85
|
-
failOnce: (route: string, reply: FakeHttpReplySource, scope?: FakeHttpScope) =>
|
|
86
|
-
Effect.sync(() => checkRoute(route)).pipe(Effect.andThen(controls.queue(route, reply, scope))),
|
|
87
85
|
fault: (route: string, fault: FakeHttpFault, scope?: FakeHttpScope) =>
|
|
88
86
|
Effect.sync(() => checkRoute(route)).pipe(Effect.andThen(controls.fault(route, fault, scope))),
|
|
89
87
|
hold: (route: string, holdOptions?: FakeHttpHoldOptions) =>
|
|
@@ -96,8 +94,6 @@ export const fakeHttpService = Effect.fnUntraced(function* (options: FakeHttpSer
|
|
|
96
94
|
checkRoute(route);
|
|
97
95
|
checkRoute(arrival);
|
|
98
96
|
}).pipe(Effect.andThen(controls.releaseOnArrival(route, arrival, scope))),
|
|
99
|
-
replyOnce: (route: string, reply: FakeHttpReplySource, scope?: FakeHttpScope) =>
|
|
100
|
-
Effect.sync(() => checkRoute(route)).pipe(Effect.andThen(controls.queue(route, reply, scope))),
|
|
101
97
|
get requests(): readonly FakeHttpRequestRecord[] {
|
|
102
98
|
return requests.slice();
|
|
103
99
|
},
|
|
@@ -27,6 +27,7 @@ export function compose(name: string, kits: KitList): LooseDefinition {
|
|
|
27
27
|
layer: definitions.reduce<AnyTestLayer>((layer, definition) => Layer.merge(layer, definition.layer ?? Layer.empty), Layer.empty),
|
|
28
28
|
name,
|
|
29
29
|
outbound: Object.assign({}, ...definitions.map((definition) => definition.outbound)),
|
|
30
|
+
outboundPassthrough: definitions.some((definition) => definition.outboundPassthrough === true),
|
|
30
31
|
runIf: definitions.every((definition) => definition.runIf !== false),
|
|
31
32
|
stages: definitions.flatMap((definition) => definition.stages),
|
|
32
33
|
verbs: (engine, story) => {
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
|
|
3
|
+
const DOM = Symbol.for("test-story.dom");
|
|
4
|
+
|
|
5
|
+
export function inDom() {
|
|
6
|
+
return typeof Reflect.get(globalThis, DOM) === "function";
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// The DOM environment keeps one window per file, so each story clears what an earlier story stored in it.
|
|
10
|
+
export const freshDocument = Effect.sync(() => {
|
|
11
|
+
const clear: unknown = Reflect.get(globalThis, DOM);
|
|
12
|
+
if (typeof clear === "function") {
|
|
13
|
+
clear();
|
|
14
|
+
}
|
|
15
|
+
});
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import type { ServerResponse } from "node:http";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
-
import * as HttpStatus from "effect/http/HttpStatus";
|
|
4
3
|
import type {
|
|
5
4
|
FakeHttpFault,
|
|
6
5
|
FakeHttpHoldOptions,
|
|
@@ -13,8 +12,11 @@ import { fakeHttpGates } from "#internal/fakeHttpGates.ts";
|
|
|
13
12
|
import { fakeHttpScripts } from "#internal/fakeHttpScripts.ts";
|
|
14
13
|
import { fakeHttpSelection } from "#internal/fakeHttpSelection.ts";
|
|
15
14
|
import { fakeHttpConnection, fakeHttpSend } from "#internal/fakeHttpSend.ts";
|
|
15
|
+
import { describeValue } from "#internal/render.ts";
|
|
16
|
+
import { explained } from "#internal/wrap.ts";
|
|
16
17
|
|
|
17
18
|
export function fakeHttpControls(tell: (line: string) => void, now: () => number) {
|
|
19
|
+
let failure: Error | undefined;
|
|
18
20
|
const scripts = fakeHttpScripts(tell);
|
|
19
21
|
const holds = fakeHttpGates(tell, "hold");
|
|
20
22
|
const stalls = fakeHttpGates(tell, "stall");
|
|
@@ -29,7 +31,7 @@ export function fakeHttpControls(tell: (line: string) => void, now: () => number
|
|
|
29
31
|
}
|
|
30
32
|
arrivals.delete(route);
|
|
31
33
|
},
|
|
32
|
-
close: ()
|
|
34
|
+
close: Effect.fnUntraced(function* () {
|
|
33
35
|
for (const response of active.keys()) {
|
|
34
36
|
response.destroy();
|
|
35
37
|
}
|
|
@@ -37,6 +39,16 @@ export function fakeHttpControls(tell: (line: string) => void, now: () => number
|
|
|
37
39
|
stalls.close();
|
|
38
40
|
arrivals.clear();
|
|
39
41
|
scripts.clear();
|
|
42
|
+
if (failure !== undefined) {
|
|
43
|
+
yield* Effect.die(failure);
|
|
44
|
+
}
|
|
45
|
+
}),
|
|
46
|
+
failed: (method: string, path: string, cause: unknown) => {
|
|
47
|
+
const message = `fake HTTP could not answer ${method} ${path}: ${describeValue(cause)}`;
|
|
48
|
+
tell(message);
|
|
49
|
+
failure ??= new Error(explained(message, "Fix the fake's handler. To simulate an HTTP failure, return a reply with an error status."), {
|
|
50
|
+
cause,
|
|
51
|
+
});
|
|
40
52
|
},
|
|
41
53
|
fault: (route: string, fault: FakeHttpFault, scope?: FakeHttpScope) =>
|
|
42
54
|
Effect.promise(async () => {
|
|
@@ -65,13 +77,6 @@ export function fakeHttpControls(tell: (line: string) => void, now: () => number
|
|
|
65
77
|
await fakeHttpConnection(response).pause(holds.get(route, request)?.enter());
|
|
66
78
|
return !(response.destroyed || response.writableEnded || response.headersSent);
|
|
67
79
|
},
|
|
68
|
-
queue: (route: string, reply: FakeHttpReplySource, scope?: FakeHttpScope) =>
|
|
69
|
-
Effect.sync(() => {
|
|
70
|
-
scripts.make(route, { kind: "status", reply }, scope, false);
|
|
71
|
-
tell(
|
|
72
|
-
`fake HTTP ${fakeHttpSelection(route, scope).label} will answer once with ${typeof reply === "function" ? "a request-time reply" : (reply.status ?? HttpStatus.fromLiteral("OK"))}`,
|
|
73
|
-
);
|
|
74
|
-
}),
|
|
75
80
|
releaseOnArrival: (route: string, arrival: string, scope?: FakeHttpScope) =>
|
|
76
81
|
Effect.sync(() => {
|
|
77
82
|
const hold = holds.at(route, scope);
|
|
@@ -107,11 +107,7 @@ export async function fakeHttpReceive(
|
|
|
107
107
|
}
|
|
108
108
|
await fakeHttpAnswer(found.name, found.route, request, response, controls, tell);
|
|
109
109
|
} catch (error) {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
response.destroy();
|
|
113
|
-
} else {
|
|
114
|
-
await fakeHttpSend(response, { status: HttpStatus.fromLiteral("InternalServerError") });
|
|
115
|
-
}
|
|
110
|
+
controls.failed(incoming.method ?? "GET", incoming.url ?? "/", error);
|
|
111
|
+
response.destroy();
|
|
116
112
|
}
|
|
117
113
|
}
|
|
@@ -10,13 +10,11 @@ export function fakeHttpScripts(tell: (line: string) => void) {
|
|
|
10
10
|
}>();
|
|
11
11
|
return {
|
|
12
12
|
clear: () => scripts.clear(),
|
|
13
|
-
make: (route: string, fault: FakeHttpFault, options: FakeHttpScriptOptions = {}
|
|
13
|
+
make: (route: string, fault: FakeHttpFault, options: FakeHttpScriptOptions = {}): FakeHttpScript => {
|
|
14
14
|
const selection = fakeHttpSelection(route, options);
|
|
15
15
|
const script = { fault, options, selection };
|
|
16
16
|
scripts.add(script);
|
|
17
|
-
|
|
18
|
-
tell(`fake HTTP ${selection.label} will fault ${fault.kind} ${options.phase ?? "before"} reply ${options.repeat ? "until lifted" : "once"}`);
|
|
19
|
-
}
|
|
17
|
+
tell(`fake HTTP ${selection.label} will fault ${fault.kind} ${options.phase ?? "before"} reply ${options.repeat ? "until lifted" : "once"}`);
|
|
20
18
|
return {
|
|
21
19
|
lift: Effect.sync(() => {
|
|
22
20
|
if (scripts.delete(script)) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Crypto, Effect, Layer, Scope } from "effect";
|
|
2
|
-
import type { AnyEffect,
|
|
2
|
+
import type { AnyEffect, HookEffects, HookValue, ServicesOf } from "#internal/hooked.ts";
|
|
3
3
|
import type { StoryKitDefinition } from "#storyKit.ts";
|
|
4
4
|
|
|
5
5
|
export type AfterResults<TStage extends string> = {
|
|
@@ -26,21 +26,3 @@ export type InferredDefinition<
|
|
|
26
26
|
readonly create: TCreate;
|
|
27
27
|
readonly after?: { readonly [TKey in keyof TAfter]: (engine: HookValue<ReturnType<TCreate>>) => TAfter[TKey] };
|
|
28
28
|
} & LayerFor<HookEffects<ReturnType<TCreate> | TAfter[keyof TAfter]> | TStep | TFailed | TDiagnose, TProvided>;
|
|
29
|
-
|
|
30
|
-
// A synchronous after hook has no effect to infer, so TypeScript leaves it at the AnyEffect constraint.
|
|
31
|
-
type AfterEffects<TAfter> = { readonly [TKey in keyof TAfter]-?: AnyEffect extends TAfter[TKey] ? never : TAfter[TKey] }[keyof TAfter];
|
|
32
|
-
|
|
33
|
-
export type LegacyDefinition<
|
|
34
|
-
TEngine,
|
|
35
|
-
TStage extends string,
|
|
36
|
-
TVerbs,
|
|
37
|
-
TProvided,
|
|
38
|
-
TCreate extends AnyEffect,
|
|
39
|
-
TAfter extends { readonly [TKey in TStage]?: AnyEffect },
|
|
40
|
-
TStep extends AnyEffect,
|
|
41
|
-
TFailed extends AnyEffect,
|
|
42
|
-
TDiagnose extends AnyEffect,
|
|
43
|
-
> = Omit<StoryKitDefinition<TEngine, TStage, TVerbs, TProvided, TCreate, TAfter, TStep, TFailed, TDiagnose>, "create" | "after"> & {
|
|
44
|
-
readonly create: Hook<[], TCreate, TEngine> & (TEngine extends AnyEffect ? never : unknown);
|
|
45
|
-
readonly after?: { readonly [TKey in keyof TAfter]: Hook<[engine: TEngine], TAfter[TKey] & AnyEffect, void> };
|
|
46
|
-
} & LayerFor<TCreate | AfterEffects<TAfter> | TStep | TFailed | TDiagnose, TProvided>;
|
|
@@ -13,7 +13,7 @@ function isDispatcher(value: unknown): value is Dispatcher {
|
|
|
13
13
|
return typeof value === "object" && value !== null && "dispatch" in value && typeof value.dispatch === "function";
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export async function installOutbound(routes: Readonly<Record<string, string>>, strict =
|
|
16
|
+
export async function installOutbound(routes: Readonly<Record<string, string>>, strict = true) {
|
|
17
17
|
const origins = new Map(Object.entries(routes).map(([hostname, target]) => [hostname, new URL(target).origin]));
|
|
18
18
|
let refused: Error | undefined;
|
|
19
19
|
// A data URL initializes Node's bundled Undici without sending an HTTP request.
|
|
@@ -26,6 +26,7 @@ export interface LooseDefinition {
|
|
|
26
26
|
readonly layer?: AnyTestLayer;
|
|
27
27
|
readonly name: string;
|
|
28
28
|
readonly outbound?: Readonly<Record<string, string>>;
|
|
29
|
+
readonly outboundPassthrough?: boolean;
|
|
29
30
|
readonly run?: LooseRunHooks<unknown>;
|
|
30
31
|
readonly runIf?: boolean;
|
|
31
32
|
readonly stages: readonly string[];
|
|
@@ -4,6 +4,7 @@ import { Effect, Layer } from "effect";
|
|
|
4
4
|
import * as TestCrypto from "effect/testing/TestCrypto";
|
|
5
5
|
import { makeEffectIt } from "@shivaedev/test/makeEffectIt.ts";
|
|
6
6
|
import type { EffectTest, EffectTestOptions } from "@shivaedev/test/types.ts";
|
|
7
|
+
import { freshDocument, inDom } from "#internal/domDocument.ts";
|
|
7
8
|
import { type Genre, genreOf } from "#internal/genre.ts";
|
|
8
9
|
import { type AnyEffect, hooked } from "#internal/hooked.ts";
|
|
9
10
|
import type { LooseDefinition, LooseTrait } from "#internal/seed.ts";
|
|
@@ -54,12 +55,13 @@ function declaring(definition: LooseDefinition, genre: Genre, declare: EffectTes
|
|
|
54
55
|
|
|
55
56
|
export function storyIt(definition: LooseDefinition): LooseIt {
|
|
56
57
|
const genre = genreOf(definition.name);
|
|
57
|
-
const outbound = definition.outbound ?? (
|
|
58
|
+
const outbound = definition.outbound ?? (inDom() ? {} : undefined);
|
|
58
59
|
const { effectApp } = makeEffectIt({
|
|
59
60
|
around: (effect) =>
|
|
60
61
|
Effect.gen(function* () {
|
|
62
|
+
yield* freshDocument;
|
|
61
63
|
if (outbound !== undefined) {
|
|
62
|
-
yield* routeOutbound(outbound);
|
|
64
|
+
yield* routeOutbound(outbound, { passthrough: definition.outboundPassthrough === true });
|
|
63
65
|
}
|
|
64
66
|
return yield* definition.around?.(effect) ?? effect;
|
|
65
67
|
}).pipe(Effect.provide(TestCrypto.layer(0).pipe(Layer.provide(NodeCrypto.layer)))),
|
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import { Effect } from "effect";
|
|
2
|
+
import { inDom } from "#internal/domDocument.ts";
|
|
2
3
|
import { installOutbound } from "#internal/installOutbound.ts";
|
|
3
4
|
|
|
4
|
-
export function routeOutbound(routes: Readonly<Record<string, string
|
|
5
|
+
export function routeOutbound(routes: Readonly<Record<string, string>>, options: { readonly passthrough?: boolean } = {}) {
|
|
5
6
|
return Effect.acquireRelease(
|
|
6
|
-
Effect.promise(() => installOutbound(routes,
|
|
7
|
+
Effect.promise(() => installOutbound(routes, !options.passthrough || inDom())),
|
|
7
8
|
(restore) => Effect.sync(restore),
|
|
8
9
|
).pipe(Effect.asVoid);
|
|
9
10
|
}
|
package/src/server/storyKit.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { EffectClock, EffectTestOptions } from "@shivaedev/test/types.ts";
|
|
|
4
4
|
import { compose } from "#internal/compose.ts";
|
|
5
5
|
import { type ComposedKit, type Composition, type KitDefinition, type KitList, kitDefinition } from "#internal/compositionTypes.ts";
|
|
6
6
|
import { type AnyEffect, type EffectHook, type ErrorOf, type Hook, type HookValue, hooked, type ServicesOf } from "#internal/hooked.ts";
|
|
7
|
-
import type { AfterResults, InferredDefinition
|
|
7
|
+
import type { AfterResults, InferredDefinition } from "#internal/hookInference.ts";
|
|
8
8
|
import type { LooseDefinition } from "#internal/seed.ts";
|
|
9
9
|
import { storyIt } from "#internal/storyIt.ts";
|
|
10
10
|
import { type Parts, trait, traits } from "#internal/traits.ts";
|
|
@@ -55,6 +55,7 @@ export interface StoryKitDefinition<
|
|
|
55
55
|
readonly layer?: Layer.Layer<TProvided, unknown, never>;
|
|
56
56
|
readonly name: string;
|
|
57
57
|
readonly outbound?: Readonly<Record<string, string>>;
|
|
58
|
+
readonly outboundPassthrough?: boolean;
|
|
58
59
|
readonly run?: RunHooks<TEngine, TStep, TFailed, TDiagnose>;
|
|
59
60
|
readonly runIf?: boolean;
|
|
60
61
|
readonly stages: readonly TStage[];
|
|
@@ -114,20 +115,6 @@ export function storyKit<
|
|
|
114
115
|
>(
|
|
115
116
|
definition: InferredDefinition<TCreate, TStage, TVerbs, TProvided, TAfter, TStep, TFailed, TDiagnose>,
|
|
116
117
|
): StoryKit<HookValue<ReturnType<TCreate>>, TStage, TVerbs, TStep | TFailed | TDiagnose, TProvided>;
|
|
117
|
-
// Keep explicit engine and effect type arguments accepted by existing kits.
|
|
118
|
-
export function storyKit<
|
|
119
|
-
TEngine,
|
|
120
|
-
const TStage extends string = never,
|
|
121
|
-
TVerbs extends object = object,
|
|
122
|
-
TProvided = never,
|
|
123
|
-
TCreate extends AnyEffect = never,
|
|
124
|
-
TAfter extends { readonly [TKey in TStage]?: AnyEffect } = Record<never, never>,
|
|
125
|
-
TStep extends AnyEffect = never,
|
|
126
|
-
TFailed extends AnyEffect = never,
|
|
127
|
-
TDiagnose extends AnyEffect = never,
|
|
128
|
-
>(
|
|
129
|
-
definition: LegacyDefinition<TEngine, TStage, TVerbs, TProvided, TCreate, TAfter, TStep, TFailed, TDiagnose>,
|
|
130
|
-
): StoryKit<TEngine, TStage, TVerbs, TStep | TFailed | TDiagnose, TProvided>;
|
|
131
118
|
export function storyKit<const TKits extends KitList>(definition: Composition<TKits>): ComposedKit<TKits>;
|
|
132
119
|
export function storyKit(input: LooseDefinition | Composition<KitList>): unknown {
|
|
133
120
|
const definition = "extends" in input ? compose(input.name, input.extends) : input;
|