Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
728 changes: 540 additions & 188 deletions server/underlay/__tests__/client.test.ts

Large diffs are not rendered by default.

21 changes: 20 additions & 1 deletion server/underlay/__tests__/hash.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';

import { canonicalize, hashBytes, hashRecord, hashSchema } from '../hash';
import { canonicalize, hashBytes, hashRecord, hashSchema, jcs } from '../hash';

describe('underlay/hash', () => {
it('canonicalizes the spec example to sorted-key JSON', () => {
Expand Down Expand Up @@ -60,6 +60,25 @@ describe('underlay/hash', () => {
expect(withExtra.hash).toBe(without.hash);
});

it('sorts integer-like keys as strings, as JCS does (not in JavaScript enumeration order)', () => {
const { canonical } = hashRecord({
id: 'x',
type: 'T',
data: { b: 1, '10': 'ten', '9': 'nine', a: { '2': true, '1': false } },
});
// "10" < "9" < "a" < "b" by UTF-16 code units. A sorted object passed to JSON.stringify would
// put "9" before "10".
expect(canonical).toBe(
'{"id":"x","type":"T","data":{"10":"ten","9":"nine","a":{"1":false,"2":true},"b":1}}',
);
});

it('matches JSON.stringify for strings and numbers', () => {
expect(jcs({ s: 'é\n"', n: [1, 1.5, -0, 1e21] })).toBe(
'{"n":[1,1.5,0,1e+21],"s":"é\\n\\""}',
);
});

it('hashes schemas by canonicalized content', () => {
expect(hashSchema({ type: 'object', properties: { title: { type: 'string' } } })).toBe(
'75a323804435b510db3f8cc7fda9a750763b0ee894a8e28a00ffafc4823ee03a',
Expand Down
59 changes: 57 additions & 2 deletions server/underlay/__tests__/incremental.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -715,8 +715,8 @@ describe('underlay/incremental — resumed push re-supplies records without hoar
const pubUpdatedAt = { p1: '2026-01-05T00:00:00.000Z', p2: '2026-01-06T00:00:00.000Z' };

/**
* A checkpointed pub has had its FILES uploaded, but records are only sent after negotiate — so a
* push that died mid-mapping leaves cache entries whose records the server has never seen. The
* A checkpointed pub has had its FILES uploaded, but records are only sent in the push session —
* so a push that died mid-mapping leaves cache entries whose records the server has never seen. The
* next push must be able to produce them on demand, without retaining the file bytes that
* re-mapping also yields.
*/
Expand Down Expand Up @@ -786,3 +786,58 @@ describe('underlay/incremental — resumed push re-supplies records without hoar
expect(calls.p1).toBe(2);
});
});

describe('underlay/incremental — cached hashes from an older hash function', () => {
const pubs = [makePub('p1', 1)];
const pubUpdatedAt = { p1: '2026-01-05T00:00:00.000Z' };

/** A real cache entry whose stored record hashes no longer match what the pub maps to. */
const staleEntries = async () => {
const first = await buildIncrementalPush({
community,
collections: [],
pubs,
pubUpdatedAt,
options: OPTIONS,
cacheEntries: [],
mapPub: makeMapPub({}),
});
return first.cacheUpserts.map((entry) => ({
...entry,
recordHashes: Object.fromEntries(
Object.entries(entry.recordHashes).map(([id, r]) => [
id,
{ ...r, hash: `old-${r.hash}` },
]),
),
}));
};

it('resolves a record by (type, id) and queues a corrected cache entry', async () => {
const entries = await staleEntries();
const resumed = await buildIncrementalPush({
community,
collections: [],
pubs,
pubUpdatedAt,
options: OPTIONS,
cacheEntries: entries,
mapPub: makeMapPub({}),
});
expect(resumed.stats.cacheHits).toBe(1);
expect(resumed.cacheUpserts).toEqual([]);

// The manifest carries the stale hash, so a lookup by hash alone finds nothing…
const entry = resumed.payload.manifest!.find((m) => m.type === 'Pub' && m.id === 'p1')!;
expect(entry.hash.startsWith('old-')).toBe(true);
expect(await resumed.payload.resolveRecordByHash?.(entry.hash)).toBeNull();

// …but (type, id) still produces the record.
const record = await resumed.payload.resolveRecord?.(entry);
expect(record).toMatchObject({ id: 'p1', type: 'Pub' });

// And the entry is corrected, so the next push's manifest matches the head.
expect(resumed.cacheUpserts).toHaveLength(1);
expect(resumed.cacheUpserts[0].recordHashes.p1.hash).toBe(hashRecord(record!).hash);
});
});
Loading
Loading