@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/matchers.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { ExpectExtendMatchers } from 'bun:test';
|
|
|
6
6
|
import { expect } from 'bun:test';
|
|
7
7
|
import { describeValue, isUltimateError, renderCauseValue, stringField } from '@ultimat3/core';
|
|
8
8
|
import { TestJobExpectedError, TestSchemaExpectedError } from './errors';
|
|
9
|
+
import { toHaveEnqueued, toHaveSentMail } from './matcher-doubles';
|
|
9
10
|
import {
|
|
10
11
|
TestNumberExpectedError,
|
|
11
12
|
TestOpenApiExpectedError,
|
|
@@ -13,9 +14,17 @@ import {
|
|
|
13
14
|
} from './matcher-receiver-errors';
|
|
14
15
|
import type { MatcherResult } from './matcher-result';
|
|
15
16
|
import { rowEquality } from './matcher-row';
|
|
16
|
-
import type {
|
|
17
|
+
import type {
|
|
18
|
+
EnqueuedJobRef,
|
|
19
|
+
MatcherMailRef,
|
|
20
|
+
SentMailMatch,
|
|
21
|
+
UltimateMatchers,
|
|
22
|
+
} from './matcher-surface';
|
|
17
23
|
import type { VisibleOptions } from './matcher-visible';
|
|
18
24
|
import { assertVisibilityProbe, visibilityResult } from './matcher-visible';
|
|
25
|
+
// `toHaveBeenCalledWith` on a `stub()`: bun's own name, so it is installed beside bun's matcher
|
|
26
|
+
// rather than declared on `UltimateMatchers` (two declarations of one member do not merge).
|
|
27
|
+
import './matcher-stub';
|
|
19
28
|
import type { OpenApiLike } from './test-types';
|
|
20
29
|
|
|
21
30
|
export type { MatcherResult } from './matcher-result';
|
|
@@ -302,6 +311,15 @@ const implementations: ExpectExtendMatchers<UltimateMatchers<unknown>> = {
|
|
|
302
311
|
return rowEquality(received, expected);
|
|
303
312
|
},
|
|
304
313
|
|
|
314
|
+
// Not `async`: the receiver and input checks throw synchronously; the read is the promise.
|
|
315
|
+
toHaveEnqueued(received: unknown, job: EnqueuedJobRef<unknown>, ...input: unknown[]) {
|
|
316
|
+
return toHaveEnqueued(received, job, ...input);
|
|
317
|
+
},
|
|
318
|
+
|
|
319
|
+
toHaveSentMail(received: unknown, mail: MatcherMailRef, match?: SentMailMatch) {
|
|
320
|
+
return toHaveSentMail(received, mail, match);
|
|
321
|
+
},
|
|
322
|
+
|
|
305
323
|
toAcceptInput(received: unknown, input: unknown) {
|
|
306
324
|
const schema = assertStandardSchema(received);
|
|
307
325
|
return hasIssues(schema, input).then((rejected) =>
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
// The `McpConfirmationStore` contract of `@ultimat3/mcp` as shared examples, structurally (an
|
|
2
|
+
// `McpConfirmationStore` is assignable): `behavesLike(mcpConfirmationStoreConformance, () => fresh())`.
|
|
3
|
+
// Every transition answers alike on every store. The subject must be EMPTY per check: `purge` counts.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'bun:test';
|
|
6
|
+
import { uuidV7 } from '@ultimat3/core';
|
|
7
|
+
import type { SharedExamples } from './shared-examples';
|
|
8
|
+
import { sharedExamples } from './shared-examples';
|
|
9
|
+
|
|
10
|
+
type Maybe<T> = T | Promise<T>;
|
|
11
|
+
|
|
12
|
+
/** `@ultimat3/mcp`'s `McpConfirmation`, the fields the checks read. */
|
|
13
|
+
export interface McpConfirmationLike {
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly orgId: string | null;
|
|
16
|
+
readonly status: 'pending' | 'approved' | 'rejected';
|
|
17
|
+
readonly expiresAt: Date;
|
|
18
|
+
readonly decidedAt: Date | null;
|
|
19
|
+
readonly decidedBy: string | null;
|
|
20
|
+
readonly quorum: number;
|
|
21
|
+
readonly approvers: readonly string[] | null;
|
|
22
|
+
readonly approvals: readonly {
|
|
23
|
+
readonly approver: string;
|
|
24
|
+
readonly at: Date;
|
|
25
|
+
readonly signature: string | null;
|
|
26
|
+
}[];
|
|
27
|
+
readonly consumedAt: Date | null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** `@ultimat3/mcp`'s `McpConfirmationDraft`, structurally. */
|
|
31
|
+
export interface McpConfirmationDraftLike {
|
|
32
|
+
readonly id: string;
|
|
33
|
+
readonly actorId: string;
|
|
34
|
+
readonly orgId: string | null;
|
|
35
|
+
readonly tool: string;
|
|
36
|
+
readonly inputDigest: string;
|
|
37
|
+
readonly sealedArguments: string;
|
|
38
|
+
readonly createdAt: Date;
|
|
39
|
+
readonly expiresAt: Date;
|
|
40
|
+
readonly quorum?: number | undefined;
|
|
41
|
+
readonly approvers?: readonly string[] | null | undefined;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/** The five members of `@ultimat3/mcp`'s `McpConfirmationStore`, structurally. */
|
|
45
|
+
export interface McpConfirmationStoreLike {
|
|
46
|
+
open(
|
|
47
|
+
draft: McpConfirmationDraftLike,
|
|
48
|
+
): Promise<{ readonly row: McpConfirmationLike; readonly created: boolean }>;
|
|
49
|
+
get(id: string): Promise<McpConfirmationLike | undefined>;
|
|
50
|
+
decide(
|
|
51
|
+
id: string,
|
|
52
|
+
status: 'approved' | 'rejected',
|
|
53
|
+
by: string,
|
|
54
|
+
at: Date,
|
|
55
|
+
signature: string | null,
|
|
56
|
+
): Promise<McpConfirmationLike | undefined>;
|
|
57
|
+
consume(id: string, at: Date): Promise<boolean>;
|
|
58
|
+
purge(before: Date): Promise<number>;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export interface McpConfirmationStoreCheck {
|
|
62
|
+
readonly name: string;
|
|
63
|
+
run(store: McpConfirmationStoreLike): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const T0 = Date.parse('2026-10-06T09:00:00.000Z');
|
|
67
|
+
const at = (seconds: number): Date => new Date(T0 + seconds * 1000);
|
|
68
|
+
|
|
69
|
+
const draft = (over: Partial<McpConfirmationDraftLike> = {}): McpConfirmationDraftLike => ({
|
|
70
|
+
id: uuidV7(),
|
|
71
|
+
actorId: 'agent-1',
|
|
72
|
+
orgId: 'org-1',
|
|
73
|
+
tool: 'refundOrder',
|
|
74
|
+
inputDigest: 'a'.repeat(64),
|
|
75
|
+
sealedArguments: 'x1.sealed-elsewhere',
|
|
76
|
+
createdAt: at(0),
|
|
77
|
+
expiresAt: at(600),
|
|
78
|
+
...over,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
const OPEN: readonly McpConfirmationStoreCheck[] = [
|
|
82
|
+
{
|
|
83
|
+
name: 'open creates one pending row, and a second open for the same call returns it',
|
|
84
|
+
async run(store) {
|
|
85
|
+
const first = await store.open(draft());
|
|
86
|
+
expect(first.created).toBe(true);
|
|
87
|
+
expect(first.row).toMatchObject({
|
|
88
|
+
status: 'pending',
|
|
89
|
+
decidedAt: null,
|
|
90
|
+
consumedAt: null,
|
|
91
|
+
sealedArguments: 'x1.sealed-elsewhere',
|
|
92
|
+
});
|
|
93
|
+
expect(first.row.expiresAt.getTime()).toBe(at(600).getTime());
|
|
94
|
+
const again = await store.open(draft());
|
|
95
|
+
expect(again.created).toBe(false);
|
|
96
|
+
expect(again.row.id).toBe(first.row.id);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'two identical opens racing open one row',
|
|
101
|
+
async run(store) {
|
|
102
|
+
const both = await Promise.all([store.open(draft()), store.open(draft())]);
|
|
103
|
+
expect(both.filter((each) => each.created)).toHaveLength(1);
|
|
104
|
+
expect(new Set(both.map((each) => each.row.id)).size).toBe(1);
|
|
105
|
+
},
|
|
106
|
+
},
|
|
107
|
+
{
|
|
108
|
+
name: 'a different actor, org, tool or input is a different call',
|
|
109
|
+
async run(store) {
|
|
110
|
+
const base = await store.open(draft());
|
|
111
|
+
for (const over of [
|
|
112
|
+
{ actorId: 'agent-2' },
|
|
113
|
+
{ orgId: 'org-2' },
|
|
114
|
+
{ orgId: null },
|
|
115
|
+
{ orgId: '' },
|
|
116
|
+
{ tool: 'other' },
|
|
117
|
+
{ inputDigest: 'b'.repeat(64) },
|
|
118
|
+
]) {
|
|
119
|
+
const other = await store.open(draft(over));
|
|
120
|
+
expect(other.created).toBe(true);
|
|
121
|
+
expect(other.row.id).not.toBe(base.row.id);
|
|
122
|
+
}
|
|
123
|
+
},
|
|
124
|
+
},
|
|
125
|
+
{
|
|
126
|
+
name: 'an approval in one org is not the open row of the same actor and call in another',
|
|
127
|
+
async run(store) {
|
|
128
|
+
const { row } = await store.open(draft({ orgId: 'o1' }));
|
|
129
|
+
await store.decide(row.id, 'approved', 'user-9', at(10), null);
|
|
130
|
+
const other = await store.open(draft({ orgId: 'o2' }));
|
|
131
|
+
expect(other.created).toBe(true);
|
|
132
|
+
expect(other.row).toMatchObject({ status: 'pending', orgId: 'o2' });
|
|
133
|
+
const again = await store.open(draft({ orgId: 'o1' }));
|
|
134
|
+
expect(again).toMatchObject({ created: false, row: { id: row.id, status: 'approved' } });
|
|
135
|
+
// Consuming one org's row frees only that org's key.
|
|
136
|
+
expect(await store.consume(row.id, at(20))).toBe(true);
|
|
137
|
+
expect((await store.open(draft({ orgId: 'o2' }))).row.id).toBe(other.row.id);
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
];
|
|
141
|
+
|
|
142
|
+
const DECIDE: readonly McpConfirmationStoreCheck[] = [
|
|
143
|
+
{
|
|
144
|
+
name: 'decide moves pending to a decision once, and never a second time',
|
|
145
|
+
async run(store) {
|
|
146
|
+
const { row } = await store.open(draft());
|
|
147
|
+
const approved = await store.decide(row.id, 'approved', 'user-9', at(10), null);
|
|
148
|
+
expect(approved).toMatchObject({ status: 'approved', decidedBy: 'user-9' });
|
|
149
|
+
expect(approved?.decidedAt?.getTime()).toBe(at(10).getTime());
|
|
150
|
+
expect(await store.decide(row.id, 'rejected', 'user-9', at(11), null)).toBeUndefined();
|
|
151
|
+
expect((await store.get(row.id))?.status).toBe('approved');
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
{
|
|
155
|
+
name: 'decide refuses a row past its expiry, and an unknown id',
|
|
156
|
+
async run(store) {
|
|
157
|
+
const { row } = await store.open(draft());
|
|
158
|
+
expect(await store.decide(row.id, 'approved', 'user-9', at(600), null)).toBeUndefined();
|
|
159
|
+
expect(await store.decide(uuidV7(), 'approved', 'user-9', at(1), null)).toBeUndefined();
|
|
160
|
+
expect(await store.get(uuidV7())).toBeUndefined();
|
|
161
|
+
},
|
|
162
|
+
},
|
|
163
|
+
{
|
|
164
|
+
name: 'consume takes a row exactly once, and a consumed row frees the call for a new one',
|
|
165
|
+
async run(store) {
|
|
166
|
+
const { row } = await store.open(draft());
|
|
167
|
+
expect(await store.consume(row.id, at(20))).toBe(true);
|
|
168
|
+
expect(await store.consume(row.id, at(21))).toBe(false);
|
|
169
|
+
expect((await store.get(row.id))?.consumedAt?.getTime()).toBe(at(20).getTime());
|
|
170
|
+
const next = await store.open(draft());
|
|
171
|
+
expect(next.created).toBe(true);
|
|
172
|
+
expect(next.row.id).not.toBe(row.id);
|
|
173
|
+
// A consumed row can no longer be decided: its call has already been answered.
|
|
174
|
+
expect(await store.decide(row.id, 'approved', 'user-9', at(22), null)).toBeUndefined();
|
|
175
|
+
},
|
|
176
|
+
},
|
|
177
|
+
{
|
|
178
|
+
name: 'a quorum row counts one vote per approver and flips to approved at the quorum',
|
|
179
|
+
async run(store) {
|
|
180
|
+
const approvers = ['role:finance', 'actor:user-7'];
|
|
181
|
+
const { row } = await store.open(draft({ quorum: 2, approvers }));
|
|
182
|
+
expect(row).toMatchObject({ quorum: 2, approvers, approvals: [] });
|
|
183
|
+
const first = await store.decide(row.id, 'approved', 'user-7', at(10), 'sig-7');
|
|
184
|
+
expect(first).toMatchObject({ status: 'pending', decidedBy: null, decidedAt: null });
|
|
185
|
+
expect(first?.approvals).toEqual([{ approver: 'user-7', at: at(10), signature: 'sig-7' }]);
|
|
186
|
+
// The same approver again is no vote at all — the row is unchanged.
|
|
187
|
+
expect(await store.decide(row.id, 'approved', 'user-7', at(11), 'sig-7')).toBeUndefined();
|
|
188
|
+
expect((await store.get(row.id))?.approvals).toHaveLength(1);
|
|
189
|
+
const second = await store.decide(row.id, 'approved', 'user-8', at(12), null);
|
|
190
|
+
expect(second).toMatchObject({ status: 'approved', decidedBy: 'user-8' });
|
|
191
|
+
expect(second?.decidedAt?.getTime()).toBe(at(12).getTime());
|
|
192
|
+
expect(second?.approvals.map((vote) => [vote.approver, vote.signature])).toEqual([
|
|
193
|
+
['user-7', 'sig-7'],
|
|
194
|
+
['user-8', null],
|
|
195
|
+
]);
|
|
196
|
+
expect(await store.decide(row.id, 'approved', 'user-9', at(13), null)).toBeUndefined();
|
|
197
|
+
},
|
|
198
|
+
},
|
|
199
|
+
{
|
|
200
|
+
name: 'one rejection settles a partly approved row, and a draft without a bar needs one',
|
|
201
|
+
async run(store) {
|
|
202
|
+
const { row } = await store.open(draft({ quorum: 3, approvers: ['role:finance'] }));
|
|
203
|
+
await store.decide(row.id, 'approved', 'user-7', at(10), null);
|
|
204
|
+
const rejected = await store.decide(row.id, 'rejected', 'user-8', at(11), null);
|
|
205
|
+
expect(rejected).toMatchObject({ status: 'rejected', decidedBy: 'user-8' });
|
|
206
|
+
expect(await store.decide(row.id, 'approved', 'user-9', at(12), null)).toBeUndefined();
|
|
207
|
+
const plain = await store.open(draft({ tool: 'plain' }));
|
|
208
|
+
expect(plain.row).toMatchObject({ quorum: 1, approvers: null, approvals: [] });
|
|
209
|
+
},
|
|
210
|
+
},
|
|
211
|
+
{
|
|
212
|
+
name: 'approvers racing lose no vote, and the quorum flips the row exactly once',
|
|
213
|
+
async run(store) {
|
|
214
|
+
const { row } = await store.open(draft({ quorum: 2, approvers: ['role:finance'] }));
|
|
215
|
+
const votes = await Promise.all(
|
|
216
|
+
['user-1', 'user-2', 'user-3'].map((by) =>
|
|
217
|
+
store.decide(row.id, 'approved', by, at(10), null),
|
|
218
|
+
),
|
|
219
|
+
);
|
|
220
|
+
// Two land (the second flips it), the third finds it approved; whichever order they ran in.
|
|
221
|
+
expect(votes.filter((vote) => vote !== undefined)).toHaveLength(2);
|
|
222
|
+
const held = await store.get(row.id);
|
|
223
|
+
expect(held?.status).toBe('approved');
|
|
224
|
+
expect(held?.approvals).toHaveLength(2);
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
name: 'a vote past the expiry is refused even with the quorum half met',
|
|
229
|
+
async run(store) {
|
|
230
|
+
const { row } = await store.open(draft({ quorum: 2, approvers: ['role:finance'] }));
|
|
231
|
+
await store.decide(row.id, 'approved', 'user-7', at(10), null);
|
|
232
|
+
expect(await store.decide(row.id, 'approved', 'user-8', at(600), null)).toBeUndefined();
|
|
233
|
+
expect((await store.get(row.id))?.status).toBe('pending');
|
|
234
|
+
},
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
name: 'purge deletes what expired before the instant, and counts it',
|
|
238
|
+
async run(store) {
|
|
239
|
+
const gone = await store.open(draft({ expiresAt: at(60) }));
|
|
240
|
+
const live = await store.open(draft({ tool: 'kept', expiresAt: at(6000) }));
|
|
241
|
+
expect(await store.purge(at(120))).toBe(1);
|
|
242
|
+
expect(await store.get(gone.row.id)).toBeUndefined();
|
|
243
|
+
expect(await store.get(live.row.id)).toBeDefined();
|
|
244
|
+
// The purged row's call is free again.
|
|
245
|
+
expect((await store.open(draft({ expiresAt: at(60) }))).created).toBe(true);
|
|
246
|
+
},
|
|
247
|
+
},
|
|
248
|
+
];
|
|
249
|
+
|
|
250
|
+
export const MCP_CONFIRMATION_STORE_CHECKS: readonly McpConfirmationStoreCheck[] = [
|
|
251
|
+
...OPEN,
|
|
252
|
+
...DECIDE,
|
|
253
|
+
];
|
|
254
|
+
|
|
255
|
+
export const mcpConfirmationStoreConformance: SharedExamples<Maybe<McpConfirmationStoreLike>> =
|
|
256
|
+
sharedExamples('an MCP confirmation store', (subject) => {
|
|
257
|
+
for (const check of MCP_CONFIRMATION_STORE_CHECKS) {
|
|
258
|
+
test(check.name, async () => {
|
|
259
|
+
await check.run(await subject());
|
|
260
|
+
});
|
|
261
|
+
}
|
|
262
|
+
});
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// The `MeterStore` contract of `@ultimat3/action` as shared examples, for a store of your own:
|
|
2
|
+
// `behavesLike(meterStoreConformance, () => store)`. The race is the point: a draw is one atomic
|
|
3
|
+
// check-and-add, and a store that reads then writes admits past the limit under parallel calls.
|
|
4
|
+
|
|
5
|
+
import { expect, test } from 'bun:test';
|
|
6
|
+
import type { MeterKey, MeterStore } from '@ultimat3/action/adapter';
|
|
7
|
+
import type { SharedExamples } from './shared-examples';
|
|
8
|
+
import { sharedExamples } from './shared-examples';
|
|
9
|
+
|
|
10
|
+
type Maybe<T> = T | Promise<T>;
|
|
11
|
+
|
|
12
|
+
export interface MeterStoreCheck {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
run(store: MeterStore): Promise<void>;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
const DAY = 86_400_000;
|
|
18
|
+
const START = Date.parse('2026-11-06T00:00:00.000Z');
|
|
19
|
+
const RACERS = 50;
|
|
20
|
+
|
|
21
|
+
let minted = 0;
|
|
22
|
+
/** A subject no other check, run or process shares — a shared database outlives one test file. */
|
|
23
|
+
const keyOf = (): MeterKey => {
|
|
24
|
+
minted += 1;
|
|
25
|
+
return {
|
|
26
|
+
subject: `org:conformance-${process.pid.toString(36)}-${minted.toString(36)}`,
|
|
27
|
+
meter: 'calls',
|
|
28
|
+
period: 'day@UTC',
|
|
29
|
+
};
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
const drawOf =
|
|
33
|
+
(store: MeterStore, key: MeterKey) =>
|
|
34
|
+
(limit: number, periodStartMs = START, over: Partial<MeterKey> = {}) =>
|
|
35
|
+
store.draw({ ...key, ...over, periodStartMs, limit });
|
|
36
|
+
|
|
37
|
+
export const METER_STORE_CHECKS: readonly MeterStoreCheck[] = [
|
|
38
|
+
{
|
|
39
|
+
name: 'declares its scope',
|
|
40
|
+
async run(store) {
|
|
41
|
+
expect(['process', 'shared']).toContain(store.scope);
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
name: 'counts up to the limit and refuses the next draw without counting it',
|
|
46
|
+
async run(store) {
|
|
47
|
+
const draw = drawOf(store, keyOf());
|
|
48
|
+
const seen = [];
|
|
49
|
+
for (let i = 0; i < 4; i += 1) seen.push(await draw(3));
|
|
50
|
+
expect(seen.map((result) => [result.allowed, result.used])).toEqual([
|
|
51
|
+
[true, 1],
|
|
52
|
+
[true, 2],
|
|
53
|
+
[true, 3],
|
|
54
|
+
[false, 3],
|
|
55
|
+
]);
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
name: `${RACERS} parallel draws at the edge admit exactly the limit`,
|
|
60
|
+
async run(store) {
|
|
61
|
+
const draw = drawOf(store, keyOf());
|
|
62
|
+
const results = await Promise.all(Array.from({ length: RACERS }, () => draw(7)));
|
|
63
|
+
expect(results.filter((result) => result.allowed)).toHaveLength(7);
|
|
64
|
+
expect(results.filter((result) => !result.allowed).every((r) => r.used === 7)).toBe(true);
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'a limit of 0 refuses every draw',
|
|
69
|
+
async run(store) {
|
|
70
|
+
expect((await drawOf(store, keyOf())(0)).allowed).toBe(false);
|
|
71
|
+
},
|
|
72
|
+
},
|
|
73
|
+
{
|
|
74
|
+
name: 'a later period starts from zero; an earlier one adds to the current',
|
|
75
|
+
async run(store) {
|
|
76
|
+
const draw = drawOf(store, keyOf());
|
|
77
|
+
await draw(2, START + DAY);
|
|
78
|
+
await draw(2, START + DAY);
|
|
79
|
+
expect((await draw(2, START + DAY)).allowed).toBe(false);
|
|
80
|
+
// A replica a period behind cannot reset today's count by drawing against yesterday.
|
|
81
|
+
expect((await draw(2, START)).allowed).toBe(false);
|
|
82
|
+
expect(await draw(2, START + 2 * DAY)).toEqual({ allowed: true, used: 1, warned: false });
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
name: 'a raised limit applies at once, to the count already spent',
|
|
87
|
+
async run(store) {
|
|
88
|
+
const draw = drawOf(store, keyOf());
|
|
89
|
+
await draw(1);
|
|
90
|
+
expect((await draw(1)).allowed).toBe(false);
|
|
91
|
+
expect(await draw(5)).toEqual({ allowed: true, used: 2, warned: false });
|
|
92
|
+
},
|
|
93
|
+
},
|
|
94
|
+
{
|
|
95
|
+
name: 'subjects, meters and period declarations are separate counters',
|
|
96
|
+
async run(store) {
|
|
97
|
+
const key = keyOf();
|
|
98
|
+
const draw = drawOf(store, key);
|
|
99
|
+
await draw(1);
|
|
100
|
+
expect((await draw(1, START, { subject: `${key.subject}-2` })).allowed).toBe(true);
|
|
101
|
+
expect((await draw(1, START, { meter: 'exports' })).allowed).toBe(true);
|
|
102
|
+
expect((await draw(1, START, { period: 'month@UTC' })).allowed).toBe(true);
|
|
103
|
+
expect((await draw(1)).allowed).toBe(false);
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
{
|
|
107
|
+
name: "a period's warning has one claimant, is released for a retry, and resets with the period",
|
|
108
|
+
async run(store) {
|
|
109
|
+
const key = keyOf();
|
|
110
|
+
const draw = drawOf(store, key);
|
|
111
|
+
await draw(9);
|
|
112
|
+
const claims = await Promise.all(
|
|
113
|
+
Array.from({ length: 6 }, () => store.claimWarning(key, START)),
|
|
114
|
+
);
|
|
115
|
+
expect(claims.filter(Boolean)).toHaveLength(1);
|
|
116
|
+
expect((await draw(9)).warned).toBe(true);
|
|
117
|
+
await store.releaseWarning(key, START);
|
|
118
|
+
expect(await store.claimWarning(key, START)).toBe(true);
|
|
119
|
+
// A claim for a period the counter is not in is no claim at all.
|
|
120
|
+
expect(await store.claimWarning(key, START + DAY)).toBe(false);
|
|
121
|
+
expect((await draw(9, START + DAY)).warned).toBe(false);
|
|
122
|
+
},
|
|
123
|
+
},
|
|
124
|
+
];
|
|
125
|
+
|
|
126
|
+
export const meterStoreConformance: SharedExamples<Maybe<MeterStore>> = sharedExamples(
|
|
127
|
+
'a meter store',
|
|
128
|
+
(subject) => {
|
|
129
|
+
for (const check of METER_STORE_CHECKS) {
|
|
130
|
+
test(check.name, async () => {
|
|
131
|
+
await check.run(await subject());
|
|
132
|
+
});
|
|
133
|
+
}
|
|
134
|
+
},
|
|
135
|
+
);
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
// The `DigestStore` contract of `@ultimat3/notify` as checks: a rolling window one append opens and
|
|
2
|
+
// owns, a closed window sealed rather than lost, a drain that takes its own window and any older
|
|
3
|
+
// one a crashed flush left, and a replayed appender answered what it was answered the first time.
|
|
4
|
+
// Every check coalesces into a slot of its own, so one shared table serves them all.
|
|
5
|
+
|
|
6
|
+
import { expect } from 'bun:test';
|
|
7
|
+
import type { NotifyEvent } from '@ultimat3/notify';
|
|
8
|
+
import type { DigestSlot, DigestStore } from '@ultimat3/notify/adapter';
|
|
9
|
+
|
|
10
|
+
export interface DigestStoreCheck {
|
|
11
|
+
readonly name: string;
|
|
12
|
+
run(store: DigestStore): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
const WINDOW_MS = 100;
|
|
16
|
+
|
|
17
|
+
let minted = 0;
|
|
18
|
+
const slotOf = (): DigestSlot => {
|
|
19
|
+
minted += 1;
|
|
20
|
+
const recipient = `conformance-${process.pid.toString(36)}-${minted.toString(36)}`;
|
|
21
|
+
return { recipient, notifier: 'post.commented', channel: 'email', group: 'g' };
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const event = (key: string, at: number): NotifyEvent<unknown> => ({
|
|
25
|
+
notifier: 'post.commented',
|
|
26
|
+
key,
|
|
27
|
+
params: { key },
|
|
28
|
+
at: new Date(at),
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
const keys = (events: readonly NotifyEvent<unknown>[]): readonly string[] =>
|
|
32
|
+
events.map((one) => one.key);
|
|
33
|
+
|
|
34
|
+
/** One run per event key unless named: a replay is the SAME appender again. */
|
|
35
|
+
const appendTo = (store: DigestStore, slot: DigestSlot) => (key: string, now: number) =>
|
|
36
|
+
store.append({
|
|
37
|
+
slot,
|
|
38
|
+
event: event(key, now),
|
|
39
|
+
windowMs: WINDOW_MS,
|
|
40
|
+
now: new Date(now),
|
|
41
|
+
appender: `run-${key}`,
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
export const DIGEST_STORE_CHECKS: readonly DigestStoreCheck[] = [
|
|
45
|
+
{
|
|
46
|
+
name: 'the first append opens the window and a later one inside it joins',
|
|
47
|
+
async run(store) {
|
|
48
|
+
const slot = slotOf();
|
|
49
|
+
const append = appendTo(store, slot);
|
|
50
|
+
expect(await append('e1', 0)).toEqual({ opened: true, endsAt: 100 });
|
|
51
|
+
expect(await append('e2', 50)).toEqual({ opened: false, endsAt: 100 });
|
|
52
|
+
expect(keys(await store.drain(slot, 100))).toEqual(['e1', 'e2']);
|
|
53
|
+
expect(await store.drain(slot, 100)).toEqual([]);
|
|
54
|
+
},
|
|
55
|
+
},
|
|
56
|
+
{
|
|
57
|
+
name: 'an append after a window closed opens a second and keeps the first',
|
|
58
|
+
async run(store) {
|
|
59
|
+
const slot = slotOf();
|
|
60
|
+
const append = appendTo(store, slot);
|
|
61
|
+
await append('e1', 0);
|
|
62
|
+
expect(await append('e2', 150)).toEqual({ opened: true, endsAt: 250 });
|
|
63
|
+
expect(keys(await store.drain(slot, 100))).toEqual(['e1']);
|
|
64
|
+
expect(keys(await store.drain(slot, 250))).toEqual(['e2']);
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'the next window’s drain collects one a crashed flush left, oldest first',
|
|
69
|
+
async run(store) {
|
|
70
|
+
const slot = slotOf();
|
|
71
|
+
const append = appendTo(store, slot);
|
|
72
|
+
await append('e1', 0);
|
|
73
|
+
await append('e2', 150);
|
|
74
|
+
expect(keys(await store.drain(slot, 250))).toEqual(['e1', 'e2']);
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
name: 'a drain naming no window takes the oldest alone',
|
|
79
|
+
async run(store) {
|
|
80
|
+
const slot = slotOf();
|
|
81
|
+
const append = appendTo(store, slot);
|
|
82
|
+
await append('e1', 0);
|
|
83
|
+
await append('e2', 150);
|
|
84
|
+
expect(keys(await store.drain(slot))).toEqual(['e1']);
|
|
85
|
+
expect(keys(await store.drain(slot))).toEqual(['e2']);
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'a window holds its events whole, `at` a Date, and slots never mix',
|
|
90
|
+
async run(store) {
|
|
91
|
+
const slot = slotOf();
|
|
92
|
+
const other: DigestSlot = { ...slot, group: 'thread-2' };
|
|
93
|
+
await appendTo(store, slot)('e1', 10);
|
|
94
|
+
expect(await appendTo(store, other)('x1', 20)).toEqual({ opened: true, endsAt: 120 });
|
|
95
|
+
expect(await store.drain(slot, 110)).toEqual([event('e1', 10)]);
|
|
96
|
+
expect(keys(await store.drain(other, 120))).toEqual(['x1']);
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
{
|
|
100
|
+
name: 'a replayed append answers what the first one did and lands its event once',
|
|
101
|
+
async run(store) {
|
|
102
|
+
const slot = slotOf();
|
|
103
|
+
const append = appendTo(store, slot);
|
|
104
|
+
expect(await append('e1', 0)).toEqual({ opened: true, endsAt: 100 });
|
|
105
|
+
expect(await append('e2', 10)).toEqual({ opened: false, endsAt: 100 });
|
|
106
|
+
// The step that appended re-runs whole: both appenders arrive a second time.
|
|
107
|
+
expect(await append('e1', 20)).toEqual({ opened: true, endsAt: 100 });
|
|
108
|
+
expect(await append('e2', 30)).toEqual({ opened: false, endsAt: 100 });
|
|
109
|
+
expect(keys(await store.drain(slot, 100))).toEqual(['e1', 'e2']);
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
name: 'a replay after its window closed answers that window, never a new one',
|
|
114
|
+
async run(store) {
|
|
115
|
+
const slot = slotOf();
|
|
116
|
+
const append = appendTo(store, slot);
|
|
117
|
+
await append('e1', 0);
|
|
118
|
+
expect(await append('e2', 150)).toEqual({ opened: true, endsAt: 250 });
|
|
119
|
+
expect(await append('e1', 160)).toEqual({ opened: true, endsAt: 100 });
|
|
120
|
+
expect(keys(await store.drain(slot, 250))).toEqual(['e1', 'e2']);
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
{
|
|
124
|
+
name: 'a drained window forgets its appenders: the same run in a later window opens it',
|
|
125
|
+
async run(store) {
|
|
126
|
+
const slot = slotOf();
|
|
127
|
+
const append = appendTo(store, slot);
|
|
128
|
+
await append('e1', 0);
|
|
129
|
+
await store.drain(slot, 100);
|
|
130
|
+
expect(await append('e1', 150)).toEqual({ opened: true, endsAt: 250 });
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
];
|