@shivaedev/test-story 0.21.1 → 0.23.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 +24 -0
- package/README.md +258 -193
- package/dist/server/forwardingProxy.d.ts.map +1 -1
- package/dist/server/forwardingProxy.js +44 -7
- package/dist/server/forwardingProxy.js.map +1 -1
- package/dist/server/internal/compose.d.ts +4 -0
- package/dist/server/internal/compose.d.ts.map +1 -0
- package/dist/server/internal/compose.js +38 -0
- package/dist/server/internal/compose.js.map +1 -0
- package/dist/server/internal/compositionTypes.d.ts +37 -0
- package/dist/server/internal/compositionTypes.d.ts.map +1 -0
- package/dist/server/internal/compositionTypes.js +2 -0
- package/dist/server/internal/compositionTypes.js.map +1 -0
- package/dist/server/internal/documentRedirect.d.ts.map +1 -1
- package/dist/server/internal/documentRedirect.js +3 -1
- package/dist/server/internal/documentRedirect.js.map +1 -1
- package/dist/server/internal/documentTransport.d.ts.map +1 -1
- package/dist/server/internal/documentTransport.js +4 -1
- package/dist/server/internal/documentTransport.js.map +1 -1
- package/dist/server/internal/fakeHttpAnswer.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpAnswer.js +3 -2
- package/dist/server/internal/fakeHttpAnswer.js.map +1 -1
- package/dist/server/internal/fakeHttpControls.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpControls.js +2 -1
- 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 +5 -4
- package/dist/server/internal/fakeHttpReceive.js.map +1 -1
- package/dist/server/internal/fakeHttpSend.d.ts.map +1 -1
- package/dist/server/internal/fakeHttpSend.js +3 -2
- package/dist/server/internal/fakeHttpSend.js.map +1 -1
- package/dist/server/internal/hookInference.d.ts +3 -3
- package/dist/server/internal/hookInference.d.ts.map +1 -1
- package/dist/server/internal/screenElements.d.ts +1 -0
- package/dist/server/internal/screenElements.d.ts.map +1 -1
- package/dist/server/internal/screenElements.js +11 -0
- package/dist/server/internal/screenElements.js.map +1 -1
- package/dist/server/internal/storyIt.d.ts.map +1 -1
- package/dist/server/internal/storyIt.js +7 -5
- package/dist/server/internal/storyIt.js.map +1 -1
- package/dist/server/otlpReceiver.d.ts.map +1 -1
- package/dist/server/otlpReceiver.js +5 -4
- package/dist/server/otlpReceiver.js.map +1 -1
- package/dist/server/outcomes.d.ts +6 -0
- package/dist/server/outcomes.d.ts.map +1 -0
- package/dist/server/outcomes.js +13 -0
- package/dist/server/outcomes.js.map +1 -0
- package/dist/server/screenDriver.js +1 -1
- package/dist/server/screenDriver.js.map +1 -1
- package/dist/server/storyKit.d.ts +6 -3
- package/dist/server/storyKit.d.ts.map +1 -1
- package/dist/server/storyKit.js +5 -1
- package/dist/server/storyKit.js.map +1 -1
- package/package.json +8 -6
- package/src/server/forwardingProxy.ts +59 -23
- package/src/server/internal/compose.ts +45 -0
- package/src/server/internal/compositionTypes.ts +56 -0
- package/src/server/internal/documentRedirect.ts +4 -1
- package/src/server/internal/documentTransport.ts +12 -1
- package/src/server/internal/fakeHttpAnswer.ts +3 -2
- package/src/server/internal/fakeHttpControls.ts +2 -1
- package/src/server/internal/fakeHttpReceive.ts +5 -4
- package/src/server/internal/fakeHttpSend.ts +3 -2
- package/src/server/internal/hookInference.ts +3 -3
- package/src/server/internal/screenElements.ts +11 -0
- package/src/server/internal/storyIt.ts +9 -8
- package/src/server/otlpReceiver.ts +5 -4
- package/src/server/outcomes.ts +21 -0
- package/src/server/screenDriver.ts +1 -1
- package/src/server/storyKit.ts +11 -5
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { IncomingMessage, ServerResponse } from "node:http";
|
|
2
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
2
3
|
import type { FakeHttpRequestRecord, FakeHttpRoute, FakeHttpServiceOptions } from "#fakeHttpService.ts";
|
|
3
4
|
import { fakeHttpAnswer } from "#internal/fakeHttpAnswer.ts";
|
|
4
5
|
import type { fakeHttpControls } from "#internal/fakeHttpControls.ts";
|
|
@@ -91,17 +92,17 @@ export async function fakeHttpReceive(
|
|
|
91
92
|
tell(`fake HTTP received ${method} ${path}`);
|
|
92
93
|
if (!decodeParams(params)) {
|
|
93
94
|
tell(`fake HTTP rejected ${method} ${path} with 400`);
|
|
94
|
-
await fakeHttpSend(response, { status:
|
|
95
|
+
await fakeHttpSend(response, { status: HttpStatus.fromLiteral("BadRequest") });
|
|
95
96
|
return;
|
|
96
97
|
}
|
|
97
98
|
if ((options.guard && !options.guard(request)) || (found?.route.guard && !found.route.guard(request))) {
|
|
98
99
|
tell(`fake HTTP rejected ${method} ${path} with 401`);
|
|
99
|
-
await fakeHttpSend(response, { status:
|
|
100
|
+
await fakeHttpSend(response, { status: HttpStatus.fromLiteral("Unauthorized") });
|
|
100
101
|
return;
|
|
101
102
|
}
|
|
102
103
|
if (!found) {
|
|
103
104
|
tell(`fake HTTP answered ${method} ${path} with 404`);
|
|
104
|
-
await fakeHttpSend(response, { status:
|
|
105
|
+
await fakeHttpSend(response, { status: HttpStatus.fromLiteral("NotFound") });
|
|
105
106
|
return;
|
|
106
107
|
}
|
|
107
108
|
await fakeHttpAnswer(found.name, found.route, request, response, controls, tell);
|
|
@@ -110,7 +111,7 @@ export async function fakeHttpReceive(
|
|
|
110
111
|
if (response.headersSent) {
|
|
111
112
|
response.destroy();
|
|
112
113
|
} else {
|
|
113
|
-
await fakeHttpSend(response, { status:
|
|
114
|
+
await fakeHttpSend(response, { status: HttpStatus.fromLiteral("InternalServerError") });
|
|
114
115
|
}
|
|
115
116
|
}
|
|
116
117
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { ServerResponse } from "node:http";
|
|
2
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
2
3
|
import type { FakeHttpFault, FakeHttpReply } from "#fakeHttpService.ts";
|
|
3
4
|
|
|
4
5
|
export function fakeHttpConnection(response: ServerResponse) {
|
|
@@ -41,7 +42,7 @@ export async function fakeHttpSend(response: ServerResponse, reply: FakeHttpRepl
|
|
|
41
42
|
}
|
|
42
43
|
const connection = fakeHttpConnection(response);
|
|
43
44
|
const body = replyBody(reply.body);
|
|
44
|
-
response.writeHead(reply.status ??
|
|
45
|
+
response.writeHead(reply.status ?? HttpStatus.fromLiteral("OK"), { "content-type": "application/json", ...reply.headers });
|
|
45
46
|
if (pause) {
|
|
46
47
|
response.flushHeaders();
|
|
47
48
|
await connection.pause(pause());
|
|
@@ -56,7 +57,7 @@ export async function fakeHttpSend(response: ServerResponse, reply: FakeHttpRepl
|
|
|
56
57
|
|
|
57
58
|
export async function fakeHttpStrike(response: ServerResponse, fault: Exclude<FakeHttpFault, { kind: "status" }>, reply: FakeHttpReply = {}) {
|
|
58
59
|
if (fault.kind === "reset") {
|
|
59
|
-
response.writeHead(reply.status ??
|
|
60
|
+
response.writeHead(reply.status ?? HttpStatus.fromLiteral("OK"), { "content-type": "application/json", ...reply.headers });
|
|
60
61
|
response.flushHeaders();
|
|
61
62
|
await new Promise<void>((resolve) => response.write("", () => resolve()));
|
|
62
63
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { Effect, Layer, Scope } from "effect";
|
|
1
|
+
import type { Crypto, Effect, Layer, Scope } from "effect";
|
|
2
2
|
import type { AnyEffect, Hook, HookEffects, HookValue, ServicesOf } from "#internal/hooked.ts";
|
|
3
3
|
import type { StoryKitDefinition } from "#storyKit.ts";
|
|
4
4
|
|
|
@@ -6,9 +6,9 @@ export type AfterResults<TStage extends string> = {
|
|
|
6
6
|
readonly [TKey in TStage]?: Generator<AnyEffect, void, never> | Effect.Effect<void, unknown, unknown> | void;
|
|
7
7
|
};
|
|
8
8
|
|
|
9
|
-
type LayerFor<THooks, TProvided> = [Exclude<ServicesOf<THooks>, Scope.Scope>] extends [TProvided]
|
|
9
|
+
type LayerFor<THooks, TProvided> = [Exclude<ServicesOf<THooks>, Scope.Scope | Crypto.Crypto>] extends [TProvided]
|
|
10
10
|
? unknown
|
|
11
|
-
: { readonly layer: Layer.Layer<Exclude<ServicesOf<THooks>, Scope.Scope>, unknown, never> };
|
|
11
|
+
: { readonly layer: Layer.Layer<Exclude<ServicesOf<THooks>, Scope.Scope | Crypto.Crypto>, unknown, never> };
|
|
12
12
|
|
|
13
13
|
export type InferredDefinition<
|
|
14
14
|
TCreate extends () => unknown,
|
|
@@ -35,6 +35,16 @@ export function screenElements(root: () => HTMLElement) {
|
|
|
35
35
|
throw new Error(`The screen's ${targetName(selector)} is disabled.\nhelp: Wait for the app to enable the control before acting on it.`);
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
|
+
function text(found: HTMLElement): string {
|
|
39
|
+
const walker = found.ownerDocument.createTreeWalker(found, NodeFilter.SHOW_TEXT);
|
|
40
|
+
const parts: string[] = [];
|
|
41
|
+
for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
|
|
42
|
+
if (node.parentElement instanceof HTMLElement && isRendered(node.parentElement)) {
|
|
43
|
+
parts.push(node.textContent ?? "");
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return parts.join("");
|
|
47
|
+
}
|
|
38
48
|
return {
|
|
39
49
|
element,
|
|
40
50
|
enabled,
|
|
@@ -60,5 +70,6 @@ export function screenElements(root: () => HTMLElement) {
|
|
|
60
70
|
},
|
|
61
71
|
input,
|
|
62
72
|
rendered,
|
|
73
|
+
text,
|
|
63
74
|
};
|
|
64
75
|
}
|
|
@@ -1,5 +1,7 @@
|
|
|
1
|
+
import { NodeCrypto } from "@effect/platform-node";
|
|
1
2
|
import type { TestContext } from "@effect/vitest";
|
|
2
3
|
import { Effect, Layer } from "effect";
|
|
4
|
+
import * as TestCrypto from "effect/testing/TestCrypto";
|
|
3
5
|
import { makeEffectIt } from "@shivaedev/test/makeEffectIt.ts";
|
|
4
6
|
import type { EffectTest, EffectTestOptions } from "@shivaedev/test/types.ts";
|
|
5
7
|
import { type Genre, genreOf } from "#internal/genre.ts";
|
|
@@ -54,14 +56,13 @@ export function storyIt(definition: LooseDefinition): LooseIt {
|
|
|
54
56
|
const genre = genreOf(definition.name);
|
|
55
57
|
const outbound = definition.outbound ?? (Reflect.get(globalThis, Symbol.for("test-story.dom")) ? {} : undefined);
|
|
56
58
|
const { effectApp } = makeEffectIt({
|
|
57
|
-
around:
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
}),
|
|
59
|
+
around: (effect) =>
|
|
60
|
+
Effect.gen(function* () {
|
|
61
|
+
if (outbound !== undefined) {
|
|
62
|
+
yield* routeOutbound(outbound);
|
|
63
|
+
}
|
|
64
|
+
return yield* definition.around?.(effect) ?? effect;
|
|
65
|
+
}).pipe(Effect.provide(TestCrypto.layer(0).pipe(Layer.provide(NodeCrypto.layer)))),
|
|
65
66
|
clock: definition.clock,
|
|
66
67
|
layer: definition.layer ?? Layer.empty,
|
|
67
68
|
makeHarness: () => Effect.void,
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createServer, type IncomingHttpHeaders, type IncomingMessage, type ServerResponse } from "node:http";
|
|
2
2
|
import { Effect } from "effect";
|
|
3
|
+
import * as HttpStatus from "effect/http/HttpStatus";
|
|
3
4
|
import { loopbackServer } from "#internal/loopback.ts";
|
|
4
5
|
import { otlpReadout } from "#internal/otlpReadout.ts";
|
|
5
6
|
|
|
@@ -25,16 +26,16 @@ async function receive(request: IncomingMessage, response: ServerResponse, reque
|
|
|
25
26
|
const body = Buffer.concat(chunks);
|
|
26
27
|
requests.push({ body, headers: request.headers, path: request.url, rawHeaders: request.rawHeaders });
|
|
27
28
|
if (request.method !== "POST" || request.url !== (options.path ?? "/v1/traces")) {
|
|
28
|
-
response.writeHead(
|
|
29
|
+
response.writeHead(HttpStatus.fromLiteral("NotFound")).end();
|
|
29
30
|
return;
|
|
30
31
|
}
|
|
31
32
|
const contentType = request.headers["content-type"]?.split(";")[0]?.trim();
|
|
32
33
|
if (contentType !== "application/json" && contentType !== "application/x-protobuf") {
|
|
33
|
-
response.writeHead(
|
|
34
|
+
response.writeHead(HttpStatus.fromLiteral("UnsupportedMediaType")).end();
|
|
34
35
|
return;
|
|
35
36
|
}
|
|
36
37
|
spans.push(...otlpReadout(body, contentType));
|
|
37
|
-
const status = typeof options.status === "function" ? options.status(requests.length) : (options.status ??
|
|
38
|
+
const status = typeof options.status === "function" ? options.status(requests.length) : (options.status ?? HttpStatus.fromLiteral("OK"));
|
|
38
39
|
if (status === undefined) {
|
|
39
40
|
return;
|
|
40
41
|
}
|
|
@@ -45,7 +46,7 @@ async function receive(request: IncomingMessage, response: ServerResponse, reque
|
|
|
45
46
|
response.end(options.answer ?? "{}");
|
|
46
47
|
}
|
|
47
48
|
} catch {
|
|
48
|
-
response.writeHead(
|
|
49
|
+
response.writeHead(HttpStatus.fromLiteral("BadRequest")).end();
|
|
49
50
|
}
|
|
50
51
|
}
|
|
51
52
|
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { Effect } from "effect";
|
|
2
|
+
import { type DeeplyAllowMatchers, expect } from "vitest";
|
|
3
|
+
|
|
4
|
+
type AnyReadout = Effect.Effect<unknown, unknown, unknown>;
|
|
5
|
+
|
|
6
|
+
export const outcomes = Effect.fn("Story.outcomes")(check);
|
|
7
|
+
|
|
8
|
+
function check<TReadouts extends Record<string, AnyReadout>>(
|
|
9
|
+
rows: {
|
|
10
|
+
readonly [TKey in keyof TReadouts]: readonly [TReadouts[TKey], DeeplyAllowMatchers<Effect.Success<NoInfer<TReadouts[TKey]>>>];
|
|
11
|
+
},
|
|
12
|
+
): Effect.fn.Return<void, Effect.Error<TReadouts[keyof TReadouts]>, Effect.Services<TReadouts[keyof TReadouts]>>;
|
|
13
|
+
function* check(rows: Readonly<Record<string, readonly [AnyReadout, unknown]>>) {
|
|
14
|
+
const actual: Record<string, unknown> = {};
|
|
15
|
+
const expected: Record<string, unknown> = {};
|
|
16
|
+
for (const [label, [readout, value]] of Object.entries(rows)) {
|
|
17
|
+
actual[label] = yield* readout;
|
|
18
|
+
expected[label] = value;
|
|
19
|
+
}
|
|
20
|
+
expect(actual).toEqual(expected);
|
|
21
|
+
}
|
|
@@ -101,7 +101,7 @@ function makeScreenDriver({ root, pending, tell, act = (action) => action(), tim
|
|
|
101
101
|
settle,
|
|
102
102
|
submit: (selector: ScreenTarget = "form") => perform(`submit ${targetName(selector)}`, () => elements.form(selector).requestSubmit()),
|
|
103
103
|
text: (selector?: ScreenTarget) => {
|
|
104
|
-
const text = (selector === undefined ? elements.rendered(currentRoot(), "root") : elements.element(selector))
|
|
104
|
+
const text = elements.text(selector === undefined ? elements.rendered(currentRoot(), "root") : elements.element(selector));
|
|
105
105
|
tell(`read ${selector === undefined ? "the screen" : targetName(selector)}: ${JSON.stringify(text)}`);
|
|
106
106
|
return text;
|
|
107
107
|
},
|
package/src/server/storyKit.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import type { TestContext } from "@effect/vitest";
|
|
2
|
-
import type { Effect, Layer, Scope } from "effect";
|
|
2
|
+
import type { Crypto, Effect, Layer, Scope } from "effect";
|
|
3
3
|
import type { EffectClock, EffectTestOptions } from "@shivaedev/test/types.ts";
|
|
4
|
+
import { compose } from "#internal/compose.ts";
|
|
5
|
+
import { type ComposedKit, type Composition, type KitDefinition, type KitList, kitDefinition } from "#internal/compositionTypes.ts";
|
|
4
6
|
import { type AnyEffect, type EffectHook, type ErrorOf, type Hook, type HookValue, hooked, type ServicesOf } from "#internal/hooked.ts";
|
|
5
7
|
import type { AfterResults, InferredDefinition, LegacyDefinition } from "#internal/hookInference.ts";
|
|
6
8
|
import type { LooseDefinition } from "#internal/seed.ts";
|
|
@@ -46,7 +48,7 @@ export interface StoryKitDefinition<
|
|
|
46
48
|
TDiagnose extends AnyEffect,
|
|
47
49
|
> {
|
|
48
50
|
readonly after?: { readonly [TKey in keyof TAfter]: EffectHook<[engine: TEngine], TAfter[TKey] & AnyEffect, void> };
|
|
49
|
-
readonly around?: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, unknown, R | NoInfer<TProvided> | Scope.Scope>;
|
|
51
|
+
readonly around?: <A, E, R>(effect: Effect.Effect<A, E, R>) => Effect.Effect<A, unknown, R | NoInfer<TProvided> | Crypto.Crypto | Scope.Scope>;
|
|
50
52
|
readonly clock?: EffectClock;
|
|
51
53
|
readonly create: EffectHook<[], TCreate, TEngine>;
|
|
52
54
|
readonly inspect?: (engine: TEngine) => unknown;
|
|
@@ -67,10 +69,10 @@ export type StoryBody<TEngine, TVerbs, TRun extends AnyEffect, TEffect extends A
|
|
|
67
69
|
) => Generator<TEffect, unknown, never> | undefined;
|
|
68
70
|
|
|
69
71
|
export type StoryTest<TEngine, TStage extends string, TVerbs, TRun extends AnyEffect, TProvided> = <
|
|
70
|
-
TEffect extends Needs<TProvided | Scope.Scope> = never,
|
|
72
|
+
TEffect extends Needs<TProvided | Crypto.Crypto | Scope.Scope> = never,
|
|
71
73
|
>(
|
|
72
74
|
name: string,
|
|
73
|
-
given: readonly Trait<TEngine, TStage, TProvided | Scope.Scope>[],
|
|
75
|
+
given: readonly Trait<TEngine, TStage, TProvided | Crypto.Crypto | Scope.Scope>[],
|
|
74
76
|
body?: StoryBody<TEngine, TVerbs, TRun, TEffect>,
|
|
75
77
|
options?: number | EffectTestOptions,
|
|
76
78
|
) => void;
|
|
@@ -97,6 +99,7 @@ export interface StoryKit<TEngine, TStage extends string, TVerbs, TRun extends A
|
|
|
97
99
|
readonly traits: <const TGiven extends readonly Trait<TEngine, TStage, unknown>[]>(
|
|
98
100
|
...given: TGiven
|
|
99
101
|
) => Trait<TEngine, TStage, TraitServices<TGiven[number]>>;
|
|
102
|
+
readonly [kitDefinition]: KitDefinition<TEngine, TStage, TVerbs, TRun, TProvided>;
|
|
100
103
|
}
|
|
101
104
|
|
|
102
105
|
export function storyKit<
|
|
@@ -125,8 +128,11 @@ export function storyKit<
|
|
|
125
128
|
>(
|
|
126
129
|
definition: LegacyDefinition<TEngine, TStage, TVerbs, TProvided, TCreate, TAfter, TStep, TFailed, TDiagnose>,
|
|
127
130
|
): StoryKit<TEngine, TStage, TVerbs, TStep | TFailed | TDiagnose, TProvided>;
|
|
128
|
-
export function storyKit(definition:
|
|
131
|
+
export function storyKit<const TKits extends KitList>(definition: Composition<TKits>): ComposedKit<TKits>;
|
|
132
|
+
export function storyKit(input: LooseDefinition | Composition<KitList>): unknown {
|
|
133
|
+
const definition = "extends" in input ? compose(input.name, input.extends) : input;
|
|
129
134
|
return {
|
|
135
|
+
[kitDefinition]: definition,
|
|
130
136
|
it: storyIt(definition),
|
|
131
137
|
trait: (stage: string, line: string, apply: EffectHook<[engine: unknown], AnyEffect, void>) =>
|
|
132
138
|
trait(stage, line, (engine: unknown) => hooked(() => apply(engine))),
|