Add tests for canonical hashtag preference kind

This commit is contained in:
thePR0M3TH3AN committed 2025-11-13 14:59:03 -05:00
1 parent a1ec4c89a6
commit f0218e5bb5
1 file changed
+86
+86
View File
@@ -69,6 +69,7 @@ test(
async list(relays, filters) {
assert.deepEqual(relays, ["wss://relay.one"]);
assert.equal(filters.length, 1);
assert.deepEqual(filters[0].kinds, [30015, 30005]);
assert.equal(filters[0]["#d"][0], "bitvid:tag-preferences");
return [
{
@@ -244,6 +245,7 @@ test(
assert.equal(publishedEvents.length, 1);
const publishedEvent = publishedEvents[0].event;
assert.deepEqual(publishedEvents[0].urls, ["wss://relay.publish"]);
assert.equal(publishedEvent.kind, 30015);
const encryptedTag = publishedEvent.tags.find(
(tag) => Array.isArray(tag) && tag[0] === "encrypted",
);
@@ -318,6 +320,7 @@ test(
assert.ok(encryptCalls[1].plaintext.includes("windowtag"));
assert.equal(publishedEvents.length, 1);
assert.deepEqual(publishedEvents[0].urls, ["wss://relay.window"]);
assert.equal(publishedEvents[0].event.kind, 30015);
const encryptedTag = publishedEvents[0].event.tags.find(
(tag) => Array.isArray(tag) && tag[0] === "encrypted",
);
@@ -325,3 +328,86 @@ test(
},
);
test(
"load prefers canonical kind when timestamps match while accepting legacy payload",
{ concurrency: false },
async () => {
const pubkey = "f".repeat(64);
const decryptCalls = [];
setActiveSigner({
nip44Decrypt: async (target, ciphertext) => {
decryptCalls.push({ target, ciphertext });
if (ciphertext === "canonical-cipher") {
return JSON.stringify({
version: 2,
interests: ["NewKind"],
disinterests: [],
});
}
if (ciphertext === "legacy-cipher") {
return JSON.stringify({
version: 1,
interests: ["Legacy"],
disinterests: [],
});
}
throw new Error(`unexpected ciphertext: ${ciphertext}`);
},
});
nostrClient.pool = {
async list(relays, filters) {
assert.deepEqual(relays, ["wss://relay.tie"]);
assert.deepEqual(filters[0].kinds, [30015, 30005]);
return [
{
id: "evt-canonical",
kind: 30015,
created_at: 500,
pubkey,
content: "canonical-cipher",
tags: [["encrypted", "nip44_v2"]],
},
{
id: "evt-legacy",
kind: 30005,
created_at: 500,
pubkey,
content: "legacy-cipher",
tags: [["encrypted", "nip44_v2"]],
},
];
},
};
nostrClient.relays = ["wss://relay.tie"];
nostrClient.writeRelays = ["wss://relay.tie"];
await hashtagPreferences.load(pubkey);
assert.deepEqual(decryptCalls.map((call) => call.ciphertext), [
"canonical-cipher",
]);
assert.deepEqual(hashtagPreferences.getInterests(), ["newkind"]);
nostrClient.pool.list = async () => [
{
id: "evt-legacy-only",
kind: 30005,
created_at: 600,
pubkey,
content: "legacy-cipher",
tags: [["encrypted", "nip44_v2"]],
},
];
await hashtagPreferences.load(pubkey);
assert.deepEqual(decryptCalls.map((call) => call.ciphertext), [
"canonical-cipher",
"legacy-cipher",
]);
assert.deepEqual(hashtagPreferences.getInterests(), ["legacy"]);
},
);