@apifuse/provider-sdk 2.2.0-beta.2 → 2.2.0-beta.21
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/AUTHORING.md +487 -0
- package/CHANGELOG.md +90 -0
- package/README.md +49 -3
- package/SUBMISSION.md +1 -1
- package/bin/apifuse-check.ts +44 -59
- package/bin/apifuse-create.ts +1 -1
- package/bin/apifuse-dev.ts +27 -52
- package/bin/apifuse-pack-check.ts +14 -0
- package/bin/apifuse-pack-smoke.ts +36 -81
- package/bin/apifuse-pack-types.ts +305 -0
- package/bin/apifuse-perf.ts +45 -127
- package/bin/apifuse-record.ts +659 -111
- package/bin/apifuse-submit-check.ts +546 -48
- package/bin/apifuse-sync-assets.ts +117 -0
- package/bin/apifuse.ts +1 -1
- package/bin/submit-check-delimited-text.ts +50 -0
- package/bin/submit-check-xml-semantics.ts +204 -0
- package/bin/submit-check-xml.ts +134 -0
- package/dist/auth-turn/index.d.ts +3 -3
- package/dist/auth.d.ts +2 -2
- package/dist/auth.js +9 -18
- package/dist/ceremonies/index.d.ts +9 -1
- package/dist/ceremonies/index.js +65 -18
- package/dist/cli/commands.d.ts +1 -1
- package/dist/cli/commands.js +8 -0
- package/dist/cli/create.d.ts +3 -0
- package/dist/cli/create.js +34 -35
- package/dist/cli/prompt-assets.d.ts +80 -0
- package/dist/cli/prompt-assets.js +743 -0
- package/dist/cli/templates/provider/AGENTS.md.tpl +17 -8
- package/dist/config/loader.d.ts +176 -8
- package/dist/config/loader.js +424 -95
- package/dist/contract-serialization.d.ts +2 -2
- package/dist/contract-serialization.js +3 -6
- package/dist/contract-types.d.ts +2 -2
- package/dist/contract.d.ts +3 -3
- package/dist/contract.js +4 -6
- package/dist/define.d.ts +9 -1
- package/dist/define.js +304 -119
- package/dist/dev.d.ts +1 -1
- package/dist/dev.js +1 -1
- package/dist/error-resolution.d.ts +2 -0
- package/dist/error-resolution.js +90 -0
- package/dist/errors.d.ts +22 -1
- package/dist/errors.js +88 -0
- package/dist/fixture-sanitization.d.ts +26 -0
- package/dist/fixture-sanitization.js +216 -0
- package/dist/i18n/catalog.d.ts +2 -2
- package/dist/i18n/catalog.js +4 -10
- package/dist/i18n/index.d.ts +2 -2
- package/dist/i18n/index.js +2 -2
- package/dist/i18n/keys.d.ts +2 -2
- package/dist/index.d.ts +46 -42
- package/dist/index.js +41 -37
- package/dist/lint.d.ts +1 -1
- package/dist/lint.js +8 -15
- package/dist/native-address.d.ts +43 -0
- package/dist/native-address.js +281 -0
- package/dist/native-egress-policy.d.ts +31 -0
- package/dist/native-egress-policy.js +288 -0
- package/dist/observability.d.ts +5 -2
- package/dist/observability.js +48 -1
- package/dist/provider.d.ts +12 -11
- package/dist/provider.js +10 -9
- package/dist/public-schema-field-lint.d.ts +1 -1
- package/dist/recipes/gov-api.js +1 -1
- package/dist/runtime/auth-flow.d.ts +2 -1
- package/dist/runtime/auth-flow.js +4 -3
- package/dist/runtime/browser.d.ts +1 -1
- package/dist/runtime/browser.js +15 -29
- package/dist/runtime/cache.d.ts +1 -1
- package/dist/runtime/cache.js +4 -8
- package/dist/runtime/choice.d.ts +1 -1
- package/dist/runtime/choice.js +31 -35
- package/dist/runtime/credential.d.ts +1 -1
- package/dist/runtime/credential.js +1 -1
- package/dist/runtime/env.d.ts +1 -1
- package/dist/runtime/executor.d.ts +1 -1
- package/dist/runtime/executor.js +31 -4
- package/dist/runtime/http.d.ts +2 -2
- package/dist/runtime/http.js +387 -47
- package/dist/runtime/insights.d.ts +1 -1
- package/dist/runtime/insights.js +6 -13
- package/dist/runtime/instrumentation.d.ts +2 -2
- package/dist/runtime/instrumentation.js +345 -22
- package/dist/runtime/keyring.js +1 -1
- package/dist/runtime/namespace.js +1 -1
- package/dist/runtime/native-network.d.ts +127 -0
- package/dist/runtime/native-network.js +1298 -0
- package/dist/runtime/otlp.d.ts +1 -1
- package/dist/runtime/perf.d.ts +1 -1
- package/dist/runtime/provider.d.ts +1 -1
- package/dist/runtime/provider.js +1 -2
- package/dist/runtime/proxy-errors.d.ts +1 -1
- package/dist/runtime/proxy-errors.js +9 -7
- package/dist/runtime/proxy-nodemaven.d.ts +56 -0
- package/dist/runtime/proxy-nodemaven.js +146 -0
- package/dist/runtime/proxy-retry-policy.d.ts +2 -2
- package/dist/runtime/proxy-retry-policy.js +2 -2
- package/dist/runtime/proxy-telemetry.d.ts +2 -1
- package/dist/runtime/proxy-telemetry.js +55 -52
- package/dist/runtime/redirects.d.ts +29 -0
- package/dist/runtime/redirects.js +36 -0
- package/dist/runtime/redis.d.ts +1 -1
- package/dist/runtime/redis.js +2 -4
- package/dist/runtime/request-options.d.ts +68 -1
- package/dist/runtime/request-options.js +548 -0
- package/dist/runtime/secrets.d.ts +27 -0
- package/dist/runtime/secrets.js +51 -0
- package/dist/runtime/state.d.ts +2 -2
- package/dist/runtime/state.js +238 -26
- package/dist/runtime/stealth.d.ts +5 -3
- package/dist/runtime/stealth.js +423 -143
- package/dist/runtime/stt.d.ts +1 -1
- package/dist/runtime/stt.js +11 -15
- package/dist/runtime/trace.d.ts +2 -2
- package/dist/runtime/trace.js +2 -4
- package/dist/runtime/waterfall.d.ts +1 -1
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +7 -15
- package/dist/serve.d.ts +1 -1
- package/dist/serve.js +1 -1
- package/dist/server/index.d.ts +7 -7
- package/dist/server/index.js +6 -6
- package/dist/server/self-test-input-tokens.d.ts +2 -1
- package/dist/server/self-test-input-tokens.js +18 -14
- package/dist/server/self-test-redaction.d.ts +1 -1
- package/dist/server/self-test-redaction.js +1 -1
- package/dist/server/self-test.d.ts +104 -3
- package/dist/server/self-test.js +673 -115
- package/dist/server/serve.d.ts +116 -4
- package/dist/server/serve.js +799 -128
- package/dist/server/types.d.ts +34 -9
- package/dist/server/types.js +8 -1
- package/dist/stateful/errors.d.ts +14 -0
- package/dist/stateful/errors.js +14 -0
- package/dist/stateful/http-provider-event-emitter.d.ts +40 -0
- package/dist/stateful/http-provider-event-emitter.js +237 -0
- package/dist/stateful/http-session-owner-registry.d.ts +44 -0
- package/dist/stateful/http-session-owner-registry.js +210 -0
- package/dist/stateful/index.d.ts +18 -0
- package/dist/stateful/index.js +18 -0
- package/dist/stateful/provider-event-delivery-failures.d.ts +32 -0
- package/dist/stateful/provider-event-delivery-failures.js +43 -0
- package/dist/stateful/provider-event-pipeline-metrics.d.ts +46 -0
- package/dist/stateful/provider-event-pipeline-metrics.js +48 -0
- package/dist/stateful/provider-event-pipeline.d.ts +50 -0
- package/dist/stateful/provider-event-pipeline.js +1 -0
- package/dist/stateful/provider-events.d.ts +101 -0
- package/dist/stateful/provider-events.js +289 -0
- package/dist/stateful/session-key.d.ts +15 -0
- package/dist/stateful/session-key.js +86 -0
- package/dist/stateful/stateful-provider-adapter-context.d.ts +5 -0
- package/dist/stateful/stateful-provider-adapter-context.js +42 -0
- package/dist/stateful/stateful-provider-adapter-metrics.d.ts +15 -0
- package/dist/stateful/stateful-provider-adapter-metrics.js +21 -0
- package/dist/stateful/stateful-provider-adapter.d.ts +98 -0
- package/dist/stateful/stateful-provider-adapter.js +287 -0
- package/dist/stateful/stateful-provider-observability.d.ts +62 -0
- package/dist/stateful/stateful-provider-observability.js +161 -0
- package/dist/stateful/stateful-provider-owner-forwarder.d.ts +41 -0
- package/dist/stateful/stateful-provider-owner-forwarder.js +215 -0
- package/dist/stateful/stateful-provider-runtime-context.d.ts +32 -0
- package/dist/stateful/stateful-provider-runtime-context.js +60 -0
- package/dist/stateful/stateful-provider-runtime-executor.d.ts +34 -0
- package/dist/stateful/stateful-provider-runtime-executor.js +52 -0
- package/dist/stateful/stateful-provider-session-routing.d.ts +71 -0
- package/dist/stateful/stateful-provider-session-routing.js +353 -0
- package/dist/stateful/stateful-provider-session-runtime.d.ts +98 -0
- package/dist/stateful/stateful-provider-session-runtime.js +245 -0
- package/dist/stateful-signing.d.ts +18 -0
- package/dist/stateful-signing.js +27 -0
- package/dist/stealth/profiles.d.ts +1 -1
- package/dist/stealth/profiles.js +5 -14
- package/dist/stream-evidence.d.ts +74 -0
- package/dist/stream-evidence.js +785 -0
- package/dist/stream.d.ts +1 -1
- package/dist/testing/index.d.ts +2 -2
- package/dist/testing/index.js +2 -2
- package/dist/testing/run.d.ts +32 -2
- package/dist/testing/run.js +478 -28
- package/dist/types.d.ts +342 -14
- package/dist/types.js +1 -0
- package/dist/user-input.d.ts +30 -0
- package/dist/user-input.js +66 -0
- package/package.json +16 -5
- package/src/auth-turn/index.ts +1 -1
- package/src/auth.ts +28 -86
- package/src/ceremonies/index.ts +103 -78
- package/src/cli/commands.ts +10 -0
- package/src/cli/create.ts +42 -35
- package/src/cli/prompt-assets.ts +865 -0
- package/src/cli/templates/provider/AGENTS.md.tpl +17 -8
- package/src/config/loader.ts +652 -204
- package/src/contract-serialization.ts +5 -11
- package/src/contract-types.ts +2 -2
- package/src/contract.ts +12 -28
- package/src/define.ts +499 -498
- package/src/dev.ts +4 -9
- package/src/error-resolution.ts +91 -0
- package/src/errors.ts +110 -5
- package/src/fixture-sanitization.ts +247 -0
- package/src/i18n/catalog.ts +10 -32
- package/src/i18n/index.ts +2 -2
- package/src/i18n/keys.ts +5 -11
- package/src/index.ts +112 -42
- package/src/lint.ts +88 -152
- package/src/native-address.ts +340 -0
- package/src/native-egress-policy.ts +358 -0
- package/src/observability.ts +51 -1
- package/src/provider.ts +66 -11
- package/src/public-schema-field-lint.ts +7 -33
- package/src/recipes/gov-api.ts +2 -5
- package/src/runtime/auth-flow.ts +7 -7
- package/src/runtime/browser.ts +64 -187
- package/src/runtime/cache.ts +20 -67
- package/src/runtime/choice.ts +79 -132
- package/src/runtime/credential.ts +2 -2
- package/src/runtime/env.ts +1 -1
- package/src/runtime/executor.ts +43 -20
- package/src/runtime/http.ts +494 -57
- package/src/runtime/insights.ts +15 -53
- package/src/runtime/instrumentation.ts +495 -66
- package/src/runtime/keyring.ts +7 -19
- package/src/runtime/namespace.ts +2 -7
- package/src/runtime/native-network.ts +1686 -0
- package/src/runtime/otlp.ts +12 -23
- package/src/runtime/perf.ts +1 -1
- package/src/runtime/provider.ts +4 -9
- package/src/runtime/proxy-errors.ts +29 -42
- package/src/runtime/proxy-nodemaven.ts +221 -0
- package/src/runtime/proxy-retry-policy.ts +3 -3
- package/src/runtime/proxy-telemetry.ts +79 -77
- package/src/runtime/redirects.ts +66 -0
- package/src/runtime/redis.ts +4 -12
- package/src/runtime/request-options.ts +679 -9
- package/src/runtime/secrets.ts +64 -0
- package/src/runtime/state.ts +353 -133
- package/src/runtime/stealth.ts +505 -154
- package/src/runtime/stt.ts +38 -94
- package/src/runtime/trace.ts +14 -44
- package/src/runtime/waterfall.ts +5 -18
- package/src/schema.ts +23 -84
- package/src/serve.ts +1 -1
- package/src/server/index.ts +29 -7
- package/src/server/self-test-input-tokens.ts +29 -14
- package/src/server/self-test-redaction.ts +2 -2
- package/src/server/self-test.ts +857 -132
- package/src/server/serve.ts +1151 -328
- package/src/server/types.ts +12 -13
- package/src/stateful/README.md +146 -0
- package/src/stateful/errors.ts +23 -0
- package/src/stateful/http-provider-event-emitter.ts +314 -0
- package/src/stateful/http-session-owner-registry.ts +306 -0
- package/src/stateful/index.ts +18 -0
- package/src/stateful/provider-event-delivery-failures.ts +80 -0
- package/src/stateful/provider-event-pipeline-metrics.ts +95 -0
- package/src/stateful/provider-event-pipeline.ts +61 -0
- package/src/stateful/provider-events.ts +462 -0
- package/src/stateful/session-key.ts +111 -0
- package/src/stateful/stateful-provider-adapter-context.ts +59 -0
- package/src/stateful/stateful-provider-adapter-metrics.ts +48 -0
- package/src/stateful/stateful-provider-adapter.ts +562 -0
- package/src/stateful/stateful-provider-observability.ts +261 -0
- package/src/stateful/stateful-provider-owner-forwarder.ts +287 -0
- package/src/stateful/stateful-provider-runtime-context.ts +92 -0
- package/src/stateful/stateful-provider-runtime-executor.ts +96 -0
- package/src/stateful/stateful-provider-session-routing.ts +555 -0
- package/src/stateful/stateful-provider-session-runtime.ts +403 -0
- package/src/stateful-signing.ts +46 -0
- package/src/stealth/profiles.ts +10 -26
- package/src/stream-evidence.ts +988 -0
- package/src/stream.ts +8 -19
- package/src/testing/index.ts +10 -2
- package/src/testing/run.ts +653 -74
- package/src/types.ts +408 -28
- package/src/user-input.ts +118 -0
- package/dist/cli/templates/provider/CLAUDE.md.tpl +0 -1
- package/src/cli/templates/provider/CLAUDE.md.tpl +0 -1
- /package/dist/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
- /package/dist/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/fixtures-and-recording/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/health-checks-and-fail-closed/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/normalization-standards/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/pagination-and-counts/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-contract-verification/SKILL.md.tpl +0 -0
- /package/src/cli/templates/provider/{skills → .agents/skills}/upstream-notes/README.md.tpl +0 -0
package/dist/stream.d.ts
CHANGED
package/dist/testing/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { describeTransform, snapshotTransform, toMatchShape } from "./helpers";
|
|
2
|
-
export { runStandardTests } from "./run";
|
|
1
|
+
export { describeTransform, snapshotTransform, toMatchShape } from "./helpers.js";
|
|
2
|
+
export { runStandardTests, type StandardTestsManifest, type StandardTestsOptions, type StandardTestsResult, type StandardTestsUpstreamCall, type StandardTestsUpstreamResponse, type StandardTestsUpstreamStub, } from "./run.js";
|
package/dist/testing/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { describeTransform, snapshotTransform, toMatchShape } from "./helpers";
|
|
2
|
-
export { runStandardTests } from "./run";
|
|
1
|
+
export { describeTransform, snapshotTransform, toMatchShape } from "./helpers.js";
|
|
2
|
+
export { runStandardTests, } from "./run.js";
|
package/dist/testing/run.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AuthMode, ProviderDefinition } from "../types";
|
|
1
|
+
import type { AuthMode, ProviderContext, ProviderDefinition } from "../types.js";
|
|
2
2
|
export interface StandardTestsManifest {
|
|
3
3
|
id?: string;
|
|
4
4
|
displayName?: string;
|
|
@@ -22,7 +22,34 @@ export interface StandardTestsOptions {
|
|
|
22
22
|
validateAuthMode?: boolean;
|
|
23
23
|
/** Override inferred __fixtures__ directory for tests generated outside providers/<id>. */
|
|
24
24
|
fixtureDir?: string;
|
|
25
|
+
/** Opt in to real-handler E2E with strict, offline canned upstream responses. */
|
|
26
|
+
upstreamStub?: StandardTestsUpstreamStub;
|
|
27
|
+
/** Require a committed snapshot; missing files fail unless --update-snapshots is passed. */
|
|
28
|
+
requireSnapshot?: boolean;
|
|
25
29
|
}
|
|
30
|
+
export interface StandardTestsUpstreamCall {
|
|
31
|
+
/** Operation whose real handler initiated the call. */
|
|
32
|
+
operationName: string;
|
|
33
|
+
/** ProviderContext transport surface used by the handler. */
|
|
34
|
+
transport: "http" | "stealth" | "browser" | "native";
|
|
35
|
+
method: string;
|
|
36
|
+
url?: string;
|
|
37
|
+
body?: unknown;
|
|
38
|
+
options?: unknown;
|
|
39
|
+
}
|
|
40
|
+
export interface StandardTestsUpstreamResponse {
|
|
41
|
+
status?: number;
|
|
42
|
+
headers?: Readonly<Record<string, string>>;
|
|
43
|
+
/** JSON-compatible values are encoded as JSON; strings and bytes are preserved. */
|
|
44
|
+
body?: unknown;
|
|
45
|
+
}
|
|
46
|
+
export type StandardTestsUpstreamStub = (call: StandardTestsUpstreamCall) => Response | StandardTestsUpstreamResponse | undefined | Promise<Response | StandardTestsUpstreamResponse | undefined>;
|
|
47
|
+
export interface StandardTestsResult {
|
|
48
|
+
warnings: readonly string[];
|
|
49
|
+
}
|
|
50
|
+
export declare function createSnapshotContext(rawFixture: unknown): ProviderContext;
|
|
51
|
+
/** Internal execution seam exported for focused SDK tests; use runStandardTests as public API. */
|
|
52
|
+
export declare function executeStandardTestHandler(provider: ProviderDefinition, operationName: string, upstreamStub: StandardTestsUpstreamStub): Promise<unknown>;
|
|
26
53
|
/**
|
|
27
54
|
* Run standard SDK tests for a provider in one line.
|
|
28
55
|
*
|
|
@@ -31,4 +58,7 @@ export interface StandardTestsOptions {
|
|
|
31
58
|
* import { runStandardTests } from "@apifuse/provider-sdk/testing";
|
|
32
59
|
* runStandardTests(myProvider, rawFixture, manifest, { snapshot: true });
|
|
33
60
|
*/
|
|
34
|
-
export declare function runStandardTests(provider: ProviderDefinition,
|
|
61
|
+
export declare function runStandardTests(provider: ProviderDefinition, options: StandardTestsOptions & {
|
|
62
|
+
upstreamStub: StandardTestsUpstreamStub;
|
|
63
|
+
}): StandardTestsResult;
|
|
64
|
+
export declare function runStandardTests(provider: ProviderDefinition, rawFixture?: unknown, manifest?: StandardTestsManifest, options?: StandardTestsOptions): StandardTestsResult;
|
package/dist/testing/run.js
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
import { describe, expect, it } from "bun:test";
|
|
2
|
-
import { createProviderCache } from "../runtime/cache";
|
|
3
|
-
import { createTestProviderChoiceContext } from "../runtime/choice";
|
|
4
|
-
import { createMemoryProviderRuntimeState } from "../runtime/state";
|
|
5
|
-
import { createUnsupportedSttClient } from "../runtime/stt";
|
|
6
|
-
import {
|
|
2
|
+
import { createProviderCache } from "../runtime/cache.js";
|
|
3
|
+
import { createTestProviderChoiceContext } from "../runtime/choice.js";
|
|
4
|
+
import { createMemoryProviderRuntimeState } from "../runtime/state.js";
|
|
5
|
+
import { createUnsupportedSttClient } from "../runtime/stt.js";
|
|
6
|
+
import { createNativeEgressAuthorization, NativeNetworkError, snapshotNativeConnectInput, snapshotNativeGrantInput, } from "../runtime/native-network.js";
|
|
7
|
+
import { safeParseSchemaSync } from "../schema.js";
|
|
8
|
+
import { requestPathForFixture } from "../fixture-sanitization.js";
|
|
9
|
+
import { findStreamCaptureGroup, replayStreamEvidence } from "../stream-evidence.js";
|
|
7
10
|
// Mirrors CONNECTOR_ID_REGEX in ../define.ts, which defineProvider() enforces.
|
|
8
11
|
// A single lowercase segment (no hyphen) is a valid id, so the trailing group
|
|
9
12
|
// is optional (`*`), matching providers like `kakaomap`, `kstartup`, `triple`.
|
|
@@ -13,8 +16,10 @@ const VALID_AUTH_MODES = [
|
|
|
13
16
|
"platform-managed",
|
|
14
17
|
"credentials",
|
|
15
18
|
"oauth2",
|
|
19
|
+
"oauth2_proxied",
|
|
16
20
|
];
|
|
17
21
|
const UPDATE_SNAPSHOT_ARGS = new Set(["-u", "--update-snapshots"]);
|
|
22
|
+
const snapshotCaptureStates = new WeakMap();
|
|
18
23
|
function isFixtureEnvelope(value) {
|
|
19
24
|
return (value !== null &&
|
|
20
25
|
typeof value === "object" &&
|
|
@@ -60,9 +65,7 @@ function inferFixtureDir(providerId) {
|
|
|
60
65
|
.map((line) => line.match(/\(?((?:file:\/\/)?[^():]+\.test\.ts)/)?.[1])
|
|
61
66
|
.find((file) => file !== undefined);
|
|
62
67
|
if (testFile) {
|
|
63
|
-
const pathname = testFile.startsWith("file://")
|
|
64
|
-
? new URL(testFile).pathname
|
|
65
|
-
: testFile;
|
|
68
|
+
const pathname = testFile.startsWith("file://") ? new URL(testFile).pathname : testFile;
|
|
66
69
|
return `${pathname.replace(/\/[^/]+$/, "")}/../__fixtures__`;
|
|
67
70
|
}
|
|
68
71
|
return `providers/${providerId}/__fixtures__`;
|
|
@@ -81,10 +84,349 @@ function jsonResponse(data) {
|
|
|
81
84
|
bytes: async () => bodyBytes.slice(0),
|
|
82
85
|
};
|
|
83
86
|
}
|
|
87
|
+
function headersToRecord(headers) {
|
|
88
|
+
return Object.fromEntries(headers.entries());
|
|
89
|
+
}
|
|
90
|
+
async function normalizeUpstreamResponse(response) {
|
|
91
|
+
if (response instanceof Response) {
|
|
92
|
+
const bytes = new Uint8Array(await response.arrayBuffer());
|
|
93
|
+
const text = new TextDecoder().decode(bytes);
|
|
94
|
+
const headers = headersToRecord(response.headers);
|
|
95
|
+
let data = text;
|
|
96
|
+
if (response.headers.get("content-type")?.includes("application/json")) {
|
|
97
|
+
data = text.length > 0 ? JSON.parse(text) : null;
|
|
98
|
+
}
|
|
99
|
+
return { status: response.status, headers, data, text, bytes };
|
|
100
|
+
}
|
|
101
|
+
const headers = { ...(response.headers ?? {}) };
|
|
102
|
+
const body = response.body ?? null;
|
|
103
|
+
let bytes;
|
|
104
|
+
let text;
|
|
105
|
+
let data;
|
|
106
|
+
if (body instanceof Uint8Array) {
|
|
107
|
+
bytes = body.slice(0);
|
|
108
|
+
text = new TextDecoder().decode(bytes);
|
|
109
|
+
data = text;
|
|
110
|
+
}
|
|
111
|
+
else if (body instanceof ArrayBuffer) {
|
|
112
|
+
bytes = new Uint8Array(body.slice(0));
|
|
113
|
+
text = new TextDecoder().decode(bytes);
|
|
114
|
+
data = text;
|
|
115
|
+
}
|
|
116
|
+
else if (typeof body === "string") {
|
|
117
|
+
text = body;
|
|
118
|
+
bytes = new TextEncoder().encode(text);
|
|
119
|
+
data = body;
|
|
120
|
+
}
|
|
121
|
+
else {
|
|
122
|
+
text = JSON.stringify(body);
|
|
123
|
+
bytes = new TextEncoder().encode(text);
|
|
124
|
+
data = body;
|
|
125
|
+
if (!Object.keys(headers).some((name) => name.toLowerCase() === "content-type")) {
|
|
126
|
+
headers["content-type"] = "application/json";
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
return { status: response.status ?? 200, headers, data, text, bytes };
|
|
130
|
+
}
|
|
131
|
+
function toHttpResponse(response) {
|
|
132
|
+
return {
|
|
133
|
+
status: response.status,
|
|
134
|
+
ok: response.status >= 200 && response.status < 300,
|
|
135
|
+
headers: response.headers,
|
|
136
|
+
data: response.data,
|
|
137
|
+
json: async () => JSON.parse(response.text),
|
|
138
|
+
text: async () => response.text,
|
|
139
|
+
arrayBuffer: async () => response.bytes.slice(0).buffer,
|
|
140
|
+
bytes: async () => response.bytes.slice(0),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const emptyCookieJar = {
|
|
144
|
+
get: () => undefined,
|
|
145
|
+
getAll: () => ({}),
|
|
146
|
+
toString: () => "",
|
|
147
|
+
};
|
|
148
|
+
function emptyCookieStore() {
|
|
149
|
+
return {
|
|
150
|
+
version: 1,
|
|
151
|
+
jar: { cookies: [] },
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
function toStealthResponse(response, url) {
|
|
155
|
+
return {
|
|
156
|
+
status: response.status,
|
|
157
|
+
ok: response.status >= 200 && response.status < 300,
|
|
158
|
+
url,
|
|
159
|
+
redirected: false,
|
|
160
|
+
headers: response.headers,
|
|
161
|
+
rawHeaders: Object.entries(response.headers),
|
|
162
|
+
body: response.text,
|
|
163
|
+
cookies: emptyCookieJar,
|
|
164
|
+
json: async () => JSON.parse(response.text),
|
|
165
|
+
arrayBuffer: async () => response.bytes.slice(0).buffer,
|
|
166
|
+
bytes: async () => response.bytes.slice(0),
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
function streamFromBytes(bytes) {
|
|
170
|
+
return new ReadableStream({
|
|
171
|
+
start(controller) {
|
|
172
|
+
controller.enqueue(bytes.slice(0));
|
|
173
|
+
controller.close();
|
|
174
|
+
},
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
async function* singleBytes(bytes) {
|
|
178
|
+
yield bytes.slice(0);
|
|
179
|
+
}
|
|
180
|
+
async function* singleText(textValue) {
|
|
181
|
+
yield textValue;
|
|
182
|
+
}
|
|
183
|
+
function createUpstreamContext(provider, operationName, upstreamStub) {
|
|
184
|
+
const credential = {
|
|
185
|
+
mode: "none",
|
|
186
|
+
get: () => undefined,
|
|
187
|
+
getAll: () => ({}),
|
|
188
|
+
getAccessToken: () => undefined,
|
|
189
|
+
getScopes: () => [],
|
|
190
|
+
};
|
|
191
|
+
const request = { headers: {} };
|
|
192
|
+
const state = createMemoryProviderRuntimeState();
|
|
193
|
+
const nativeEgress = provider.native
|
|
194
|
+
? createNativeEgressAuthorization({ egress: provider.native.network })
|
|
195
|
+
: undefined;
|
|
196
|
+
const requireNativeEgress = () => {
|
|
197
|
+
if (!nativeEgress)
|
|
198
|
+
throw new NativeNetworkError("Native egress authorization is unavailable", "native_egress_policy_invalid");
|
|
199
|
+
return nativeEgress;
|
|
200
|
+
};
|
|
201
|
+
const dispatch = async (call) => {
|
|
202
|
+
const canned = await upstreamStub({ operationName, ...call });
|
|
203
|
+
if (canned === undefined) {
|
|
204
|
+
throw new Error(`Unmatched upstream call for operation "${operationName}": ${call.transport}.${call.method}${call.url ? ` ${call.url}` : ""}. Add a canned response to upstreamStub; live network passthrough is disabled.`);
|
|
205
|
+
}
|
|
206
|
+
return normalizeUpstreamResponse(canned);
|
|
207
|
+
};
|
|
208
|
+
const httpCall = async (method, url, body, options) => toHttpResponse(await dispatch({ transport: "http", method, url, body, options }));
|
|
209
|
+
const stealthCall = async (url, options) => toStealthResponse(await dispatch({
|
|
210
|
+
transport: "stealth",
|
|
211
|
+
method: options?.method?.toUpperCase() ?? "GET",
|
|
212
|
+
url,
|
|
213
|
+
body: options?.body,
|
|
214
|
+
options,
|
|
215
|
+
}), url);
|
|
216
|
+
const createBrowserPage = () => {
|
|
217
|
+
let currentUrl = "about:blank";
|
|
218
|
+
let currentResponse;
|
|
219
|
+
const browserAction = async (method, body) => {
|
|
220
|
+
currentResponse = await dispatch({
|
|
221
|
+
transport: "browser",
|
|
222
|
+
method,
|
|
223
|
+
url: currentUrl,
|
|
224
|
+
body,
|
|
225
|
+
});
|
|
226
|
+
return currentResponse;
|
|
227
|
+
};
|
|
228
|
+
const page = {
|
|
229
|
+
id: `standard-test-${operationName}`,
|
|
230
|
+
url: async () => currentUrl,
|
|
231
|
+
title: async () => currentResponse?.text.match(/<title[^>]*>([^<]*)<\/title>/i)?.[1] ?? "",
|
|
232
|
+
content: async () => currentResponse?.text ?? "",
|
|
233
|
+
evaluate: async (fn) => (await browserAction("evaluate", typeof fn === "string" ? fn : String(fn))).data,
|
|
234
|
+
locator: (selector) => ({
|
|
235
|
+
click: async () => {
|
|
236
|
+
await browserAction("locator.click", { selector });
|
|
237
|
+
},
|
|
238
|
+
fill: async (textValue) => {
|
|
239
|
+
await browserAction("locator.fill", { selector, text: textValue });
|
|
240
|
+
},
|
|
241
|
+
textContent: async () => {
|
|
242
|
+
const value = (await browserAction("locator.textContent", { selector })).data;
|
|
243
|
+
return value === null || value === undefined ? null : String(value);
|
|
244
|
+
},
|
|
245
|
+
waitFor: async (options) => {
|
|
246
|
+
await browserAction("locator.waitFor", { selector, options });
|
|
247
|
+
},
|
|
248
|
+
}),
|
|
249
|
+
close: async () => { },
|
|
250
|
+
fill: async (selector, textValue) => {
|
|
251
|
+
await browserAction("fill", { selector, text: textValue });
|
|
252
|
+
},
|
|
253
|
+
goto: async (url) => {
|
|
254
|
+
currentUrl = url;
|
|
255
|
+
await browserAction("goto");
|
|
256
|
+
},
|
|
257
|
+
screenshot: async (options) => Buffer.from((await browserAction("screenshot", options)).bytes),
|
|
258
|
+
click: async (selector) => {
|
|
259
|
+
await browserAction("click", { selector });
|
|
260
|
+
},
|
|
261
|
+
type: async (selector, textValue) => {
|
|
262
|
+
await browserAction("type", { selector, text: textValue });
|
|
263
|
+
},
|
|
264
|
+
waitForSelector: async (selector, options) => {
|
|
265
|
+
await browserAction("waitForSelector", { selector, options });
|
|
266
|
+
},
|
|
267
|
+
frames: async () => [page],
|
|
268
|
+
withResourcePolicy: async (_policy, run) => run(),
|
|
269
|
+
};
|
|
270
|
+
return page;
|
|
271
|
+
};
|
|
272
|
+
const createStealthSession = () => ({
|
|
273
|
+
fetch: stealthCall,
|
|
274
|
+
cookies: {
|
|
275
|
+
...emptyCookieJar,
|
|
276
|
+
has: () => false,
|
|
277
|
+
setFromCookieStrings: () => { },
|
|
278
|
+
toHeader: () => "",
|
|
279
|
+
snapshot: () => ({}),
|
|
280
|
+
restore: () => { },
|
|
281
|
+
serialize: emptyCookieStore,
|
|
282
|
+
deserialize: () => { },
|
|
283
|
+
clear: () => { },
|
|
284
|
+
},
|
|
285
|
+
redirects: {
|
|
286
|
+
run: async (options) => {
|
|
287
|
+
const final = await stealthCall(options.url, options);
|
|
288
|
+
return {
|
|
289
|
+
final,
|
|
290
|
+
hops: [],
|
|
291
|
+
reason: "completed",
|
|
292
|
+
cookies: {},
|
|
293
|
+
cookieStore: emptyCookieStore(),
|
|
294
|
+
};
|
|
295
|
+
},
|
|
296
|
+
},
|
|
297
|
+
close: () => { },
|
|
298
|
+
});
|
|
299
|
+
return {
|
|
300
|
+
env: { get: () => undefined },
|
|
301
|
+
credential,
|
|
302
|
+
request,
|
|
303
|
+
http: {
|
|
304
|
+
request: (url, options) => httpCall(options?.method?.toUpperCase() ?? "GET", url, options?.body, options),
|
|
305
|
+
get: (url, options) => httpCall("GET", url, undefined, options),
|
|
306
|
+
post: (url, body, options) => httpCall("POST", url, body, options),
|
|
307
|
+
put: (url, body, options) => httpCall("PUT", url, body, options),
|
|
308
|
+
delete: (url, options) => httpCall("DELETE", url, undefined, options),
|
|
309
|
+
stream: async (url, options) => {
|
|
310
|
+
const response = await dispatch({
|
|
311
|
+
transport: "http",
|
|
312
|
+
method: options?.method?.toUpperCase() ?? "GET",
|
|
313
|
+
url,
|
|
314
|
+
body: options?.body,
|
|
315
|
+
options,
|
|
316
|
+
});
|
|
317
|
+
return {
|
|
318
|
+
status: response.status,
|
|
319
|
+
ok: response.status >= 200 && response.status < 300,
|
|
320
|
+
headers: response.headers,
|
|
321
|
+
body: streamFromBytes(response.bytes),
|
|
322
|
+
bytes: () => singleBytes(response.bytes),
|
|
323
|
+
textChunks: () => singleText(response.text),
|
|
324
|
+
lines: () => singleText(response.text),
|
|
325
|
+
};
|
|
326
|
+
},
|
|
327
|
+
sse: async (url, options) => {
|
|
328
|
+
const response = await dispatch({
|
|
329
|
+
transport: "http",
|
|
330
|
+
method: options?.method?.toUpperCase() ?? "GET",
|
|
331
|
+
url,
|
|
332
|
+
body: options?.body,
|
|
333
|
+
options,
|
|
334
|
+
});
|
|
335
|
+
async function* messages() {
|
|
336
|
+
for (const block of response.text.split(/\r?\n\r?\n/)) {
|
|
337
|
+
const data = block
|
|
338
|
+
.split(/\r?\n/)
|
|
339
|
+
.filter((line) => line.startsWith("data:"))
|
|
340
|
+
.map((line) => line.slice(5).trimStart())
|
|
341
|
+
.join("\n");
|
|
342
|
+
if (!data)
|
|
343
|
+
continue;
|
|
344
|
+
yield { event: "message", data, json: () => JSON.parse(data) };
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
return messages();
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
cache: createProviderCache({ providerId: `standard-test-${operationName}` }),
|
|
351
|
+
state,
|
|
352
|
+
stealth: {
|
|
353
|
+
fetch: stealthCall,
|
|
354
|
+
createSession: createStealthSession,
|
|
355
|
+
},
|
|
356
|
+
browser: {
|
|
357
|
+
engine: "playwright-stealth",
|
|
358
|
+
newPage: async () => createBrowserPage(),
|
|
359
|
+
rawPage: async () => createBrowserPage(),
|
|
360
|
+
withIsolatedContext: async (handler) => handler(createBrowserPage()),
|
|
361
|
+
solveChallenge: async (challenge) => (await dispatch({
|
|
362
|
+
transport: "browser",
|
|
363
|
+
method: "solveChallenge",
|
|
364
|
+
body: challenge,
|
|
365
|
+
})).data,
|
|
366
|
+
},
|
|
367
|
+
...(provider.native
|
|
368
|
+
? {
|
|
369
|
+
native: {
|
|
370
|
+
network: {
|
|
371
|
+
connectTcp: async (options) => {
|
|
372
|
+
const request = snapshotNativeConnectInput(options);
|
|
373
|
+
requireNativeEgress().assertConnect(request, "disabled");
|
|
374
|
+
return createNativeConnection(await dispatch({
|
|
375
|
+
transport: "native",
|
|
376
|
+
method: "connectTcp",
|
|
377
|
+
url: `tcp://${request.host}:${request.port}`,
|
|
378
|
+
options: request,
|
|
379
|
+
}), dispatch, `tcp://${request.host}:${request.port}`);
|
|
380
|
+
},
|
|
381
|
+
connectTls: async (options) => {
|
|
382
|
+
const request = snapshotNativeConnectInput(options);
|
|
383
|
+
requireNativeEgress().assertConnect(request, "required");
|
|
384
|
+
return createNativeConnection(await dispatch({
|
|
385
|
+
transport: "native",
|
|
386
|
+
method: "connectTls",
|
|
387
|
+
url: `tls://${request.host}:${request.port}`,
|
|
388
|
+
options: request,
|
|
389
|
+
}), dispatch, `tls://${request.host}:${request.port}`);
|
|
390
|
+
},
|
|
391
|
+
grantTcpEgress: (input) => {
|
|
392
|
+
return requireNativeEgress().grant(snapshotNativeGrantInput(input));
|
|
393
|
+
},
|
|
394
|
+
},
|
|
395
|
+
},
|
|
396
|
+
}
|
|
397
|
+
: {}),
|
|
398
|
+
trace: { span: async (_name, fn) => fn() },
|
|
399
|
+
auth: { requestField: async (name) => unsupported(`ctx.auth.requestField(${name})`) },
|
|
400
|
+
stt: createUnsupportedSttClient("Standard test upstream context does not support ctx.stt.transcribe"),
|
|
401
|
+
choice: createTestProviderChoiceContext({
|
|
402
|
+
providerId: `standard-test-${operationName}`,
|
|
403
|
+
request,
|
|
404
|
+
credential,
|
|
405
|
+
state,
|
|
406
|
+
}),
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
function createNativeConnection(initialResponse, dispatch, url) {
|
|
410
|
+
let unread = initialResponse.bytes.slice(0);
|
|
411
|
+
return {
|
|
412
|
+
read: async () => {
|
|
413
|
+
if (unread.byteLength === 0)
|
|
414
|
+
return null;
|
|
415
|
+
const bytes = unread;
|
|
416
|
+
unread = new Uint8Array();
|
|
417
|
+
return bytes;
|
|
418
|
+
},
|
|
419
|
+
write: async (body) => {
|
|
420
|
+
const response = await dispatch({ transport: "native", method: "write", url, body });
|
|
421
|
+
unread = response.bytes.slice(0);
|
|
422
|
+
},
|
|
423
|
+
close: async () => { },
|
|
424
|
+
};
|
|
425
|
+
}
|
|
84
426
|
function unsupported(name) {
|
|
85
427
|
throw new Error(`Standard test snapshot context does not support ${name}`);
|
|
86
428
|
}
|
|
87
|
-
function createSnapshotContext(rawFixture) {
|
|
429
|
+
export function createSnapshotContext(rawFixture) {
|
|
88
430
|
const credential = {
|
|
89
431
|
mode: "none",
|
|
90
432
|
get: () => undefined,
|
|
@@ -94,17 +436,52 @@ function createSnapshotContext(rawFixture) {
|
|
|
94
436
|
};
|
|
95
437
|
const request = { headers: {} };
|
|
96
438
|
const state = createMemoryProviderRuntimeState();
|
|
97
|
-
|
|
439
|
+
const streamCaptureGroup = findStreamCaptureGroup(rawFixture);
|
|
440
|
+
let nextCaptureItem = 0;
|
|
441
|
+
const replayJsonResponse = () => {
|
|
442
|
+
if (!streamCaptureGroup)
|
|
443
|
+
return jsonResponse(rawFixture);
|
|
444
|
+
const item = streamCaptureGroup.items[nextCaptureItem];
|
|
445
|
+
if (!item) {
|
|
446
|
+
throw new Error(`Stream fixture exhausted: no recorded response exists for ordinary HTTP call ${nextCaptureItem + 1}.`);
|
|
447
|
+
}
|
|
448
|
+
if (item.kind !== "response") {
|
|
449
|
+
throw new Error(`Stream fixture call-order mismatch: expected a stream call at position ${nextCaptureItem + 1}, received an ordinary HTTP call.`);
|
|
450
|
+
}
|
|
451
|
+
nextCaptureItem += 1;
|
|
452
|
+
return jsonResponse(item.value);
|
|
453
|
+
};
|
|
454
|
+
const context = {
|
|
98
455
|
env: { get: () => undefined },
|
|
99
456
|
credential,
|
|
100
457
|
request,
|
|
101
458
|
http: {
|
|
102
|
-
request: async () =>
|
|
103
|
-
get: async () =>
|
|
104
|
-
post: async () =>
|
|
105
|
-
put: async () =>
|
|
106
|
-
delete: async () =>
|
|
107
|
-
stream: async () =>
|
|
459
|
+
request: async () => replayJsonResponse(),
|
|
460
|
+
get: async () => replayJsonResponse(),
|
|
461
|
+
post: async () => replayJsonResponse(),
|
|
462
|
+
put: async () => replayJsonResponse(),
|
|
463
|
+
delete: async () => replayJsonResponse(),
|
|
464
|
+
stream: async (...args) => {
|
|
465
|
+
if (!streamCaptureGroup)
|
|
466
|
+
return unsupported("ctx.http.stream");
|
|
467
|
+
const item = streamCaptureGroup.items[nextCaptureItem];
|
|
468
|
+
if (!item) {
|
|
469
|
+
throw new Error(`Stream fixture exhausted: no recorded evidence exists for stream call ${nextCaptureItem + 1}.`);
|
|
470
|
+
}
|
|
471
|
+
if (item.kind !== "stream") {
|
|
472
|
+
throw new Error(`Stream fixture call-order mismatch: expected an ordinary HTTP call at position ${nextCaptureItem + 1}, received a stream call.`);
|
|
473
|
+
}
|
|
474
|
+
if (item.evidence.request) {
|
|
475
|
+
const expected = item.evidence.request;
|
|
476
|
+
const actualMethod = (args[1]?.method ?? "GET").toUpperCase();
|
|
477
|
+
const actualPath = replayRequestPath(args[0], expected.path);
|
|
478
|
+
if (actualMethod !== expected.method || actualPath !== expected.path) {
|
|
479
|
+
throw new Error(`Stream fixture request mismatch: expected ${expected.method} ${expected.path}, received ${actualMethod} ${actualPath}.`);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
nextCaptureItem += 1;
|
|
483
|
+
return replayStreamEvidence(item.evidence);
|
|
484
|
+
},
|
|
108
485
|
sse: async () => unsupported("ctx.http.sse"),
|
|
109
486
|
},
|
|
110
487
|
cache: createProviderCache({ providerId: "standard-test" }),
|
|
@@ -134,15 +511,39 @@ function createSnapshotContext(rawFixture) {
|
|
|
134
511
|
state,
|
|
135
512
|
}),
|
|
136
513
|
};
|
|
514
|
+
snapshotCaptureStates.set(context, {
|
|
515
|
+
assertConsumed() {
|
|
516
|
+
if (streamCaptureGroup && nextCaptureItem !== streamCaptureGroup.items.length) {
|
|
517
|
+
throw new Error(`Stream fixture has ${streamCaptureGroup.items.length - nextCaptureItem} unconsumed capture item${streamCaptureGroup.items.length - nextCaptureItem === 1 ? "" : "s"} after handler completion.`);
|
|
518
|
+
}
|
|
519
|
+
},
|
|
520
|
+
consumed: () => nextCaptureItem,
|
|
521
|
+
});
|
|
522
|
+
return context;
|
|
523
|
+
}
|
|
524
|
+
function replayRequestPath(requestUrl, expectedPath) {
|
|
525
|
+
if (/^[a-z][a-z\d+.-]*:\/\//i.test(requestUrl) || requestUrl.startsWith("/")) {
|
|
526
|
+
return requestPathForFixture(requestUrl);
|
|
527
|
+
}
|
|
528
|
+
return requestPathForFixture(new URL(requestUrl, `https://fixture.invalid${expectedPath}`).toString());
|
|
137
529
|
}
|
|
138
530
|
async function transformSnapshotOutput(provider, rawFixture) {
|
|
139
531
|
const entries = Object.entries(provider.operations);
|
|
140
|
-
const
|
|
532
|
+
const captureStates = [];
|
|
141
533
|
const outputs = await Promise.all(entries.map(async ([operationName, operation]) => {
|
|
534
|
+
const context = createSnapshotContext(rawFixture);
|
|
142
535
|
const request = operation.fixtures?.request ?? {};
|
|
143
536
|
const output = await operation.handler(context, request);
|
|
537
|
+
const captureState = snapshotCaptureStates.get(context);
|
|
538
|
+
if (captureState)
|
|
539
|
+
captureStates.push(captureState);
|
|
540
|
+
if (captureState?.consumed())
|
|
541
|
+
captureState.assertConsumed();
|
|
144
542
|
return [operationName, output];
|
|
145
543
|
}));
|
|
544
|
+
if (findStreamCaptureGroup(rawFixture) && !captureStates.some((state) => state.consumed() > 0)) {
|
|
545
|
+
captureStates[0]?.assertConsumed();
|
|
546
|
+
}
|
|
146
547
|
if (outputs.length === 1) {
|
|
147
548
|
return outputs[0]?.[1];
|
|
148
549
|
}
|
|
@@ -161,7 +562,7 @@ function formatJsonDiff(current, expected) {
|
|
|
161
562
|
const expectedLine = expectedLines[index];
|
|
162
563
|
if (currentLine === expectedLine) {
|
|
163
564
|
if (currentLine !== undefined) {
|
|
164
|
-
lines.push(`
|
|
565
|
+
lines.push(` ${currentLine}`);
|
|
165
566
|
}
|
|
166
567
|
continue;
|
|
167
568
|
}
|
|
@@ -187,16 +588,51 @@ function expectSchemaFixture(operationName, fieldName, fixture, result) {
|
|
|
187
588
|
function parseSchemaFixture(operationName, fieldName, schema, fixture) {
|
|
188
589
|
expectSchemaFixture(operationName, fieldName, fixture, safeParseSchemaSync(schema, fixture, `operations.${operationName}.fixtures.${fieldName}`));
|
|
189
590
|
}
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
591
|
+
async function materializeHandlerOutput(output) {
|
|
592
|
+
if (!(output instanceof Response))
|
|
593
|
+
return output;
|
|
594
|
+
const contentType = output.headers.get("content-type") ?? "";
|
|
595
|
+
if (contentType.includes("application/json"))
|
|
596
|
+
return output.json();
|
|
597
|
+
return output.text();
|
|
598
|
+
}
|
|
599
|
+
/** Internal execution seam exported for focused SDK tests; use runStandardTests as public API. */
|
|
600
|
+
export async function executeStandardTestHandler(provider, operationName, upstreamStub) {
|
|
601
|
+
const operation = provider.operations[operationName];
|
|
602
|
+
if (!operation)
|
|
603
|
+
throw new Error(`Unknown operation "${operationName}".`);
|
|
604
|
+
if (operation.fixtures?.request === undefined) {
|
|
605
|
+
throw new Error(`Operation "${operationName}" has no fixtures.request for handler E2E execution.`);
|
|
606
|
+
}
|
|
607
|
+
const context = createUpstreamContext(provider, operationName, upstreamStub);
|
|
608
|
+
const output = await materializeHandlerOutput(await operation.handler(context, operation.fixtures.request));
|
|
609
|
+
const result = safeParseSchemaSync(operation.output, output, `operations.${operationName}.handler.output`);
|
|
610
|
+
if (!result.success) {
|
|
611
|
+
throw new Error([
|
|
612
|
+
`Handler output for operation "${operationName}" failed schema validation.`,
|
|
613
|
+
formatJsonDiff({ valid: false, value: output, error: result.error }, { valid: true, value: output }),
|
|
614
|
+
].join("\n"));
|
|
615
|
+
}
|
|
616
|
+
return output;
|
|
617
|
+
}
|
|
618
|
+
function isOptionsShortcut(value) {
|
|
619
|
+
return (value !== null &&
|
|
620
|
+
typeof value === "object" &&
|
|
621
|
+
!Array.isArray(value) &&
|
|
622
|
+
Object.hasOwn(value, "upstreamStub"));
|
|
623
|
+
}
|
|
624
|
+
export function runStandardTests(provider, rawFixtureOrOptions, manifest, legacyOptions) {
|
|
625
|
+
const shortcut = manifest === undefined && legacyOptions === undefined && isOptionsShortcut(rawFixtureOrOptions);
|
|
626
|
+
const rawFixture = shortcut ? undefined : rawFixtureOrOptions;
|
|
627
|
+
const options = shortcut ? rawFixtureOrOptions : (legacyOptions ?? {});
|
|
199
628
|
const operations = Object.entries(provider.operations);
|
|
629
|
+
const warnings = options.upstreamStub
|
|
630
|
+
? operations
|
|
631
|
+
.filter(([, operation]) => operation.fixtures?.request === undefined)
|
|
632
|
+
.map(([operationName]) => `[provider-sdk] Operation "${provider.id}.${operationName}" has no fixtures.request, so runStandardTests cannot invoke its handler E2E.`)
|
|
633
|
+
: operations.map(([operationName]) => `[provider-sdk] Operation "${provider.id}.${operationName}" has no handler E2E coverage in runStandardTests; configure upstreamStub to invoke the real handler.`);
|
|
634
|
+
for (const warning of warnings)
|
|
635
|
+
console.warn(warning);
|
|
200
636
|
const assertFixtureValidation = () => {
|
|
201
637
|
expect(rawFixture).toBeDefined();
|
|
202
638
|
expect(isJsonCompatible(rawFixture)).toBe(true);
|
|
@@ -296,12 +732,26 @@ export function runStandardTests(provider, rawFixture, manifest, options = {}) {
|
|
|
296
732
|
const actual = await transformSnapshotOutput(provider, rawFixture);
|
|
297
733
|
const serialized = stableStringify(actual);
|
|
298
734
|
const snapshotFile = Bun.file(snapshotPath);
|
|
299
|
-
|
|
735
|
+
const snapshotExists = await snapshotFile.exists();
|
|
736
|
+
if (!snapshotExists && options.requireSnapshot && !shouldUpdateSnapshots()) {
|
|
737
|
+
throw new Error(`Required golden snapshot is missing: ${snapshotPath}. Regenerate it with bun test --update-snapshots.`);
|
|
738
|
+
}
|
|
739
|
+
if (shouldUpdateSnapshots() || !snapshotExists) {
|
|
300
740
|
await Bun.write(snapshotPath, serialized);
|
|
301
741
|
}
|
|
302
742
|
const expected = JSON.parse(await Bun.file(snapshotPath).text());
|
|
303
743
|
expect(actual).toEqual(expected);
|
|
304
744
|
});
|
|
305
745
|
}
|
|
746
|
+
if (options.upstreamStub) {
|
|
747
|
+
for (const [operationName, operation] of operations) {
|
|
748
|
+
if (operation.fixtures?.request === undefined)
|
|
749
|
+
continue;
|
|
750
|
+
it(`invokes the real ${operationName} handler with canned upstream responses`, async () => {
|
|
751
|
+
await executeStandardTestHandler(provider, operationName, options.upstreamStub);
|
|
752
|
+
});
|
|
753
|
+
}
|
|
754
|
+
}
|
|
306
755
|
});
|
|
756
|
+
return { warnings };
|
|
307
757
|
}
|