@ultimat3/testing 30.9.0 → 31.0.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/CLAUDE.md +46 -55
- package/README.md +179 -39
- package/package.json +23 -17
- package/src/ai-conformance-answer.ts +67 -0
- package/src/ai-gateway-conformance.ts +105 -0
- package/src/ai-provider-conformance.ts +69 -0
- package/src/ai-provider-transport-conformance.ts +170 -0
- package/src/audit-sink-conformance.ts +204 -0
- package/src/auth-adapter-conformance-credentials.ts +124 -0
- package/src/auth-adapter-conformance-mfa.ts +70 -0
- package/src/auth-adapter-conformance-passkeys.ts +133 -0
- package/src/auth-adapter-conformance-sessions.ts +115 -0
- package/src/auth-adapter-conformance-support.ts +71 -0
- package/src/auth-adapter-conformance-users.ts +128 -0
- package/src/auth-adapter-conformance.ts +34 -0
- package/src/auth-limiter-factory-conformance.ts +224 -0
- package/src/auth-server-store-conformance-records.ts +69 -0
- package/src/auth-server-store-conformance.ts +208 -0
- package/src/cache-tier-conformance-checks.ts +142 -0
- package/src/cache-tier-conformance.ts +24 -0
- package/src/cassette-format.ts +8 -13
- package/src/cassette-record.ts +1 -1
- package/src/cdp-browser.ts +10 -11
- package/src/cdp-e2e-page.ts +2 -2
- package/src/cdp-e2e-session.ts +2 -2
- package/src/cdp-network-probe.ts +1 -1
- package/src/cdp-offline-script.ts +2 -2
- package/src/clock-conformance.ts +126 -0
- package/src/conformance.ts +239 -0
- package/src/e2e-browser-handle.ts +1 -2
- package/src/e2e-error-codes.ts +17 -16
- package/src/e2e-run.ts +4 -3
- package/src/embedder-conformance.ts +101 -0
- package/src/entity-conformance-aggregates.ts +210 -0
- package/src/entity-conformance-batches.ts +216 -0
- package/src/entity-conformance-bulk.ts +379 -0
- package/src/entity-conformance-checks.ts +27 -0
- package/src/entity-conformance-entities.ts +52 -0
- package/src/entity-conformance-locks.ts +286 -0
- package/src/entity-conformance-matrix.ts +397 -0
- package/src/entity-conformance-reads.ts +241 -0
- package/src/entity-conformance-support.ts +56 -0
- package/src/entity-conformance-tenancy.ts +296 -0
- package/src/entity-conformance-values.ts +305 -0
- package/src/entity-conformance-versions.ts +60 -0
- package/src/entity-conformance-writes.ts +348 -0
- package/src/entity-driver-conformance.ts +103 -0
- package/src/error-groups-store-conformance.ts +198 -0
- package/src/error-reporter-conformance.ts +119 -0
- package/src/errors.ts +20 -2
- package/src/event-bus-conformance.ts +161 -0
- package/src/exercise-drift-hint.ts +4 -1
- package/src/exercise-judge.ts +5 -3
- package/src/exercise-override-line.ts +98 -0
- package/src/exercise-report.ts +30 -51
- package/src/exercise.ts +12 -6
- package/src/factories.ts +192 -179
- package/src/factory-api.ts +131 -0
- package/src/factory-associations.ts +67 -0
- package/src/factory-errors.ts +38 -0
- package/src/factory-lint.ts +92 -0
- package/src/factory-persist.ts +74 -16
- package/src/factory-pipeline.ts +191 -0
- package/src/factory-registry.ts +2 -1
- package/src/factory-types.ts +131 -0
- package/src/failure-store-conformance.ts +137 -0
- package/src/fixture-jobs.ts +27 -11
- package/src/fixture-mail.ts +7 -7
- package/src/fixture-seed.ts +111 -0
- package/src/fixture-statements.ts +68 -52
- package/src/framework-fixtures.ts +1 -1
- package/src/idempotency-store-conformance.ts +186 -0
- package/src/idempotency-store-replay-conformance.ts +140 -0
- package/src/idempotency-store-tx-conformance.ts +132 -0
- package/src/idempotency-store-world-conformance.ts +82 -0
- package/src/identity-provider-conformance.ts +144 -0
- package/src/image-transform-conformance.ts +89 -0
- package/src/index.ts +68 -157
- package/src/isr-store-conformance.ts +113 -0
- package/src/job-sim-effects.ts +19 -31
- package/src/job-sim-world.ts +6 -15
- package/src/lease-store-conformance.ts +195 -0
- package/src/mail-driver-conformance.ts +108 -0
- package/src/matcher-doubles.ts +104 -0
- package/src/matcher-stub.ts +78 -0
- package/src/matcher-surface.ts +21 -0
- package/src/matchers.ts +19 -1
- package/src/mcp-confirmation-store-conformance.ts +262 -0
- package/src/meter-store-conformance.ts +135 -0
- package/src/notify-digest-conformance.ts +133 -0
- package/src/notify-inbox-conformance.ts +130 -0
- package/src/notify-ledger-conformance.ts +134 -0
- package/src/notify-preference-conformance.ts +49 -0
- package/src/notify-stores-conformance.ts +73 -0
- package/src/outbox-store-conformance-claims.ts +112 -0
- package/src/outbox-store-conformance-subject.ts +65 -0
- package/src/outbox-store-conformance.ts +71 -0
- package/src/per-test-reset.ts +55 -20
- package/src/preload.ts +4 -2
- package/src/purge-conformance.ts +79 -0
- package/src/push-device-conformance.ts +124 -0
- package/src/push-subscription-conformance.ts +123 -0
- package/src/quiet-logs.ts +4 -4
- package/src/rate-limit-store-conformance.ts +178 -0
- package/src/registry-leak-error.ts +8 -1
- package/src/scrape-driver-conformance-frame-checks.ts +97 -0
- package/src/scrape-driver-conformance-page-checks.ts +114 -0
- package/src/scrape-driver-conformance-site.ts +84 -0
- package/src/scrape-driver-conformance.ts +34 -0
- package/src/search-index-conformance-list.ts +76 -0
- package/src/search-index-conformance-support.ts +69 -0
- package/src/search-index-conformance-versions.ts +66 -0
- package/src/search-index-conformance.ts +126 -0
- package/src/secret-source-conformance.ts +91 -0
- package/src/seed-factories.ts +57 -0
- package/src/seed-writer.ts +8 -19
- package/src/semantic-cache-factory-conformance.ts +86 -0
- package/src/sms-transport-conformance.ts +102 -0
- package/src/step-store-conformance.ts +171 -0
- package/src/storage-conformance-read-checks.ts +132 -0
- package/src/storage-conformance-shape.ts +121 -0
- package/src/storage-conformance-stream-checks.ts +226 -0
- package/src/storage-conformance.ts +34 -0
- package/src/stub-action.ts +24 -0
- package/src/stub-errors.ts +86 -0
- package/src/stub-job.ts +30 -0
- package/src/stub-query.ts +25 -0
- package/src/stub-script.ts +76 -0
- package/src/stub-stack.ts +29 -0
- package/src/stub.ts +73 -0
- package/src/swap-adapter.ts +135 -0
- package/src/test-scope.ts +96 -0
- package/src/test-types.ts +1 -1
- package/src/totp-replay-guard-conformance.ts +76 -0
- package/src/transport-conformance.ts +179 -0
- package/src/upload-scanner-conformance.ts +112 -0
- package/src/vector-store-conformance-checks.ts +174 -0
- package/src/vector-store-conformance.ts +21 -0
- package/src/cdp-connection.ts +0 -291
- package/src/cdp-errors.ts +0 -179
- package/src/cdp-launch-attempt.ts +0 -224
- package/src/cdp-launch-candidates.ts +0 -62
- package/src/cdp-launch-profile.ts +0 -106
- package/src/cdp-launch-reap.ts +0 -68
- package/src/cdp-launch-wire.ts +0 -171
- package/src/cdp-launch.ts +0 -175
- package/src/cdp-pipe.ts +0 -77
- package/src/cdp-wire-watch.ts +0 -113
package/src/per-test-reset.ts
CHANGED
|
@@ -1,35 +1,54 @@
|
|
|
1
1
|
// What every test starts from, put back by the preload before each one — the per-TEST half of
|
|
2
|
-
// isolation, beside `file-boundary.ts`'s per-file half
|
|
3
|
-
//
|
|
2
|
+
// isolation, beside `file-boundary.ts`'s per-file half: the jobs event bus, which STORES what it is
|
|
3
|
+
// handed (an answer one test published resumed the next test's waiting run), and the installed
|
|
4
|
+
// rate-limit store, read per request (a bucket one test drained was the next test's 429).
|
|
4
5
|
|
|
5
6
|
import { beforeEach } from 'bun:test';
|
|
6
|
-
import type {
|
|
7
|
+
import type { clearDefaultRateLimitBuckets } from '@ultimat3/http/adapter';
|
|
8
|
+
import type { freshDefaultEventBus, resetEventBus } from '@ultimat3/jobs/adapter';
|
|
9
|
+
import { onFileBoundary } from './file-boundary';
|
|
10
|
+
import { installTestScope } from './test-scope';
|
|
7
11
|
|
|
8
12
|
let installed = false;
|
|
9
13
|
|
|
10
14
|
/**
|
|
11
|
-
*
|
|
12
|
-
*
|
|
13
|
-
*
|
|
15
|
+
* `file` of `pkg`, when something in this process has already loaded it — and only then. A test
|
|
16
|
+
* that never touched the package has nothing to reset, and loading it to reset would put its graph
|
|
17
|
+
* into every `packages/core` test. Resolved once per process: the path does not move.
|
|
14
18
|
*/
|
|
15
|
-
function
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
function loaded(pkg: string, file: string): string | undefined {
|
|
20
|
+
const key = `${pkg}/${file}`;
|
|
21
|
+
let path = paths.get(key);
|
|
22
|
+
if (path === undefined) {
|
|
23
|
+
path = Bun.resolveSync(pkg, import.meta.dir).replace(/index\.ts$/, file);
|
|
24
|
+
paths.set(key, path);
|
|
25
|
+
}
|
|
26
|
+
return path in require.cache ? path : undefined;
|
|
22
27
|
}
|
|
23
28
|
|
|
24
|
-
|
|
29
|
+
const paths = new Map<string, string>();
|
|
25
30
|
|
|
26
|
-
/**
|
|
31
|
+
/**
|
|
32
|
+
* Before each test: a fresh DEFAULT event bus and the DEFAULT rate-limit buckets emptied — each
|
|
33
|
+
* only when its package is loaded. Already evaluated, so each import answers from the registry, the
|
|
34
|
+
* same instance. Only the defaults: a bus or store installed for the file (`beforeAll`, a booted
|
|
35
|
+
* app's) stays installed, and carries across that file's tests as it would in production.
|
|
36
|
+
*/
|
|
27
37
|
export async function resetPerTestState(): Promise<void> {
|
|
28
|
-
const events =
|
|
29
|
-
if (events
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
38
|
+
const events = loaded('@ultimat3/jobs', 'events.ts');
|
|
39
|
+
if (events !== undefined) {
|
|
40
|
+
const jobs = (await import(events)) as {
|
|
41
|
+
readonly freshDefaultEventBus: typeof freshDefaultEventBus;
|
|
42
|
+
};
|
|
43
|
+
jobs.freshDefaultEventBus();
|
|
44
|
+
}
|
|
45
|
+
const limits = loaded('@ultimat3/http', 'rate-limit-installed.ts');
|
|
46
|
+
if (limits !== undefined) {
|
|
47
|
+
const http = (await import(limits)) as {
|
|
48
|
+
readonly clearDefaultRateLimitBuckets: typeof clearDefaultRateLimitBuckets;
|
|
49
|
+
};
|
|
50
|
+
http.clearDefaultRateLimitBuckets();
|
|
51
|
+
}
|
|
33
52
|
}
|
|
34
53
|
|
|
35
54
|
/**
|
|
@@ -40,4 +59,20 @@ export function installPerTestReset(): void {
|
|
|
40
59
|
if (installed) return;
|
|
41
60
|
installed = true;
|
|
42
61
|
beforeEach(resetPerTestState);
|
|
62
|
+
// The test doubles' scope rides the same call, so both preloads — the app's and this repo's own
|
|
63
|
+
// `scripts/test-setup.ts` — put every stub and swapped adapter back with no second install line.
|
|
64
|
+
installTestScope();
|
|
65
|
+
// AFTER the scope's own file hook, so a file's swaps are released first: then the whole bus slot
|
|
66
|
+
// goes back to boot state. A frame a file installed and never released — an `afterEach` that
|
|
67
|
+
// "restored" by installing again — would otherwise outlive the file and hand every later test
|
|
68
|
+
// its events, which the per-test fresh DEFAULT bus underneath cannot reach.
|
|
69
|
+
onFileBoundary(resetEventBusAtFileEnd);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** Synchronous, as a file boundary is: the module is already loaded, so `require` answers it. */
|
|
73
|
+
function resetEventBusAtFileEnd(): void {
|
|
74
|
+
const events = loaded('@ultimat3/jobs', 'events.ts');
|
|
75
|
+
if (events === undefined) return;
|
|
76
|
+
const jobs = require(events) as { readonly resetEventBus: typeof resetEventBus };
|
|
77
|
+
jobs.resetEventBus();
|
|
43
78
|
}
|
package/src/preload.ts
CHANGED
|
@@ -79,10 +79,12 @@ installTabA11yAudit();
|
|
|
79
79
|
// the worker answering 503 X_DRAINING for a reason nothing in that file explains. Each file starts
|
|
80
80
|
// from a live lifecycle, as a file run alone would have it.
|
|
81
81
|
if (Bun.env[ISOLATED_ENV] !== '1') {
|
|
82
|
-
const { forgetHandedOutActionPaths
|
|
82
|
+
const { forgetHandedOutActionPaths } = await import('@ultimat3/action');
|
|
83
83
|
onFileBoundary(forgetHandedOutActionPaths);
|
|
84
|
+
// The meter store's reset is its port's, on the `./adapter` subpath (slice 21).
|
|
85
|
+
const { resetMeterStore } = await import('@ultimat3/action/adapter');
|
|
84
86
|
onFileBoundary(resetMeterStore);
|
|
85
|
-
const { resetRateLimitStore } = await import('@ultimat3/http');
|
|
87
|
+
const { resetRateLimitStore } = await import('@ultimat3/http/adapter');
|
|
86
88
|
onFileBoundary(resetRateLimitStore);
|
|
87
89
|
const { resetLifecycle } = await import('@ultimat3/core');
|
|
88
90
|
onFileBoundary(resetLifecycle);
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
// The `PurgeDriver` contract as shared examples: `behavesLike(purgeDriverConformance, () => ({
|
|
2
|
+
// driver, refused }))`. `driver` reaches a CDN that accepts every purge; `refused`, when the driver
|
|
3
|
+
// can be refused at all, the same driver against one that refuses — a purge that cleared nothing
|
|
4
|
+
// must never resolve as one that did, because no later read can tell.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from 'bun:test';
|
|
7
|
+
import type { PurgeDriver } from '@ultimat3/cache';
|
|
8
|
+
import type { UltimateError } from '@ultimat3/core';
|
|
9
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
10
|
+
import type { SharedExamples } from './shared-examples';
|
|
11
|
+
import { sharedExamples } from './shared-examples';
|
|
12
|
+
|
|
13
|
+
export interface PurgeSubject {
|
|
14
|
+
readonly driver: PurgeDriver;
|
|
15
|
+
/** Omitted by a driver no provider can refuse — `noopPurgeDriver`. */
|
|
16
|
+
readonly refused?: PurgeDriver | undefined;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface PurgeCheck {
|
|
20
|
+
readonly name: string;
|
|
21
|
+
run(subject: PurgeSubject): Promise<void>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** Past Cloudflare's 30-tag and Fastly's 256-key request caps, so batching is part of the claim. */
|
|
25
|
+
const MANY = Array.from({ length: 300 }, (_, at) => `post:${at}`);
|
|
26
|
+
|
|
27
|
+
/** The coded error a call was refused with, or what happened instead, as a string to compare. */
|
|
28
|
+
async function refusalOf(call: () => Promise<unknown>): Promise<UltimateError | string> {
|
|
29
|
+
try {
|
|
30
|
+
await call();
|
|
31
|
+
} catch (caught) {
|
|
32
|
+
return isUltimateError(caught) ? caught : `not-a-coded-error: ${String(caught)}`;
|
|
33
|
+
}
|
|
34
|
+
return 'resolved';
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export const PURGE_CHECKS: readonly PurgeCheck[] = [
|
|
38
|
+
{
|
|
39
|
+
name: 'a driver has a name, and an empty purge accepts nothing',
|
|
40
|
+
async run({ driver }) {
|
|
41
|
+
expect(driver.name).toMatch(/^\S+$/);
|
|
42
|
+
expect(await driver.purge([])).toEqual([]);
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
{
|
|
46
|
+
name: 'a purge the provider takes whole reports every key, unchanged, across every batch',
|
|
47
|
+
async run({ driver }) {
|
|
48
|
+
const keys = ['post', 'post:1', 'feed', 'org:9f3b-1'];
|
|
49
|
+
expect([...(await driver.purge(keys))].sort()).toEqual([...keys].sort());
|
|
50
|
+
expect([...(await driver.purge(MANY))].sort()).toEqual([...MANY].sort());
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
{
|
|
54
|
+
name: 'purgeAll resolves when the provider takes it',
|
|
55
|
+
async run({ driver }) {
|
|
56
|
+
await expect(driver.purgeAll()).resolves.toBeUndefined();
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
name: 'a refused purge and a refused purgeAll are X_CACHE_PURGE_FAILED with a fix, never accepted',
|
|
61
|
+
async run({ refused }) {
|
|
62
|
+
if (refused === undefined) return;
|
|
63
|
+
for (const call of [() => refused.purge(['post']), () => refused.purgeAll()]) {
|
|
64
|
+
const refusal = await refusalOf(call);
|
|
65
|
+
expect(typeof refusal === 'string' ? refusal : refusal.code).toBe('X_CACHE_PURGE_FAILED');
|
|
66
|
+
expect(typeof refusal === 'string' ? '' : refusal.fix).not.toBe('');
|
|
67
|
+
}
|
|
68
|
+
},
|
|
69
|
+
},
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
export const purgeDriverConformance: SharedExamples<PurgeSubject | Promise<PurgeSubject>> =
|
|
73
|
+
sharedExamples('a CDN purge driver', (subject) => {
|
|
74
|
+
for (const check of PURGE_CHECKS) {
|
|
75
|
+
test(check.name, async () => {
|
|
76
|
+
await check.run(await subject());
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
});
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
// The `PushDeviceStore` contract of `@ultimat3/notify` as shared examples:
|
|
2
|
+
// `behavesLike(pushDeviceStoreConformance, () => store)`. A device is keyed `(platform, token)`,
|
|
3
|
+
// never by actor; one order by code point; a remove naming an owner reaches only theirs; a
|
|
4
|
+
// bad-token count that survives a re-register. Every check mints its own tokens and actors.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from 'bun:test';
|
|
7
|
+
import type { PushDeviceRecord, PushDeviceStore } from '@ultimat3/notify';
|
|
8
|
+
import type { SharedExamples } from './shared-examples';
|
|
9
|
+
import { sharedExamples } from './shared-examples';
|
|
10
|
+
|
|
11
|
+
export interface PushDeviceStoreCheck {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
run(store: PushDeviceStore): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
let minted = 0;
|
|
17
|
+
/** A per-check suffix: eight hex digits, so an APNs token built on it is still hex. */
|
|
18
|
+
const mint = (): string => {
|
|
19
|
+
minted += 1;
|
|
20
|
+
return ((process.pid * 4096 + minted) % 0x1_0000_0000).toString(16).padStart(8, '0');
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
interface Scope {
|
|
24
|
+
readonly ana: string;
|
|
25
|
+
readonly ben: string;
|
|
26
|
+
/** An APNs token: 28 repeats of `pair`, then the check's suffix. */
|
|
27
|
+
ios(pair?: string): string;
|
|
28
|
+
readonly android: string;
|
|
29
|
+
record(overrides?: Partial<PushDeviceRecord>): PushDeviceRecord;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const scope = (): Scope => {
|
|
33
|
+
const suffix = mint();
|
|
34
|
+
const ios = (pair = 'ab'): string => `${pair.repeat(28)}${suffix}`;
|
|
35
|
+
const ana = `ana-${suffix}`;
|
|
36
|
+
return {
|
|
37
|
+
ana,
|
|
38
|
+
ben: `ben-${suffix}`,
|
|
39
|
+
ios,
|
|
40
|
+
android: `fcm:${'Z'.repeat(52)}${suffix}`,
|
|
41
|
+
record: (overrides = {}) => ({
|
|
42
|
+
platform: 'ios',
|
|
43
|
+
token: ios(),
|
|
44
|
+
actorId: ana,
|
|
45
|
+
locale: 'es',
|
|
46
|
+
timeZone: 'Europe/Madrid',
|
|
47
|
+
createdAt: Date.parse('2026-11-06T09:00:00Z'),
|
|
48
|
+
...overrides,
|
|
49
|
+
}),
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
export const PUSH_DEVICE_STORE_CHECKS: readonly PushDeviceStoreCheck[] = [
|
|
54
|
+
{
|
|
55
|
+
name: 'a re-register moves the device and keeps its first createdAt',
|
|
56
|
+
async run(store) {
|
|
57
|
+
const s = scope();
|
|
58
|
+
await store.save(s.record());
|
|
59
|
+
await store.save(s.record({ actorId: s.ben, locale: 'de', createdAt: 0 }));
|
|
60
|
+
expect(await store.listFor(s.ana)).toEqual([]);
|
|
61
|
+
expect(await store.listFor(s.ben)).toEqual([s.record({ actorId: s.ben, locale: 'de' })]);
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
{
|
|
65
|
+
name: 'one order, (platform, token) by code point',
|
|
66
|
+
async run(store) {
|
|
67
|
+
const s = scope();
|
|
68
|
+
// Upper case sorts before lower case by code point, and after it in most locales.
|
|
69
|
+
for (const pair of ['cd', 'ab', 'AB']) await store.save(s.record({ token: s.ios(pair) }));
|
|
70
|
+
await store.save(s.record({ platform: 'android', token: s.android }));
|
|
71
|
+
const listed = await store.listFor(s.ana);
|
|
72
|
+
expect(listed.map((d) => `${d.platform}:${d.token.slice(0, 2)}`)).toEqual([
|
|
73
|
+
'android:fc',
|
|
74
|
+
'ios:AB',
|
|
75
|
+
'ios:ab',
|
|
76
|
+
'ios:cd',
|
|
77
|
+
]);
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
name: 'a remove naming another owner is absent; the sender’s remove reaches anyone',
|
|
82
|
+
async run(store) {
|
|
83
|
+
const s = scope();
|
|
84
|
+
await store.save(s.record());
|
|
85
|
+
expect(await store.remove('ios', s.ios(), s.ben)).toBe(false);
|
|
86
|
+
expect(await store.remove('android', s.ios())).toBe(false);
|
|
87
|
+
expect(await store.remove('ios', s.ios())).toBe(true);
|
|
88
|
+
expect(await store.remove('ios', s.ios())).toBe(false);
|
|
89
|
+
expect(await store.listFor(s.ana)).toEqual([]);
|
|
90
|
+
},
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
name: 'bad-token answers count up, survive a re-register, and a clear or a missing row is 0',
|
|
94
|
+
async run(store) {
|
|
95
|
+
const s = scope();
|
|
96
|
+
await store.save(s.record());
|
|
97
|
+
expect(await store.noteBadToken('ios', s.ios())).toBe(1);
|
|
98
|
+
expect(await store.noteBadToken('ios', s.ios())).toBe(2);
|
|
99
|
+
await store.save(s.record({ locale: 'de' }));
|
|
100
|
+
expect(await store.listFor(s.ana)).toEqual([s.record({ locale: 'de', badTokens: 2 })]);
|
|
101
|
+
await store.clearBadToken('ios', s.ios());
|
|
102
|
+
expect(await store.listFor(s.ana)).toEqual([s.record({ locale: 'de' })]);
|
|
103
|
+
expect(await store.noteBadToken('android', s.ios())).toBe(0);
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: 'a caller’s badTokens on save is ignored: only the store counts',
|
|
108
|
+
async run(store) {
|
|
109
|
+
const s = scope();
|
|
110
|
+
await store.save(s.record({ badTokens: 7 }));
|
|
111
|
+
expect(await store.listFor(s.ana)).toEqual([s.record()]);
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
];
|
|
115
|
+
|
|
116
|
+
export const pushDeviceStoreConformance: SharedExamples<
|
|
117
|
+
PushDeviceStore | Promise<PushDeviceStore>
|
|
118
|
+
> = sharedExamples('a push device store', (subject) => {
|
|
119
|
+
for (const check of PUSH_DEVICE_STORE_CHECKS) {
|
|
120
|
+
test(check.name, async () => {
|
|
121
|
+
await check.run(await subject());
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
});
|
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
// The `PushSubscriptionStore` contract of `@ultimat3/pwa` as shared examples:
|
|
2
|
+
// `behavesLike(pushSubscriptionStoreConformance, () => store)`. A subscription is keyed by its
|
|
3
|
+
// endpoint, never by actor; one order by code point; a remove naming an owner reaches only theirs.
|
|
4
|
+
// Every check mints its own endpoints and actors, so one shared table serves them all.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from 'bun:test';
|
|
7
|
+
import { isUltimateError } from '@ultimat3/core';
|
|
8
|
+
import type { PushSubscriptionRecord, PushSubscriptionStore } from '@ultimat3/pwa';
|
|
9
|
+
import type { SharedExamples } from './shared-examples';
|
|
10
|
+
import { sharedExamples } from './shared-examples';
|
|
11
|
+
|
|
12
|
+
export interface PushSubscriptionStoreCheck {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
run(store: PushSubscriptionStore): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const AT = Date.parse('2026-10-08T12:00:00.000Z');
|
|
18
|
+
|
|
19
|
+
let minted = 0;
|
|
20
|
+
interface Scope {
|
|
21
|
+
readonly ana: string;
|
|
22
|
+
readonly ben: string;
|
|
23
|
+
endpoint(path: string): string;
|
|
24
|
+
record(
|
|
25
|
+
path: string,
|
|
26
|
+
actorId: string,
|
|
27
|
+
overrides?: Partial<PushSubscriptionRecord>,
|
|
28
|
+
): PushSubscriptionRecord;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const scope = (): Scope => {
|
|
32
|
+
minted += 1;
|
|
33
|
+
const id = `${process.pid.toString(36)}-${minted.toString(36)}`;
|
|
34
|
+
const endpoint = (path: string): string => `https://push.test/${id}/${path}`;
|
|
35
|
+
return {
|
|
36
|
+
ana: `ana-${id}`,
|
|
37
|
+
ben: `ben-${id}`,
|
|
38
|
+
endpoint,
|
|
39
|
+
record: (path, actorId, overrides = {}) => ({
|
|
40
|
+
endpoint: endpoint(path),
|
|
41
|
+
keys: { p256dh: `p-${path}`, auth: `a-${path}` },
|
|
42
|
+
locale: 'en',
|
|
43
|
+
timeZone: 'Europe/Berlin',
|
|
44
|
+
actorId,
|
|
45
|
+
createdAt: AT,
|
|
46
|
+
expirationTime: null,
|
|
47
|
+
...overrides,
|
|
48
|
+
}),
|
|
49
|
+
};
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export const PUSH_SUBSCRIPTION_STORE_CHECKS: readonly PushSubscriptionStoreCheck[] = [
|
|
53
|
+
{
|
|
54
|
+
name: 'one row per endpoint: a re-subscribe hands the device over with its new keys, first createdAt kept',
|
|
55
|
+
async run(store) {
|
|
56
|
+
const s = scope();
|
|
57
|
+
await store.save(s.record('b', s.ana));
|
|
58
|
+
await store.save(s.record('a', s.ana));
|
|
59
|
+
const handed = { locale: 'de', keys: { p256dh: 'p-new', auth: 'a-new' } };
|
|
60
|
+
await store.save(s.record('b', s.ben, { ...handed, createdAt: AT + 5 }));
|
|
61
|
+
expect((await store.listFor(s.ana)).map((row) => row.endpoint)).toEqual([s.endpoint('a')]);
|
|
62
|
+
expect(await store.listFor(s.ben)).toEqual([s.record('b', s.ben, handed)]);
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
name: 'one order, the endpoint by code point',
|
|
67
|
+
async run(store) {
|
|
68
|
+
const s = scope();
|
|
69
|
+
for (const path of ['b', 'B', 'a', 'é']) await store.save(s.record(path, s.ana));
|
|
70
|
+
const listed = (await store.listFor(s.ana)).map((row) => row.endpoint.slice(-1));
|
|
71
|
+
expect(listed).toEqual(['B', 'a', 'b', 'é']);
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
{
|
|
75
|
+
name: 'an expiration time round-trips as epoch milliseconds',
|
|
76
|
+
async run(store) {
|
|
77
|
+
const s = scope();
|
|
78
|
+
await store.save(s.record('a', s.ana, { expirationTime: AT + 86_400_000 }));
|
|
79
|
+
expect(await store.listFor(s.ana)).toEqual([
|
|
80
|
+
s.record('a', s.ana, { expirationTime: AT + 86_400_000 }),
|
|
81
|
+
]);
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
// `actorId` is `string`, so only untyped code gets here — and the memory store used to accept
|
|
86
|
+
// what the Postgres column refuses. One refusal, one code, nothing written.
|
|
87
|
+
name: 'a subscription with no actor is refused with X_PWA_PUSH_SUBSCRIPTION_INVALID, nothing stored',
|
|
88
|
+
async run(store) {
|
|
89
|
+
const s = scope();
|
|
90
|
+
for (const actorId of [null, '']) {
|
|
91
|
+
const anonymous = s.record('a', actorId as unknown as string);
|
|
92
|
+
const code = await store.save(anonymous).then(
|
|
93
|
+
() => 'stored',
|
|
94
|
+
(error: unknown) => (isUltimateError(error) ? error.code : 'not an UltimateError'),
|
|
95
|
+
);
|
|
96
|
+
expect(code).toBe('X_PWA_PUSH_SUBSCRIPTION_INVALID');
|
|
97
|
+
}
|
|
98
|
+
expect(await store.remove(s.endpoint('a'))).toBe(false);
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
name: 'an owner-scoped remove leaves another actor’s device; an unscoped one is the 410',
|
|
103
|
+
async run(store) {
|
|
104
|
+
const s = scope();
|
|
105
|
+
await store.save(s.record('a', s.ana));
|
|
106
|
+
expect(await store.remove(s.endpoint('a'), s.ben)).toBe(false);
|
|
107
|
+
expect(await store.listFor(s.ana)).toHaveLength(1);
|
|
108
|
+
expect(await store.remove(s.endpoint('a'))).toBe(true);
|
|
109
|
+
expect(await store.remove(s.endpoint('a'))).toBe(false);
|
|
110
|
+
expect(await store.listFor(s.ana)).toEqual([]);
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
export const pushSubscriptionStoreConformance: SharedExamples<
|
|
116
|
+
PushSubscriptionStore | Promise<PushSubscriptionStore>
|
|
117
|
+
> = sharedExamples('a push subscription store', (subject) => {
|
|
118
|
+
for (const check of PUSH_SUBSCRIPTION_STORE_CHECKS) {
|
|
119
|
+
test(check.name, async () => {
|
|
120
|
+
await check.run(await subject());
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
});
|
package/src/quiet-logs.ts
CHANGED
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
// `info` for the one moment core loads, and what the author named decides what is PRINTED.
|
|
13
13
|
//
|
|
14
14
|
// A test that asserts on log output hands `structuredLogger({ level, writer })` its own writer, or
|
|
15
|
-
// installs its own `
|
|
15
|
+
// installs its own with `installLogSink` and calls the release it returns. Never `process.stdout`.
|
|
16
16
|
|
|
17
17
|
import type { LogLevel } from '@ultimat3/core';
|
|
18
18
|
|
|
@@ -42,15 +42,15 @@ const routing = logRouting(named);
|
|
|
42
42
|
// import and the author's value goes back after it: a child this process spawns inherits what
|
|
43
43
|
// was named, never the pin. A dynamic import, because a static one is hoisted above this line.
|
|
44
44
|
if (routing.lines === 'filtered') Bun.env['LOG_LEVEL'] = 'info';
|
|
45
|
-
const {
|
|
45
|
+
const { installLogSink } = await import('@ultimat3/core');
|
|
46
46
|
|
|
47
47
|
if (routing.lines === 'dropped') {
|
|
48
|
-
|
|
48
|
+
installLogSink(() => undefined);
|
|
49
49
|
} else if (routing.lines === 'filtered') {
|
|
50
50
|
Bun.env['LOG_LEVEL'] = routing.from;
|
|
51
51
|
const shown = new Set<LogLevel>(ABOVE_INFO.slice(ABOVE_INFO.indexOf(routing.from)));
|
|
52
52
|
// Core's own split: `error` and above to stderr, the rest to stdout.
|
|
53
|
-
|
|
53
|
+
installLogSink((line, level) => {
|
|
54
54
|
if (shown.has(level)) (level === 'warn' ? process.stdout : process.stderr).write(`${line}\n`);
|
|
55
55
|
});
|
|
56
56
|
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
// The `RateLimitStore` contract of `@ultimat3/http` as shared examples, for a store of your own:
|
|
2
|
+
// `behavesLike(rateLimitStoreConformance, () => store)`. Asserted through the port only — the
|
|
3
|
+
// numbers every driver owes (`rateLimitDecision`'s maths), the read that writes nothing, the
|
|
4
|
+
// reset that touches one key, and the race a shared counter exists to win.
|
|
5
|
+
|
|
6
|
+
import { expect, test } from 'bun:test';
|
|
7
|
+
import type { Bucket, RateLimitStore } from '@ultimat3/http/adapter';
|
|
8
|
+
import type { SharedExamples } from './shared-examples';
|
|
9
|
+
import { sharedExamples } from './shared-examples';
|
|
10
|
+
|
|
11
|
+
export interface RateLimitStoreCheck {
|
|
12
|
+
readonly name: string;
|
|
13
|
+
run(store: RateLimitStore): Promise<void>;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** Far from zero and from the host clock, so a store reading `Date.now()` instead of `nowMs` shows. */
|
|
17
|
+
const NOW = 1_700_000_000_000;
|
|
18
|
+
const FOUR: Bucket = { capacity: 4, refillPerSecond: 1 };
|
|
19
|
+
const HALVES: Bucket = { capacity: 3, refillPerSecond: 0.5 };
|
|
20
|
+
const RACERS = 16;
|
|
21
|
+
|
|
22
|
+
/** A key no other check, run or process shares — a shared table holds every run's buckets. */
|
|
23
|
+
const keyOf = (label: string): string => `conformance:${label}:${crypto.randomUUID()}`;
|
|
24
|
+
|
|
25
|
+
const spend = async (store: RateLimitStore, key: string, bucket: Bucket, n: number) => {
|
|
26
|
+
for (let index = 0; index < n; index += 1) await store.take(key, bucket, 1, NOW);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const RATE_LIMIT_STORE_CHECKS: readonly RateLimitStoreCheck[] = [
|
|
30
|
+
{
|
|
31
|
+
name: 'declares where its counters live',
|
|
32
|
+
async run(store) {
|
|
33
|
+
expect(['process', 'shared']).toContain(store.scope);
|
|
34
|
+
},
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'a bucket of four admits four takes at one instant, then refuses and says when',
|
|
38
|
+
async run(store) {
|
|
39
|
+
const key = keyOf('burst');
|
|
40
|
+
const remaining: number[] = [];
|
|
41
|
+
for (let index = 0; index < 4; index += 1) {
|
|
42
|
+
const decision = await store.take(key, FOUR, 1, NOW);
|
|
43
|
+
expect([decision.allowed, decision.limit, decision.retryAfterSeconds]).toEqual([
|
|
44
|
+
true,
|
|
45
|
+
4,
|
|
46
|
+
0,
|
|
47
|
+
]);
|
|
48
|
+
remaining.push(decision.remaining);
|
|
49
|
+
}
|
|
50
|
+
expect(remaining).toEqual([3, 2, 1, 0]);
|
|
51
|
+
expect(await store.take(key, FOUR, 1, NOW)).toEqual({
|
|
52
|
+
allowed: false,
|
|
53
|
+
limit: 4,
|
|
54
|
+
remaining: 0,
|
|
55
|
+
resetAtMs: NOW + 1_000,
|
|
56
|
+
retryAfterSeconds: 1,
|
|
57
|
+
});
|
|
58
|
+
},
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
name: 'tokens return at refillPerSecond and stop at capacity',
|
|
62
|
+
async run(store) {
|
|
63
|
+
const key = keyOf('refill');
|
|
64
|
+
await spend(store, key, FOUR, 4);
|
|
65
|
+
expect(await store.take(key, FOUR, 1, NOW + 1_000)).toMatchObject({
|
|
66
|
+
allowed: true,
|
|
67
|
+
remaining: 0,
|
|
68
|
+
});
|
|
69
|
+
// A minute is sixty tokens of refill against a bucket of four: capped, never banked.
|
|
70
|
+
expect(await store.take(key, FOUR, 1, NOW + 61_000)).toMatchObject({
|
|
71
|
+
allowed: true,
|
|
72
|
+
remaining: 3,
|
|
73
|
+
});
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: 'a cost above one spends that many, and a refused cost spends nothing',
|
|
78
|
+
async run(store) {
|
|
79
|
+
const key = keyOf('cost');
|
|
80
|
+
expect(await store.take(key, FOUR, 3, NOW)).toMatchObject({ allowed: true, remaining: 1 });
|
|
81
|
+
expect(await store.take(key, FOUR, 2, NOW)).toMatchObject({
|
|
82
|
+
allowed: false,
|
|
83
|
+
remaining: 1,
|
|
84
|
+
retryAfterSeconds: 1,
|
|
85
|
+
});
|
|
86
|
+
expect(await store.take(key, FOUR, 1, NOW)).toMatchObject({ allowed: true, remaining: 0 });
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
name: 'a fractional token survives between takes — half a token left is not none',
|
|
91
|
+
async run(store) {
|
|
92
|
+
const key = keyOf('half');
|
|
93
|
+
await spend(store, key, HALVES, 3);
|
|
94
|
+
const refused = await store.take(key, HALVES, 1, NOW + 1_000);
|
|
95
|
+
expect([refused.allowed, refused.retryAfterSeconds]).toEqual([false, 1]);
|
|
96
|
+
expect((await store.take(key, HALVES, 1, NOW + 2_000)).allowed).toBe(true);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'a clock behind the stored instant refills nothing',
|
|
101
|
+
async run(store) {
|
|
102
|
+
const key = keyOf('skew');
|
|
103
|
+
for (let index = 0; index < 4; index += 1) await store.take(key, FOUR, 1, NOW + 10_000);
|
|
104
|
+
expect((await store.take(key, FOUR, 1, NOW)).allowed).toBe(false);
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: 'a five-per-week bucket is still spent a day and a fifth later',
|
|
109
|
+
async run(store) {
|
|
110
|
+
const key = keyOf('weekly');
|
|
111
|
+
const weekly: Bucket = { capacity: 5, refillPerSecond: 5 / (7 * 86_400) };
|
|
112
|
+
await spend(store, key, weekly, 5);
|
|
113
|
+
expect((await store.take(key, weekly, 1, NOW + 1.2 * 86_400_000)).allowed).toBe(false);
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: 'peek answers full for a key never taken, and refill-applied after a spend',
|
|
118
|
+
async run(store) {
|
|
119
|
+
const key = keyOf('peek');
|
|
120
|
+
expect(await store.peek(key, FOUR, NOW)).toEqual({ remaining: 4, retryAfterSeconds: 0 });
|
|
121
|
+
await spend(store, key, FOUR, 4);
|
|
122
|
+
expect(await store.peek(key, FOUR, NOW)).toEqual({ remaining: 0, retryAfterSeconds: 1 });
|
|
123
|
+
expect(await store.peek(key, FOUR, NOW + 2_500)).toEqual({
|
|
124
|
+
remaining: 2,
|
|
125
|
+
retryAfterSeconds: 0,
|
|
126
|
+
});
|
|
127
|
+
},
|
|
128
|
+
},
|
|
129
|
+
{
|
|
130
|
+
name: 'peek spends nothing: a full allowance is still there after many peeks',
|
|
131
|
+
async run(store) {
|
|
132
|
+
const key = keyOf('peek-free');
|
|
133
|
+
for (let index = 0; index < 6; index += 1) await store.peek(key, FOUR, NOW);
|
|
134
|
+
await store.take(key, FOUR, 1, NOW);
|
|
135
|
+
for (let index = 0; index < 6; index += 1) await store.peek(key, FOUR, NOW);
|
|
136
|
+
expect(await store.take(key, FOUR, 3, NOW)).toMatchObject({ allowed: true, remaining: 0 });
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: 'reset empties one key and leaves every other key spent',
|
|
141
|
+
async run(store) {
|
|
142
|
+
const [target, other] = [keyOf('reset'), keyOf('untouched')];
|
|
143
|
+
await spend(store, target, FOUR, 4);
|
|
144
|
+
await spend(store, other, FOUR, 4);
|
|
145
|
+
await store.reset(target);
|
|
146
|
+
expect((await store.take(target, FOUR, 1, NOW)).remaining).toBe(3);
|
|
147
|
+
// A reset that refilled a neighbour is one caller handing every other caller an allowance.
|
|
148
|
+
expect((await store.take(other, FOUR, 1, NOW)).allowed).toBe(false);
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
name: 'one key spent is no other key spent',
|
|
153
|
+
async run(store) {
|
|
154
|
+
const [first, second] = [keyOf('one'), keyOf('two')];
|
|
155
|
+
await spend(store, first, FOUR, 4);
|
|
156
|
+
expect((await store.take(second, FOUR, 1, NOW)).remaining).toBe(3);
|
|
157
|
+
},
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
name: `${RACERS} concurrent takes against a bucket of four admit exactly four`,
|
|
161
|
+
async run(store) {
|
|
162
|
+
const key = keyOf('race');
|
|
163
|
+
const decisions = await Promise.all(
|
|
164
|
+
Array.from({ length: RACERS }, () => store.take(key, FOUR, 1, NOW)),
|
|
165
|
+
);
|
|
166
|
+
expect(decisions.filter((decision) => decision.allowed)).toHaveLength(4);
|
|
167
|
+
},
|
|
168
|
+
},
|
|
169
|
+
];
|
|
170
|
+
|
|
171
|
+
export const rateLimitStoreConformance: SharedExamples<RateLimitStore | Promise<RateLimitStore>> =
|
|
172
|
+
sharedExamples('a rate-limit store', (subject) => {
|
|
173
|
+
for (const check of RATE_LIMIT_STORE_CHECKS) {
|
|
174
|
+
test(check.name, async () => {
|
|
175
|
+
await check.run(await subject());
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
});
|
|
@@ -31,9 +31,16 @@ const FILE_PLACEHOLDER = '<the file the cause names>';
|
|
|
31
31
|
*/
|
|
32
32
|
const DOUBLE_QUOTE_LIVE = /[$`!]/;
|
|
33
33
|
|
|
34
|
+
/**
|
|
35
|
+
* A relative path gets `./`: to `bun test` a bare one is a SUBSTRING filter, so
|
|
36
|
+
* `packages/cli/x.test.ts` also ran `examples/dummy/packages/cli/x.test.ts` under the wrong config.
|
|
37
|
+
*/
|
|
38
|
+
const anchored = (file: string): string =>
|
|
39
|
+
file.startsWith('/') || file.startsWith('./') || file.startsWith('../') ? file : `./${file}`;
|
|
40
|
+
|
|
34
41
|
const rerunArg = (file: string): string =>
|
|
35
42
|
isFixShellSafe(file) || !DOUBLE_QUOTE_LIVE.test(file)
|
|
36
|
-
? renderFixLiteral(file, FILE_PLACEHOLDER)
|
|
43
|
+
? renderFixLiteral(anchored(file), FILE_PLACEHOLDER)
|
|
37
44
|
: `'${FILE_PLACEHOLDER}'`;
|
|
38
45
|
|
|
39
46
|
/** Every leaked file as `bun test` arguments — one screen, so the fix-shell-arg guard can see it. */
|