@capybara-hq/testing 0.1.0-rc.29 → 0.1.0-rc.30
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/dist/fixtures.d.ts +2 -1
- package/dist/fixtures.js +2 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/push-conformance.d.ts +31 -0
- package/dist/push-conformance.js +160 -0
- package/fixtures/extensions/fake-push/.capybara-plugin/plugin.json +13 -0
- package/fixtures/extensions/fake-push/guides/fake-push-key.md +6 -0
- package/fixtures/extensions/fake-push/guides/fake-push-key.zh.md +6 -0
- package/fixtures/extensions/fake-push/migrations/0001_sends.sql +9 -0
- package/fixtures/extensions/fake-push/package.json +12 -0
- package/fixtures/extensions/fake-push/src/check.ts +40 -0
- package/fixtures/extensions/fake-push/src/index.ts +48 -0
- package/fixtures/extensions/fake-push/src/push.ts +103 -0
- package/package.json +5 -5
package/dist/fixtures.d.ts
CHANGED
|
@@ -22,9 +22,10 @@
|
|
|
22
22
|
* | `fake-context` | `context@1` scripted by its deployment config (`text`, default "fake context"; `mode` `ok` / `null` / `throw` / `hang`); calls recorded on `globalThis.__fakeContext` (`fakeContextProbe`); tool `note` (`sources` web and channel_dm) |
|
|
23
23
|
* | `fake-client` | client extension (`permissions.client`, no extension point): components `hud` (`conversation.properties.section`, server view with `data`), `brand` (`shell.brand`, renders `builtin` in `data-testid="fake-client-brand"`), `empty` (`conversation.empty`), `frame` (`conversation.composer.frame`) and `fx` (`shell.overlay`); `client/styles/fake.css` (outline-color on `button.primary` and the composer's buttons, `--cx-fake-client-run` while `run=running`); an `events` listener storing the last event type; theme `fake`; `suggestedAppearance` = theme `fake` + `shell.brand` -> `brand` |
|
|
24
24
|
* | `fake-client-broken` | client code that throws: component `boom` (render), a `run.started` events listener, and a `conversation.empty` replacement (render) |
|
|
25
|
+
* | `fake-push` | `push-provider@1` scripted by the token prefix (`dead:`, `auth:`, `rate:`, `flaky:<n>:`, `throw:`, `net:`, `hang:`; else delivered); deployment config `apps` (default `["com.example.app"]`), optional secret `key`; every send is a row of `plugin_fake_push.sends` and an entry of `fakePushProbe().sends`; `makeFakePush()` is the same provider without a kernel |
|
|
25
26
|
* | `fake-config-form` | display name 表单样例 / Form sample; no extension point: an openai-compatible-shaped deployment config (endpoints with a preset menu, an API key per endpoint in the secret record `apiKeys`, model rows, advanced fields, localized custom issues) carrying the config form hints of the extension config forms spec §6, for the web form e2e |
|
|
26
27
|
*/
|
|
27
|
-
export declare const FAKE_EXTENSIONS: readonly ["fake-connector", "fake-openapi", "fake-object-store", "fake-tools-ui", "fake-storage-user", "fake-lifecycle", "fake-embed", "fake-bootstrap-a", "fake-bootstrap-b", "fake-dep-provider", "fake-dep-requirer", "fake-im", "fake-web", "fake-web-2", "fake-context", "fake-config-form", "fake-client", "fake-client-broken"];
|
|
28
|
+
export declare const FAKE_EXTENSIONS: readonly ["fake-connector", "fake-openapi", "fake-object-store", "fake-tools-ui", "fake-storage-user", "fake-lifecycle", "fake-embed", "fake-bootstrap-a", "fake-bootstrap-b", "fake-dep-provider", "fake-dep-requirer", "fake-im", "fake-web", "fake-web-2", "fake-context", "fake-config-form", "fake-push", "fake-client", "fake-client-broken"];
|
|
28
29
|
/**
|
|
29
30
|
* Fake plugins without an extension (declarative: they live in `fixtures/plugins/<name>`, same
|
|
30
31
|
* pack layout).
|
package/dist/fixtures.js
CHANGED
|
@@ -25,6 +25,7 @@ import { fileURLToPath } from "node:url";
|
|
|
25
25
|
* | `fake-context` | `context@1` scripted by its deployment config (`text`, default "fake context"; `mode` `ok` / `null` / `throw` / `hang`); calls recorded on `globalThis.__fakeContext` (`fakeContextProbe`); tool `note` (`sources` web and channel_dm) |
|
|
26
26
|
* | `fake-client` | client extension (`permissions.client`, no extension point): components `hud` (`conversation.properties.section`, server view with `data`), `brand` (`shell.brand`, renders `builtin` in `data-testid="fake-client-brand"`), `empty` (`conversation.empty`), `frame` (`conversation.composer.frame`) and `fx` (`shell.overlay`); `client/styles/fake.css` (outline-color on `button.primary` and the composer's buttons, `--cx-fake-client-run` while `run=running`); an `events` listener storing the last event type; theme `fake`; `suggestedAppearance` = theme `fake` + `shell.brand` -> `brand` |
|
|
27
27
|
* | `fake-client-broken` | client code that throws: component `boom` (render), a `run.started` events listener, and a `conversation.empty` replacement (render) |
|
|
28
|
+
* | `fake-push` | `push-provider@1` scripted by the token prefix (`dead:`, `auth:`, `rate:`, `flaky:<n>:`, `throw:`, `net:`, `hang:`; else delivered); deployment config `apps` (default `["com.example.app"]`), optional secret `key`; every send is a row of `plugin_fake_push.sends` and an entry of `fakePushProbe().sends`; `makeFakePush()` is the same provider without a kernel |
|
|
28
29
|
* | `fake-config-form` | display name 表单样例 / Form sample; no extension point: an openai-compatible-shaped deployment config (endpoints with a preset menu, an API key per endpoint in the secret record `apiKeys`, model rows, advanced fields, localized custom issues) carrying the config form hints of the extension config forms spec §6, for the web form e2e |
|
|
29
30
|
*/
|
|
30
31
|
export const FAKE_EXTENSIONS = [
|
|
@@ -44,6 +45,7 @@ export const FAKE_EXTENSIONS = [
|
|
|
44
45
|
"fake-web-2",
|
|
45
46
|
"fake-context",
|
|
46
47
|
"fake-config-form",
|
|
48
|
+
"fake-push",
|
|
47
49
|
"fake-client",
|
|
48
50
|
"fake-client-broken",
|
|
49
51
|
];
|
package/dist/index.d.ts
CHANGED
|
@@ -23,6 +23,7 @@ export { FAKE_EXTENSIONS, type FakeExtensionName, fakeExtensionDir, fakeExtensio
|
|
|
23
23
|
export { cardRecorded, cookieRequest, createAgent, createConversation, createLocalAccount, createOrg, enableModelProviders, eventsOf, joinOrg, type KernelApp, type KernelRequest, type KernelRequestOptions, type KernelResponse, login, pidOwningLocalPort, postMessage, sleep, startTestKernel, TEST_PASSWORD, type TestKernel, testKernelConfig, truncateTables, waitFor, waitForEvent, waitForTerminal, } from "./kernel-helpers.js";
|
|
24
24
|
export { FAKE_PNG_BASE64, isPngBase64, pdfBase64, pdfSampleWord, redPngBase64, solidPngBase64, } from "./media-samples.js";
|
|
25
25
|
export { type ConformanceTarget, checkDeclaredParameters, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
26
|
+
export { describePushProviderConformance, type PushConformanceTarget, type PushOutcome, type PushVendorRequest, } from "./push-conformance.js";
|
|
26
27
|
export { type NativeFixtureEntry, runSandboxNativeConformance, type SandboxNativeFixture, } from "./sandbox-native-conformance.js";
|
|
27
28
|
export { runSandboxPermissionConformance, type SandboxPermissionConformancePart, type SandboxPermissionEndpoint, type SandboxPermissionFixture, type SandboxPermissionGrant, type SandboxPermissionOpenNetwork, type SandboxPermissionTarget, } from "./sandbox-permission-conformance.js";
|
|
28
29
|
export { openSse, type SseClient, type SseMessage } from "./sse.js";
|
package/dist/index.js
CHANGED
|
@@ -23,6 +23,7 @@ export { FAKE_EXTENSIONS, fakeExtensionDir, fakeExtensionEntry, packFixture, } f
|
|
|
23
23
|
export { cardRecorded, cookieRequest, createAgent, createConversation, createLocalAccount, createOrg, enableModelProviders, eventsOf, joinOrg, login, pidOwningLocalPort, postMessage, sleep, startTestKernel, TEST_PASSWORD, testKernelConfig, truncateTables, waitFor, waitForEvent, waitForTerminal, } from "./kernel-helpers.js";
|
|
24
24
|
export { FAKE_PNG_BASE64, isPngBase64, pdfBase64, pdfSampleWord, redPngBase64, solidPngBase64, } from "./media-samples.js";
|
|
25
25
|
export { checkDeclaredParameters, describeModelProviderConformance, MEDIA_SAMPLES, thinkingReplayParams, } from "./model-conformance.js";
|
|
26
|
+
export { describePushProviderConformance, } from "./push-conformance.js";
|
|
26
27
|
export { runSandboxNativeConformance, } from "./sandbox-native-conformance.js";
|
|
27
28
|
export { runSandboxPermissionConformance, } from "./sandbox-permission-conformance.js";
|
|
28
29
|
export { openSse } from "./sse.js";
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { type PushProvider } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
/** What the vendor does for one token. */
|
|
3
|
+
export type PushOutcome = "delivered" | "unregistered" | "auth" | "rate_limited" | "transient" | "network_error" | "hang";
|
|
4
|
+
/** A vendor request as the suite checks it (what reached the vendor, not what the provider was given). */
|
|
5
|
+
export interface PushVendorRequest {
|
|
6
|
+
token: string;
|
|
7
|
+
title: string;
|
|
8
|
+
body: string;
|
|
9
|
+
path: string;
|
|
10
|
+
origin: string;
|
|
11
|
+
threadId: string;
|
|
12
|
+
collapseId: string;
|
|
13
|
+
}
|
|
14
|
+
/** The provider under test and its (fake) vendor. */
|
|
15
|
+
export interface PushConformanceTarget {
|
|
16
|
+
/** The plugin name. */
|
|
17
|
+
plugin: string;
|
|
18
|
+
make(): Promise<{
|
|
19
|
+
provider: PushProvider;
|
|
20
|
+
/** Makes the vendor answer `outcome` for `token`. */
|
|
21
|
+
arrange(token: string, outcome: PushOutcome): void;
|
|
22
|
+
/** Every request that reached the vendor. */
|
|
23
|
+
requests(): PushVendorRequest[];
|
|
24
|
+
close(): Promise<void>;
|
|
25
|
+
}>;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Runs the `push-provider@1` conformance suite against one provider and its fake vendor. The suite
|
|
29
|
+
* never sleeps: `hang` settles on the signal's abort event.
|
|
30
|
+
*/
|
|
31
|
+
export declare function describePushProviderConformance(t: PushConformanceTarget): void;
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
import { checkPushResults, PushError, } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { describe, expect, it } from "vitest";
|
|
3
|
+
const MESSAGE = {
|
|
4
|
+
title: "Support agent",
|
|
5
|
+
body: "needs your approval",
|
|
6
|
+
path: "/chat/c1?org=o1",
|
|
7
|
+
threadId: "c1",
|
|
8
|
+
collapseId: "conv-c1-7",
|
|
9
|
+
origin: "https://capybara.example.com",
|
|
10
|
+
};
|
|
11
|
+
const APP_ID = "com.example.app";
|
|
12
|
+
const secretToken = (n) => `SECRET-TOKEN-${n}-do-not-leak`;
|
|
13
|
+
const target1 = (id, token) => ({
|
|
14
|
+
id,
|
|
15
|
+
platform: "ios",
|
|
16
|
+
appId: APP_ID,
|
|
17
|
+
token,
|
|
18
|
+
});
|
|
19
|
+
/**
|
|
20
|
+
* Runs the `push-provider@1` conformance suite against one provider and its fake vendor. The suite
|
|
21
|
+
* never sleeps: `hang` settles on the signal's abort event.
|
|
22
|
+
*/
|
|
23
|
+
export function describePushProviderConformance(t) {
|
|
24
|
+
describe(`push provider conformance: ${t.plugin}`, () => {
|
|
25
|
+
async function run(outcome, fn) {
|
|
26
|
+
const h = await t.make();
|
|
27
|
+
try {
|
|
28
|
+
const token = secretToken(outcome);
|
|
29
|
+
h.arrange(token, outcome);
|
|
30
|
+
let settled;
|
|
31
|
+
const ac = new AbortController();
|
|
32
|
+
if (outcome === "hang")
|
|
33
|
+
queueMicrotask(() => ac.abort());
|
|
34
|
+
try {
|
|
35
|
+
settled = {
|
|
36
|
+
kind: "results",
|
|
37
|
+
results: await h.provider.send({
|
|
38
|
+
registrations: [target1("r1", token)],
|
|
39
|
+
message: MESSAGE,
|
|
40
|
+
signal: ac.signal,
|
|
41
|
+
}),
|
|
42
|
+
};
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
settled = { kind: "threw", error };
|
|
46
|
+
}
|
|
47
|
+
await fn({ settled, token, requests: h.requests() });
|
|
48
|
+
}
|
|
49
|
+
finally {
|
|
50
|
+
await h.close();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/** The one outcome of a call: a result for r1, or the kind of a thrown PushError. */
|
|
54
|
+
const outcomeOf = (s) => {
|
|
55
|
+
if (s.kind === "threw") {
|
|
56
|
+
if (s.error instanceof PushError)
|
|
57
|
+
return { status: "failed", kind: s.error.kind };
|
|
58
|
+
throw s.error;
|
|
59
|
+
}
|
|
60
|
+
expect(checkPushResults([target1("r1", "x")], s.results)).toBeNull();
|
|
61
|
+
const r = s.results[0];
|
|
62
|
+
return r.status === "failed" ? { status: "failed", kind: r.kind } : { status: r.status };
|
|
63
|
+
};
|
|
64
|
+
it("delivered answers delivered and the vendor request carries exactly the message", async () => {
|
|
65
|
+
await run("delivered", ({ settled, token, requests }) => {
|
|
66
|
+
expect(settled).toEqual({
|
|
67
|
+
kind: "results",
|
|
68
|
+
results: [{ registrationId: "r1", status: "delivered" }],
|
|
69
|
+
});
|
|
70
|
+
expect(requests).toEqual([{ token, ...MESSAGE }]);
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
it("unregistered answers unregistered", async () => {
|
|
74
|
+
await run("unregistered", ({ settled }) => {
|
|
75
|
+
expect(outcomeOf(settled)).toEqual({ status: "unregistered" });
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
it.each(["auth", "rate_limited", "transient"])("%s answers failed with that kind", async (outcome) => {
|
|
79
|
+
await run(outcome, ({ settled }) => {
|
|
80
|
+
expect(outcomeOf(settled)).toEqual({ status: "failed", kind: outcome });
|
|
81
|
+
});
|
|
82
|
+
});
|
|
83
|
+
it("a network error yields transient", async () => {
|
|
84
|
+
await run("network_error", ({ settled }) => {
|
|
85
|
+
if (settled.kind === "threw") {
|
|
86
|
+
expect(settled.error).toBeInstanceOf(PushError);
|
|
87
|
+
expect(settled.error.kind).toBe("transient");
|
|
88
|
+
}
|
|
89
|
+
else {
|
|
90
|
+
expect(outcomeOf(settled)).toEqual({ status: "failed", kind: "transient" });
|
|
91
|
+
}
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
it.each([
|
|
95
|
+
"delivered",
|
|
96
|
+
"unregistered",
|
|
97
|
+
"auth",
|
|
98
|
+
"rate_limited",
|
|
99
|
+
"transient",
|
|
100
|
+
"network_error",
|
|
101
|
+
])("%s is requested exactly once (no retries inside)", async (outcome) => {
|
|
102
|
+
await run(outcome, ({ requests, token }) => {
|
|
103
|
+
expect(requests.filter((r) => r.token === token)).toHaveLength(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
it("two registrations in one call get two results", async () => {
|
|
107
|
+
const h = await t.make();
|
|
108
|
+
try {
|
|
109
|
+
const regs = [target1("r1", secretToken("a")), target1("r2", secretToken("b"))];
|
|
110
|
+
for (const r of regs)
|
|
111
|
+
h.arrange(r.token, "delivered");
|
|
112
|
+
const results = await h.provider.send({
|
|
113
|
+
registrations: regs,
|
|
114
|
+
message: MESSAGE,
|
|
115
|
+
signal: new AbortController().signal,
|
|
116
|
+
});
|
|
117
|
+
expect(checkPushResults(regs, results)).toBeNull();
|
|
118
|
+
expect(results).toHaveLength(2);
|
|
119
|
+
}
|
|
120
|
+
finally {
|
|
121
|
+
await h.close();
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
it("hang with an aborted signal settles without a timer", async () => {
|
|
125
|
+
await run("hang", ({ settled }) => {
|
|
126
|
+
if (settled.kind === "threw")
|
|
127
|
+
return;
|
|
128
|
+
expect(outcomeOf(settled)).toEqual({ status: "failed", kind: "transient" });
|
|
129
|
+
});
|
|
130
|
+
});
|
|
131
|
+
it.each([
|
|
132
|
+
"unregistered",
|
|
133
|
+
"auth",
|
|
134
|
+
"rate_limited",
|
|
135
|
+
"transient",
|
|
136
|
+
"network_error",
|
|
137
|
+
"hang",
|
|
138
|
+
])("%s leaks no token in a message", async (outcome) => {
|
|
139
|
+
await run(outcome, ({ settled, token }) => {
|
|
140
|
+
const messages = settled.kind === "threw"
|
|
141
|
+
? [settled.error instanceof Error ? settled.error.message : String(settled.error)]
|
|
142
|
+
: settled.results.flatMap((r) => (r.status === "failed" ? [r.message] : []));
|
|
143
|
+
for (const m of messages)
|
|
144
|
+
expect(m).not.toContain(token);
|
|
145
|
+
});
|
|
146
|
+
});
|
|
147
|
+
it("apps() returns the same array twice and does not touch the vendor", async () => {
|
|
148
|
+
const h = await t.make();
|
|
149
|
+
try {
|
|
150
|
+
const first = h.provider.apps();
|
|
151
|
+
expect(h.provider.apps()).toBe(first);
|
|
152
|
+
expect(Array.isArray(first)).toBe(true);
|
|
153
|
+
expect(h.requests()).toEqual([]);
|
|
154
|
+
}
|
|
155
|
+
finally {
|
|
156
|
+
await h.close();
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
{
|
|
2
|
+
"schemaVersion": 1,
|
|
3
|
+
"name": "fake-push",
|
|
4
|
+
"version": "0.1.0",
|
|
5
|
+
"publisher": "Capybara",
|
|
6
|
+
"description": "Test fake: a scripted push provider (push-provider@1) driven by the token prefix",
|
|
7
|
+
"setupGuides": [
|
|
8
|
+
{
|
|
9
|
+
"id": "fake-push-key",
|
|
10
|
+
"place": "extension-deployment"
|
|
11
|
+
}
|
|
12
|
+
]
|
|
13
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
-- Runs with search_path "plugin_fake_push, public": table names carry no schema prefix.
|
|
2
|
+
-- Every send attempt (one row per registration per call), so dual-replica tests count across processes.
|
|
3
|
+
CREATE TABLE IF NOT EXISTS sends (
|
|
4
|
+
id bigserial PRIMARY KEY,
|
|
5
|
+
registration_id text NOT NULL,
|
|
6
|
+
job_marker text NOT NULL DEFAULT '',
|
|
7
|
+
message jsonb NOT NULL,
|
|
8
|
+
at timestamptz NOT NULL DEFAULT now()
|
|
9
|
+
);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@capybara-hq/test-plugin-fake-push",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": true,
|
|
5
|
+
"description": "Test fake: a scripted push provider (push-provider@1) driven by the token prefix",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"dependencies": {
|
|
9
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.0",
|
|
10
|
+
"zod": "^4.6.7"
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { SetupCheckDefinition } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* The platform setup check of `fake-push` (place `extension-deployment`), registered in
|
|
5
|
+
* `index.ts` as `fake-push-key`: it reads the deployment config's `key` from its target (the
|
|
6
|
+
* extension's own deployment config) and answers without a third-party call:
|
|
7
|
+
*
|
|
8
|
+
* - no key: `failing` (set one);
|
|
9
|
+
* - a key starting with `bad`: `failing`, with a reason and a fix that echo the key (the kernel
|
|
10
|
+
* must redact it; a key of 8 characters or more is redacted);
|
|
11
|
+
* - any other key: `ok`, with a reason naming the plugin and the org the check got (`none` for the
|
|
12
|
+
* platform), so a test sees what the kernel passed.
|
|
13
|
+
*/
|
|
14
|
+
export const keyCheck: SetupCheckDefinition = {
|
|
15
|
+
place: "extension-deployment",
|
|
16
|
+
async run(args) {
|
|
17
|
+
if (args.target.kind !== "extension-deployment") throw new Error("not a deployment target");
|
|
18
|
+
const key = (args.target.config as { key?: unknown } | null)?.key;
|
|
19
|
+
if (typeof key !== "string" || key === "") {
|
|
20
|
+
return {
|
|
21
|
+
status: "failing",
|
|
22
|
+
reason: { en: "No key is set in the deployment config." },
|
|
23
|
+
fix: { en: "Save a key in the deployment config field key." },
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
if (key.startsWith("bad")) {
|
|
27
|
+
return {
|
|
28
|
+
status: "failing",
|
|
29
|
+
reason: { en: `The fake push service refused the key ${key}.` },
|
|
30
|
+
fix: { en: `Replace ${key} with a key from the fake push console.` },
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return {
|
|
34
|
+
status: "ok",
|
|
35
|
+
reason: {
|
|
36
|
+
en: `The fake push service accepted the key of ${args.target.plugin} (org: ${args.orgId ?? "none"}).`,
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
},
|
|
40
|
+
};
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { defineExtension, PUSH_PROVIDER_EXTENSION_POINT, secret } from "@capybara-hq/plugin-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { keyCheck } from "./check.js";
|
|
4
|
+
import { fakePushProbe, makeFakePush } from "./push.js";
|
|
5
|
+
|
|
6
|
+
export { type FakePushSend, fakePushProbe, makeFakePush } from "./push.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Test fake of a push provider (`push-provider@1`), scripted by the token prefix (see
|
|
10
|
+
* `makeFakePush`). Deployment config: `apps` (the app ids it covers, default
|
|
11
|
+
* `["com.example.app"]`) and an optional secret `key`. Every send attempt is recorded in
|
|
12
|
+
* `plugin_fake_push.sends` (counted across replicas) and on `fakePushProbe().sends` (in process).
|
|
13
|
+
* Its platform setup guide (`guides/fake-push-key.md`, place `extension-deployment`) has one check,
|
|
14
|
+
* `fake-push-key` (`check.ts`), which reads `key`.
|
|
15
|
+
*/
|
|
16
|
+
const DeploymentConfig = z.object({
|
|
17
|
+
apps: z.array(z.string().min(1)).default(["com.example.app"]),
|
|
18
|
+
key: secret(z.string()).optional(),
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
export default defineExtension({
|
|
22
|
+
name: "fake-push",
|
|
23
|
+
version: "0.1.0",
|
|
24
|
+
sdk: "^0.1.0-rc.0",
|
|
25
|
+
provides: [PUSH_PROVIDER_EXTENSION_POINT],
|
|
26
|
+
permissions: { setupChecks: ["fake-push-key"] },
|
|
27
|
+
migrations: "../migrations",
|
|
28
|
+
config: { deployment: DeploymentConfig },
|
|
29
|
+
setup(ctx) {
|
|
30
|
+
const cfg = DeploymentConfig.parse(ctx.config);
|
|
31
|
+
const { provider } = makeFakePush({
|
|
32
|
+
apps: cfg.apps,
|
|
33
|
+
probe: fakePushProbe(),
|
|
34
|
+
async onSend(s) {
|
|
35
|
+
await ctx.db
|
|
36
|
+
.insertInto("sends")
|
|
37
|
+
.values({
|
|
38
|
+
registration_id: s.registrationId,
|
|
39
|
+
job_marker: "",
|
|
40
|
+
message: JSON.stringify(s.message),
|
|
41
|
+
})
|
|
42
|
+
.execute();
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
ctx.provide(PUSH_PROVIDER_EXTENSION_POINT, provider);
|
|
46
|
+
ctx.setup.check("fake-push-key", keyCheck);
|
|
47
|
+
},
|
|
48
|
+
});
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import {
|
|
2
|
+
PushError,
|
|
3
|
+
type PushMessage,
|
|
4
|
+
type PushProvider,
|
|
5
|
+
type PushResult,
|
|
6
|
+
type PushTarget,
|
|
7
|
+
} from "@capybara-hq/plugin-sdk";
|
|
8
|
+
|
|
9
|
+
/** One send attempt as the probe records it. */
|
|
10
|
+
export interface FakePushSend {
|
|
11
|
+
registrationId: string;
|
|
12
|
+
token: string;
|
|
13
|
+
message: PushMessage;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The probe tests read: every send attempt of this process (extension instances share it). */
|
|
17
|
+
export function fakePushProbe(): { sends: FakePushSend[] } {
|
|
18
|
+
const g = globalThis as { __fakePush?: { sends: FakePushSend[] } };
|
|
19
|
+
g.__fakePush ??= { sends: [] };
|
|
20
|
+
return g.__fakePush;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/** Rejects with the signal's reason once it aborts. */
|
|
24
|
+
function hang(signal: AbortSignal): Promise<never> {
|
|
25
|
+
return new Promise((_, reject) => {
|
|
26
|
+
if (signal.aborted) reject(signal.reason);
|
|
27
|
+
else signal.addEventListener("abort", () => reject(signal.reason), { once: true });
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const failed = (
|
|
32
|
+
registrationId: string,
|
|
33
|
+
kind: "auth" | "rate_limited" | "transient",
|
|
34
|
+
message: string,
|
|
35
|
+
retryAfterMs?: number,
|
|
36
|
+
): PushResult => ({
|
|
37
|
+
registrationId,
|
|
38
|
+
status: "failed",
|
|
39
|
+
kind,
|
|
40
|
+
message,
|
|
41
|
+
...(retryAfterMs !== undefined && { retryAfterMs }),
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export interface FakePushOptions {
|
|
45
|
+
/** The app ids `apps()` lists (default `["com.example.app"]`). */
|
|
46
|
+
apps?: readonly string[];
|
|
47
|
+
/** Called once per registration per `send` (the extension writes `plugin_fake_push.sends`). */
|
|
48
|
+
onSend?: (send: FakePushSend) => Promise<void> | void;
|
|
49
|
+
/** The probe to record on (default: the process probe). */
|
|
50
|
+
probe?: { sends: FakePushSend[] };
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* The scripted provider, by token prefix: `dead:` unregistered; `auth:` failed auth; `rate:`
|
|
55
|
+
* failed rate_limited (`retryAfterMs` 1000); `flaky:<n>:` transient for the first n calls of that
|
|
56
|
+
* token, then delivered; `throw:` a plain Error; `net:` a thrown transient PushError; `hang:`
|
|
57
|
+
* waits for the signal; anything else delivered. Result messages never contain the token. Usable
|
|
58
|
+
* without a kernel.
|
|
59
|
+
*/
|
|
60
|
+
export function makeFakePush(opts: FakePushOptions = {}): {
|
|
61
|
+
provider: PushProvider;
|
|
62
|
+
sends: FakePushSend[];
|
|
63
|
+
} {
|
|
64
|
+
const apps = Object.freeze([...(opts.apps ?? ["com.example.app"])]);
|
|
65
|
+
const probe = opts.probe ?? { sends: [] };
|
|
66
|
+
const flakyCalls = new Map<string, number>();
|
|
67
|
+
|
|
68
|
+
async function one(
|
|
69
|
+
r: PushTarget,
|
|
70
|
+
message: PushMessage,
|
|
71
|
+
signal: AbortSignal,
|
|
72
|
+
): Promise<PushResult> {
|
|
73
|
+
const send: FakePushSend = { registrationId: r.id, token: r.token, message };
|
|
74
|
+
probe.sends.push(send);
|
|
75
|
+
await opts.onSend?.(send);
|
|
76
|
+
const t = r.token;
|
|
77
|
+
if (t.startsWith("dead:")) return { registrationId: r.id, status: "unregistered" };
|
|
78
|
+
if (t.startsWith("auth:")) return failed(r.id, "auth", "fake-push: credentials rejected");
|
|
79
|
+
if (t.startsWith("rate:")) {
|
|
80
|
+
return failed(r.id, "rate_limited", "fake-push: rate limited", 1000);
|
|
81
|
+
}
|
|
82
|
+
const flaky = /^flaky:(\d+):/.exec(t);
|
|
83
|
+
if (flaky) {
|
|
84
|
+
const n = Number(flaky[1]);
|
|
85
|
+
const seen = flakyCalls.get(t) ?? 0;
|
|
86
|
+
flakyCalls.set(t, seen + 1);
|
|
87
|
+
if (seen < n) return failed(r.id, "transient", "fake-push: temporary failure");
|
|
88
|
+
}
|
|
89
|
+
if (t.startsWith("throw:")) throw new Error("fake-push: scripted failure");
|
|
90
|
+
if (t.startsWith("net:")) {
|
|
91
|
+
throw new PushError("fake-push: network error", { kind: "transient" });
|
|
92
|
+
}
|
|
93
|
+
if (t.startsWith("hang:")) await hang(signal);
|
|
94
|
+
return { registrationId: r.id, status: "delivered" };
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
const provider: PushProvider = {
|
|
98
|
+
apps: () => apps,
|
|
99
|
+
send: ({ registrations, message, signal }) =>
|
|
100
|
+
Promise.all(registrations.map((r) => one(r, message, signal))),
|
|
101
|
+
};
|
|
102
|
+
return { provider, sends: probe.sends };
|
|
103
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@capybara-hq/testing",
|
|
3
|
-
"version": "0.1.0-rc.
|
|
3
|
+
"version": "0.1.0-rc.30",
|
|
4
4
|
"description": "Test helpers for Capybara: test containers, dual-replica harness and fake third-party servers",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"repository": {
|
|
@@ -47,17 +47,17 @@
|
|
|
47
47
|
"tsx": "^4.23.15",
|
|
48
48
|
"ws": "^8.22.0",
|
|
49
49
|
"zod": "^4.6.7",
|
|
50
|
-
"@capybara-hq/kernel": "^0.1.0-rc.
|
|
50
|
+
"@capybara-hq/kernel": "^0.1.0-rc.61"
|
|
51
51
|
},
|
|
52
52
|
"devDependencies": {
|
|
53
53
|
"@types/pg": "^8.23.1",
|
|
54
54
|
"@types/ws": "^8.18.2",
|
|
55
|
-
"@capybara-hq/plugin-client": "^0.1.0-rc.
|
|
56
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
55
|
+
"@capybara-hq/plugin-client": "^0.1.0-rc.7",
|
|
56
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.46"
|
|
57
57
|
},
|
|
58
58
|
"peerDependencies": {
|
|
59
59
|
"vitest": "^5.0.5",
|
|
60
|
-
"@capybara-hq/plugin-sdk": "^0.1.0-rc.
|
|
60
|
+
"@capybara-hq/plugin-sdk": "^0.1.0-rc.46",
|
|
61
61
|
"@capybara-hq/plugin-tools": "^0.1.0-rc.17"
|
|
62
62
|
},
|
|
63
63
|
"peerDependenciesMeta": {
|