@entifix/testing-unit 0.14.0 → 0.16.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.
@@ -1,142 +0,0 @@
1
- import { _ as _extends } from "@swc/helpers/_/_extends";
2
- import { Effect } from 'effect';
3
- import { describe, expect, it } from 'vitest';
4
- const run = (effect)=>Effect.runPromise(effect);
5
- /** A threshold every instance is older than, so no spec waits on a clock. */ const ANY_AGE = -60000;
6
- /** A threshold no instance just written is older than. */ const AN_HOUR = 3600000;
7
- const instance = (sagaId, over = {})=>_extends({
8
- sagaId,
9
- definition: 'contract-flow',
10
- state: 'RUNNING',
11
- stepIndex: 0,
12
- outcomes: [],
13
- inputs: {},
14
- resumeAttempts: 0,
15
- createdAt: new Date().toISOString()
16
- }, over);
17
- const outcome = (stepId)=>({
18
- stepId,
19
- calls: []
20
- });
21
- /**
22
- * What every {@link SagaStore} must guarantee to the saga engine: an instance
23
- * that can be started twice without being overwritten, outcomes that append in
24
- * arrival order, a compensation flag that lands on one step, and a resume claim
25
- * that only ever takes an unfinished instance past its deadline.
26
- *
27
- * Deliberately not asserted: two concurrent claims producing one winner. That
28
- * is a property of the conditional write against a real server, and a contract
29
- * run in one process cannot interleave the two.
30
- */ export const describeSagaStoreContract = (name, makeStore)=>{
31
- describe(`SagaStore contract: ${name}`, ()=>{
32
- it('starts an instance and reads it back, stamped', async ()=>{
33
- const store = await makeStore();
34
- await run(store.start(instance('s-1')));
35
- const read = await run(store.get('s-1'));
36
- expect(read).toMatchObject({
37
- sagaId: 's-1',
38
- state: 'RUNNING'
39
- });
40
- expect(read == null ? void 0 : read.updatedAt).toEqual(expect.any(String));
41
- });
42
- it('answers undefined for an instance it never started', async ()=>{
43
- const store = await makeStore();
44
- expect(await run(store.get('absent'))).toBeUndefined();
45
- });
46
- // A coordinator restarted mid-flight re-enters `start` with an id it
47
- // already wrote; what it had recorded must survive that.
48
- it('does not overwrite an instance started twice', async ()=>{
49
- const store = await makeStore();
50
- await run(store.start(instance('s-1')));
51
- await run(store.recordOutcome('s-1', outcome('reserve')));
52
- await run(store.start(instance('s-1', {
53
- stepIndex: 9
54
- })));
55
- const read = await run(store.get('s-1'));
56
- expect(read == null ? void 0 : read.stepIndex).toBe(0);
57
- expect(read == null ? void 0 : read.outcomes.map((entry)=>entry.stepId)).toEqual([
58
- 'reserve'
59
- ]);
60
- });
61
- it('moves the step index and appends outcomes in arrival order', async ()=>{
62
- const store = await makeStore();
63
- await run(store.start(instance('s-1')));
64
- await run(store.beginStep('s-1', 1));
65
- await run(store.recordOutcome('s-1', outcome('reserve')));
66
- await run(store.recordOutcome('s-1', outcome('charge')));
67
- const read = await run(store.get('s-1'));
68
- expect(read == null ? void 0 : read.stepIndex).toBe(1);
69
- expect(read == null ? void 0 : read.outcomes.map((entry)=>entry.stepId)).toEqual([
70
- 'reserve',
71
- 'charge'
72
- ]);
73
- });
74
- it('settles with a state and the error that caused it', async ()=>{
75
- const store = await makeStore();
76
- await run(store.start(instance('s-1')));
77
- await run(store.settle('s-1', 'COMPENSATED', 'charge refused'));
78
- expect(await run(store.get('s-1'))).toMatchObject({
79
- state: 'COMPENSATED',
80
- error: 'charge refused'
81
- });
82
- });
83
- it('flags only the named step compensated', async ()=>{
84
- const store = await makeStore();
85
- await run(store.start(instance('s-1')));
86
- await run(store.recordOutcome('s-1', outcome('reserve')));
87
- await run(store.recordOutcome('s-1', outcome('charge')));
88
- await run(store.markCompensated('s-1', 'reserve'));
89
- const read = await run(store.get('s-1'));
90
- expect(read == null ? void 0 : read.outcomes).toEqual([
91
- expect.objectContaining({
92
- stepId: 'reserve',
93
- compensated: true
94
- }),
95
- expect.not.objectContaining({
96
- compensated: true
97
- })
98
- ]);
99
- });
100
- // `COMPENSATING` is stuck too: a coordinator that died while unwinding is
101
- // the state that leaves holds in place.
102
- it('finds unfinished instances past the deadline, and nothing else', async ()=>{
103
- const store = await makeStore();
104
- await run(store.start(instance('running')));
105
- await run(store.start(instance('unwinding', {
106
- state: 'COMPENSATING'
107
- })));
108
- await run(store.start(instance('done', {
109
- state: 'COMPLETED'
110
- })));
111
- const stale = await run(store.findStale(ANY_AGE));
112
- expect(stale.map((entry)=>entry.sagaId).sort()).toEqual([
113
- 'running',
114
- 'unwinding'
115
- ]);
116
- expect(await run(store.findStale(AN_HOUR))).toEqual([]);
117
- });
118
- it('claims a stale instance by counting the attempt', async ()=>{
119
- var _this;
120
- const store = await makeStore();
121
- await run(store.start(instance('s-1')));
122
- const claimed = await run(store.claimForResume('s-1', ANY_AGE));
123
- expect(claimed).toMatchObject({
124
- sagaId: 's-1',
125
- resumeAttempts: 1
126
- });
127
- expect((_this = await run(store.get('s-1'))) == null ? void 0 : _this.resumeAttempts).toBe(1);
128
- });
129
- it('refuses to claim a settled or a fresh instance', async ()=>{
130
- const store = await makeStore();
131
- await run(store.start(instance('done', {
132
- state: 'COMPLETED'
133
- })));
134
- await run(store.start(instance('fresh')));
135
- expect(await run(store.claimForResume('done', ANY_AGE))).toBeUndefined();
136
- expect(await run(store.claimForResume('fresh', AN_HOUR))).toBeUndefined();
137
- expect(await run(store.claimForResume('absent', ANY_AGE))).toBeUndefined();
138
- });
139
- });
140
- };
141
-
142
- //# sourceMappingURL=saga-store.contract.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/contracts/saga-store.contract.ts"],"sourcesContent":["import type {\n SagaInstance,\n SagaStepOutcome,\n SagaStore,\n} from '@entifix/transactions';\nimport { Effect } from 'effect';\nimport { describe, expect, it } from 'vitest';\n\nconst run = <TValue, TError>(effect: Effect.Effect<TValue, TError>) =>\n Effect.runPromise(effect);\n\n/** A threshold every instance is older than, so no spec waits on a clock. */\nconst ANY_AGE = -60_000;\n/** A threshold no instance just written is older than. */\nconst AN_HOUR = 3_600_000;\n\nconst instance = (\n sagaId: string,\n over: Partial<Omit<SagaInstance, 'updatedAt'>> = {},\n): Omit<SagaInstance, 'updatedAt'> => ({\n sagaId,\n definition: 'contract-flow',\n state: 'RUNNING',\n stepIndex: 0,\n outcomes: [],\n inputs: {},\n resumeAttempts: 0,\n createdAt: new Date().toISOString(),\n ...over,\n});\n\nconst outcome = (stepId: string): SagaStepOutcome => ({ stepId, calls: [] });\n\n/**\n * What every {@link SagaStore} must guarantee to the saga engine: an instance\n * that can be started twice without being overwritten, outcomes that append in\n * arrival order, a compensation flag that lands on one step, and a resume claim\n * that only ever takes an unfinished instance past its deadline.\n *\n * Deliberately not asserted: two concurrent claims producing one winner. That\n * is a property of the conditional write against a real server, and a contract\n * run in one process cannot interleave the two.\n */\nexport const describeSagaStoreContract = (\n name: string,\n makeStore: () => SagaStore | Promise<SagaStore>,\n): void => {\n describe(`SagaStore contract: ${name}`, () => {\n it('starts an instance and reads it back, stamped', async () => {\n const store = await makeStore();\n\n await run(store.start(instance('s-1')));\n const read = await run(store.get('s-1'));\n\n expect(read).toMatchObject({ sagaId: 's-1', state: 'RUNNING' });\n expect(read?.updatedAt).toEqual(expect.any(String));\n });\n\n it('answers undefined for an instance it never started', async () => {\n const store = await makeStore();\n\n expect(await run(store.get('absent'))).toBeUndefined();\n });\n\n // A coordinator restarted mid-flight re-enters `start` with an id it\n // already wrote; what it had recorded must survive that.\n it('does not overwrite an instance started twice', async () => {\n const store = await makeStore();\n await run(store.start(instance('s-1')));\n await run(store.recordOutcome('s-1', outcome('reserve')));\n\n await run(store.start(instance('s-1', { stepIndex: 9 })));\n\n const read = await run(store.get('s-1'));\n expect(read?.stepIndex).toBe(0);\n expect(read?.outcomes.map(entry => entry.stepId)).toEqual(['reserve']);\n });\n\n it('moves the step index and appends outcomes in arrival order', async () => {\n const store = await makeStore();\n await run(store.start(instance('s-1')));\n\n await run(store.beginStep('s-1', 1));\n await run(store.recordOutcome('s-1', outcome('reserve')));\n await run(store.recordOutcome('s-1', outcome('charge')));\n\n const read = await run(store.get('s-1'));\n expect(read?.stepIndex).toBe(1);\n expect(read?.outcomes.map(entry => entry.stepId)).toEqual([\n 'reserve',\n 'charge',\n ]);\n });\n\n it('settles with a state and the error that caused it', async () => {\n const store = await makeStore();\n await run(store.start(instance('s-1')));\n\n await run(store.settle('s-1', 'COMPENSATED', 'charge refused'));\n\n expect(await run(store.get('s-1'))).toMatchObject({\n state: 'COMPENSATED',\n error: 'charge refused',\n });\n });\n\n it('flags only the named step compensated', async () => {\n const store = await makeStore();\n await run(store.start(instance('s-1')));\n await run(store.recordOutcome('s-1', outcome('reserve')));\n await run(store.recordOutcome('s-1', outcome('charge')));\n\n await run(store.markCompensated('s-1', 'reserve'));\n\n const read = await run(store.get('s-1'));\n expect(read?.outcomes).toEqual([\n expect.objectContaining({ stepId: 'reserve', compensated: true }),\n expect.not.objectContaining({ compensated: true }),\n ]);\n });\n\n // `COMPENSATING` is stuck too: a coordinator that died while unwinding is\n // the state that leaves holds in place.\n it('finds unfinished instances past the deadline, and nothing else', async () => {\n const store = await makeStore();\n await run(store.start(instance('running')));\n await run(store.start(instance('unwinding', { state: 'COMPENSATING' })));\n await run(store.start(instance('done', { state: 'COMPLETED' })));\n\n const stale = await run(store.findStale(ANY_AGE));\n expect(stale.map(entry => entry.sagaId).sort()).toEqual([\n 'running',\n 'unwinding',\n ]);\n expect(await run(store.findStale(AN_HOUR))).toEqual([]);\n });\n\n it('claims a stale instance by counting the attempt', async () => {\n const store = await makeStore();\n await run(store.start(instance('s-1')));\n\n const claimed = await run(store.claimForResume('s-1', ANY_AGE));\n\n expect(claimed).toMatchObject({ sagaId: 's-1', resumeAttempts: 1 });\n expect((await run(store.get('s-1')))?.resumeAttempts).toBe(1);\n });\n\n it('refuses to claim a settled or a fresh instance', async () => {\n const store = await makeStore();\n await run(store.start(instance('done', { state: 'COMPLETED' })));\n await run(store.start(instance('fresh')));\n\n expect(await run(store.claimForResume('done', ANY_AGE))).toBeUndefined();\n expect(await run(store.claimForResume('fresh', AN_HOUR))).toBeUndefined();\n expect(\n await run(store.claimForResume('absent', ANY_AGE)),\n ).toBeUndefined();\n });\n });\n};\n"],"names":["Effect","describe","expect","it","run","effect","runPromise","ANY_AGE","AN_HOUR","instance","sagaId","over","definition","state","stepIndex","outcomes","inputs","resumeAttempts","createdAt","Date","toISOString","outcome","stepId","calls","describeSagaStoreContract","name","makeStore","store","start","read","get","toMatchObject","updatedAt","toEqual","any","String","toBeUndefined","recordOutcome","toBe","map","entry","beginStep","settle","error","markCompensated","objectContaining","compensated","not","stale","findStale","sort","claimed","claimForResume"],"mappings":";AAKA,SAASA,MAAM,QAAQ,SAAS;AAChC,SAASC,QAAQ,EAAEC,MAAM,EAAEC,EAAE,QAAQ,SAAS;AAE9C,MAAMC,MAAM,CAAiBC,SAC3BL,OAAOM,UAAU,CAACD;AAEpB,2EAA2E,GAC3E,MAAME,UAAU,CAAC;AACjB,wDAAwD,GACxD,MAAMC,UAAU;AAEhB,MAAMC,WAAW,CACfC,QACAC,OAAiD,CAAC,CAAC,GACd;QACrCD;QACAE,YAAY;QACZC,OAAO;QACPC,WAAW;QACXC,UAAU,EAAE;QACZC,QAAQ,CAAC;QACTC,gBAAgB;QAChBC,WAAW,IAAIC,OAAOC,WAAW;OAC9BT;AAGL,MAAMU,UAAU,CAACC,SAAqC,CAAA;QAAEA;QAAQC,OAAO,EAAE;IAAC,CAAA;AAE1E;;;;;;;;;CASC,GACD,OAAO,MAAMC,4BAA4B,CACvCC,MACAC;IAEAzB,SAAS,CAAC,oBAAoB,EAAEwB,MAAM,EAAE;QACtCtB,GAAG,iDAAiD;YAClD,MAAMwB,QAAQ,MAAMD;YAEpB,MAAMtB,IAAIuB,MAAMC,KAAK,CAACnB,SAAS;YAC/B,MAAMoB,OAAO,MAAMzB,IAAIuB,MAAMG,GAAG,CAAC;YAEjC5B,OAAO2B,MAAME,aAAa,CAAC;gBAAErB,QAAQ;gBAAOG,OAAO;YAAU;YAC7DX,OAAO2B,wBAAAA,KAAMG,SAAS,EAAEC,OAAO,CAAC/B,OAAOgC,GAAG,CAACC;QAC7C;QAEAhC,GAAG,sDAAsD;YACvD,MAAMwB,QAAQ,MAAMD;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