Fix watch history snapshot merging

This commit is contained in:
thePR0M3TH3AN committed 2025-10-20 22:36:19 -04:00
1 parent 397b38d5b8
commit 7c9db2eeaf
2 files changed
+156 -12

No files matched your search

+103 -1
View File
@@ -1282,7 +1282,7 @@ async function testWatchHistoryPartialRelayRetry() {
nostrClient.sessionActor = originalSession;
nostrClient.pubkey = originalPub;
poolHarness.setResolver(() => ({ ok: true }));
watchHistoryService.resetProgress(actor);
watchHistoryService.resetProgress();
restoreCrypto.restore();
}
}
@@ -1533,6 +1533,107 @@ async function testWatchHistoryServiceIntegration() {
}
}
async function testWatchHistorySnapshotMergesQueuedWithCachedItems() {
console.log("Running watch history snapshot merge test...");
const actor = "npub-snapshot-merge";
const originalPublishSnapshot = nostrClient.publishWatchHistorySnapshot;
const originalRecordView = nostrClient.recordVideoView;
const originalEnsure = nostrClient.ensureSessionActor;
const originalSession = nostrClient.sessionActor;
const originalPub = nostrClient.pubkey;
const originalFingerprint = nostrClient.getWatchHistoryFingerprint;
try {
nostrClient.pubkey = actor;
nostrClient.sessionActor = { pubkey: actor, privateKey: "merge-priv" };
nostrClient.ensureSessionActor = async () => actor;
nostrClient.recordVideoView = async (_pointer, options = {}) => ({
ok: true,
event: {
id: `view-${options.created_at || Date.now()}`,
pubkey: actor,
created_at: options.created_at || Math.floor(Date.now() / 1000),
},
});
const publishedPayloads = [];
nostrClient.publishWatchHistorySnapshot = async (items, options = {}) => {
const clonedItems = Array.isArray(items)
? items.map((entry) => ({ ...entry }))
: [];
publishedPayloads.push({
items: clonedItems,
options: { ...options },
});
return {
ok: true,
snapshotId: `snapshot-${publishedPayloads.length}`,
items: clonedItems,
publishResults: {},
};
};
nostrClient.getWatchHistoryFingerprint = async (_actor, items = []) =>
`fingerprint-${Array.isArray(items) ? items.length : 0}-${Date.now()}`;
watchHistoryService.resetProgress(actor);
const seedItems = [
{ type: "e", value: "seed-one", watchedAt: 1_700_000_000 },
{ type: "e", value: "seed-two", watchedAt: 1_700_000_100 },
];
await watchHistoryService.snapshot(seedItems, {
actor,
reason: "seed-history",
});
assert.equal(
publishedPayloads.length,
1,
"initial snapshot should publish seed items",
);
await watchHistoryService.publishView(
{ type: "e", value: "fresh-entry" },
1_700_000_200,
{ actor },
);
const mergeResult = await watchHistoryService.snapshot(null, {
actor,
reason: "merge-queued-items",
});
assert.ok(mergeResult.ok, "merge snapshot should succeed");
assert.equal(
publishedPayloads.length,
2,
"merge snapshot should trigger a second publish",
);
const mergedCall = publishedPayloads[1];
const mergedKeys = mergedCall.items.map(
(entry) => `${entry?.type}:${entry?.value}`,
);
mergedKeys.sort();
assert.deepEqual(
mergedKeys,
["e:fresh-entry", "e:seed-one", "e:seed-two"],
"merged snapshot should include seed and queued pointers",
);
} finally {
nostrClient.publishWatchHistorySnapshot = originalPublishSnapshot;
nostrClient.recordVideoView = originalRecordView;
nostrClient.ensureSessionActor = originalEnsure;
nostrClient.sessionActor = originalSession;
nostrClient.pubkey = originalPub;
nostrClient.getWatchHistoryFingerprint = originalFingerprint;
watchHistoryService.resetProgress();
}
}
async function testHistoryCardsUseDecryptedPlaybackMetadata() {
console.log("Running watch history card playback metadata test...");
@@ -2047,6 +2148,7 @@ await testPublishSnapshotFailureRetry();
await testWatchHistoryPartialRelayRetry();
await testResolveWatchHistoryBatchingWindow();
await testWatchHistoryServiceIntegration();
await testWatchHistorySnapshotMergesQueuedWithCachedItems();
await testHistoryCardsUseDecryptedPlaybackMetadata();
await testWatchHistoryStaleCacheRefresh();
await testWatchHistoryLocalFallbackWhenDisabled();