diff --git a/desktop/src/features/channels/readState/readStateFormat.test.mjs b/desktop/src/features/channels/readState/readStateFormat.test.mjs new file mode 100644 index 0000000000..248692a91c --- /dev/null +++ b/desktop/src/features/channels/readState/readStateFormat.test.mjs @@ -0,0 +1,33 @@ +import assert from "node:assert/strict"; +import test from "node:test"; + +import { isMsgContextKey, msgContextKey } from "./readStateFormat.ts"; + +test("msgContextKey_prefixesId_returnsMsgKey", () => { + assert.equal(msgContextKey("abc123"), "msg:abc123"); +}); + +test("isMsgContextKey_wellFormedKey_returnsTrue", () => { + assert.equal(isMsgContextKey("msg:abc123"), true); +}); + +test("isMsgContextKey_threadKey_returnsFalse", () => { + assert.equal(isMsgContextKey(`thread:${"a".repeat(64)}`), false); +}); + +test("isMsgContextKey_channelKey_returnsFalse", () => { + assert.equal(isMsgContextKey("channel-1"), false); +}); + +test("isMsgContextKey_emptyId_returnsFalse", () => { + assert.equal(isMsgContextKey("msg:"), false); +}); + +test("isMsgContextKey_msgPrefixWrappingThreadKey_returnsFalse", () => { + // A thread key accidentally re-prefixed must not pass as a message key. + assert.equal(isMsgContextKey(`msg:thread:${"a".repeat(64)}`), false); +}); + +test("msgContextKey_output_roundTripsThroughValidator", () => { + assert.equal(isMsgContextKey(msgContextKey("event-id")), true); +}); diff --git a/desktop/src/features/channels/readState/readStateFormat.ts b/desktop/src/features/channels/readState/readStateFormat.ts index 6ce0d69c59..4a37d18330 100644 --- a/desktop/src/features/channels/readState/readStateFormat.ts +++ b/desktop/src/features/channels/readState/readStateFormat.ts @@ -8,7 +8,28 @@ export const READ_STATE_D_TAG_PREFIX = "read-state:"; export const READ_STATE_FETCH_LIMIT = 500; export const READ_STATE_HORIZON_SECONDS = 7 * 24 * 60 * 60; -const MAX_CONTEXTS = 10_000; +export const MAX_CONTEXTS = 10_000; + +// Context-key prefix for a per-MESSAGE read marker (LP4 v3). One grow-only +// marker per reply id; the badge predicate reads effective("msg:") live so +// reading an ancestor never covers a descendant (Issue 2 by construction). +// Distinct from THREAD_PREFIX so the parent resolver and eviction can tell the +// two key families apart. +export const MSG_PREFIX = "msg:"; +export const THREAD_PREFIX = "thread:"; + +export function msgContextKey(messageId: string): string { + return `${MSG_PREFIX}${messageId}`; +} + +// A well-formed per-message context key: the msg: prefix with a non-empty id +// that does NOT itself start with thread: (guards against a thread key being +// mistaken for, or double-prefixed into, a message key). +export function isMsgContextKey(value: string): value is `msg:${string}` { + if (!value.startsWith(MSG_PREFIX)) return false; + const id = value.slice(MSG_PREFIX.length); + return id.length > 0 && !id.startsWith(THREAD_PREFIX); +} export function localReadStateKey(pubkey: string): string { return `buzz.channel-read-state.v2:${pubkey}`;