📄 src/web3/subscription/patch-codec.test.ts
D-OPEN SOVEREIGN
1import { describe, it, expect } from 'vitest';
2import { patchToEvmPayload } from './patch-codec';
3import type { Patch } from './types';
4
5describe('patchToEvmPayload', () => {
6  it('maps raw on-chain patch fields onto the EvmPayload codec shape', () => {
7    const patch: Patch = {
8      userId: 1,
9      fromIndex: 0,
10      toIndex: 2,
11      raw: {
12        compressedActions: [1n, 2n, 3n],
13        indexes: [10n, 20n],
14        stringIndexes: ['foo', 'bar'],
15      },
16    };
17
18    const payload = patchToEvmPayload(patch, 7);
19
20    expect(payload).toEqual({
21      version: 7,
22      patches: [1n, 2n, 3n],
23      textIndex: [10, 20],
24      textData: ['foo', 'bar'],
25    });
26  });
27
28  it('converts bigint indexes to numbers for textIndex', () => {
29    const patch: Patch = {
30      userId: 1,
31      fromIndex: 0,
32      toIndex: 0,
33      raw: { compressedActions: [], indexes: [42n], stringIndexes: [] },
34    };
35
36    const payload = patchToEvmPayload(patch, 1);
37
38    expect(payload.textIndex).toEqual([42]);
39    expect(typeof payload.textIndex[0]).toBe('number');
40  });
41
42  it('handles empty patch batches', () => {
43    const patch: Patch = {
44      userId: 1,
45      fromIndex: 0,
46      toIndex: 0,
47      raw: { compressedActions: [], indexes: [], stringIndexes: [] },
48    };
49
50    const payload = patchToEvmPayload(patch, 0);
51
52    expect(payload).toEqual({
53      version: 0,
54      patches: [],
55      textIndex: [],
56      textData: [],
57    });
58  });
59});
60