Files
dashtab/tests/unit/utils/nostr-events-test.js
raucao 6750c61d87 Add Nostr photo feeds and wallpaper selection
Read the curated photo feeds from wss://nostr.kosmos.org, verify events, cache originals and thumbnails, and let users pick a wallpaper from the Photos section. Nature is the default feed; City Nights is opt-in. Adds the manual publishing scripts, real event fixtures, protocol documentation, and tests with stubbed network connections.
2026-11-08 21:51:23 +04:00

68 lines
2.1 KiB
JavaScript

import { module, test } from 'qunit';
import {
canonicalEvent,
eventId,
hasValidEventId,
hasValidSignature,
tagValue,
verifyEvent,
} from 'dashtab/utils/nostr-events';
import photoEvents from '../../fixtures/nostr/photo-events.json';
import feedEvents from '../../fixtures/nostr/feed-events.json';
module('Unit | Utility | nostr-events', function () {
test('computes the canonical NIP-01 id for real events', function (assert) {
for (const event of [...photoEvents, ...feedEvents]) {
assert.strictEqual(eventId(event), event.id, event.id);
assert.true(hasValidEventId(event), event.id);
}
});
test('verifies real event signatures', function (assert) {
for (const event of [...photoEvents, ...feedEvents]) {
assert.true(hasValidSignature(event), event.id);
assert.true(verifyEvent(event), event.id);
}
});
test('rejects a tampered event', function (assert) {
const event = structuredClone(photoEvents[0]);
event.content = `${event.content} tampered`;
assert.false(hasValidEventId(event));
// The signature covers the id, not the event body, so it still validates
// against the unchanged id. The combined check must fail.
assert.true(hasValidSignature(event));
assert.false(verifyEvent(event));
});
test('rejects a tampered signature', function (assert) {
const event = structuredClone(photoEvents[0]);
event.sig = `${event.sig.slice(0, -2)}00`;
assert.false(hasValidSignature(event));
assert.false(verifyEvent(event));
});
test('canonical serialization is the six-element NIP-01 array', function (assert) {
const event = photoEvents[0];
const parsed = JSON.parse(canonicalEvent(event));
assert.deepEqual(parsed, [
0,
event.pubkey,
event.created_at,
event.kind,
event.tags,
event.content,
]);
});
test('tagValue returns the first matching tag value', function (assert) {
const event = photoEvents[0];
assert.strictEqual(tagValue(event, 'title'), 'Rolling sand dunes');
assert.strictEqual(tagValue(event, 'missing'), null);
});
});