Files
raucao bba9a2a444
CI / Lint (pull_request) Successful in 57s
CI / Test (pull_request) Failing after 1m9s
Open Nostr event links from the search field
The full-text search field now detects anything the /e/ handler can open
(raw 64-hex ids, note1/nevent1/naddr1 codes, nostr: URIs, kind:pubkey:d
coordinates and permalinks) and opens it instead of running a Photon or
bookmark search.

As-you-type, a pasted reference resolves through the event store while the
field shows a spinner and the popover offers a single row labelled by kind
("Place photo" / "Place review", or a generic "Nostr event"). A reference
that cannot be found shows the usual not-found toast, and an unlinked event
is flagged as such. Submitting a reference, and direct /search?q=<ref>
links, route straight to the /e/ handler.
2026-11-28 19:36:02 +04:00

662 lines
20 KiB
JavaScript

import { module, test } from 'qunit';
import {
excludeRequiredRelays,
mergeRequiredRelays,
normalizeRelayUrl,
nostrEventLabel,
nostrEventRouteParam,
parseNostrEventReference,
parsePlacePhotos,
placeIdentifierFromEvent,
relayUrlMatches,
uniqNormalizedRelays,
} from 'marco/utils/nostr';
module('Unit | Utility | nostr', function () {
test('normalizeRelayUrl normalizes protocol, case, and slashes', function (assert) {
assert.strictEqual(normalizeRelayUrl(null), '');
assert.strictEqual(normalizeRelayUrl(''), '');
assert.strictEqual(normalizeRelayUrl(' '), '');
assert.strictEqual(
normalizeRelayUrl('Relay.example.com'),
'wss://relay.example.com'
);
assert.strictEqual(
normalizeRelayUrl('ws://Relay.example.com/'),
'ws://relay.example.com'
);
assert.strictEqual(
normalizeRelayUrl('wss://relay.example.com///'),
'wss://relay.example.com'
);
});
test('parsePlacePhotos includes event t tags on photo objects', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 123,
tags: [
['i', 'osm:node:123'],
['t', 'food'],
['t', 'vibe'],
['imeta', 'url https://example.com/photo.jpg', 'dim 800x600'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.deepEqual(photos[0].tags, ['food', 'vibe']);
});
test('parsePlacePhotos sorts by created_at', function (assert) {
const events = [
{
id: 'event-2',
pubkey: 'pubkey-2',
created_at: 200,
tags: [
['i', 'osm:node:456'],
['imeta', 'url https://example.com/late.jpg', 'dim 600x900'],
],
},
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
['i', 'osm:node:123'],
['imeta', 'url https://example.com/early.jpg', 'dim 600x900'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 2);
assert.strictEqual(photos[0].url, 'https://example.com/early.jpg');
assert.strictEqual(photos[1].url, 'https://example.com/late.jpg');
});
test('parsePlacePhotos promotes first landscape photo to index 0', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
['imeta', 'url https://example.com/portrait.jpg', 'dim 600x900'],
],
},
{
id: 'event-2',
pubkey: 'pubkey-2',
created_at: 200,
tags: [
['imeta', 'url https://example.com/landscape.jpg', 'dim 1200x600'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 2);
assert.strictEqual(photos[0].url, 'https://example.com/landscape.jpg');
assert.strictEqual(photos[1].url, 'https://example.com/portrait.jpg');
});
test('parsePlacePhotos skips imeta entries without urls', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [['imeta', 'dim 800x600']],
},
];
const photos = parsePlacePhotos(events);
assert.deepEqual(photos, []);
});
test('parsePlacePhotos returns one photo per event imeta tag', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
['i', 'osm:node:123'],
['imeta', 'url https://example.com/photo-1.jpg', 'dim 800x600'],
],
},
{
id: 'event-2',
pubkey: 'pubkey-2',
created_at: 200,
tags: [
['i', 'osm:node:456'],
['imeta', 'url https://example.com/photo-2.jpg', 'dim 600x800'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 2);
assert.strictEqual(photos[0].placeIdentifier, 'osm:node:123');
assert.strictEqual(photos[1].placeIdentifier, 'osm:node:456');
});
test('parsePlacePhotos extracts alt text from imeta', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
[
'imeta',
'url https://example.com/photo.jpg',
'dim 800x600',
'alt A sunny terrace overlooking the bay',
],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].alt, 'A sunny terrace overlooking the bay');
});
test('parsePlacePhotos strips the legacy placeholder alt text', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
[
'imeta',
'url https://example.com/photo.jpg',
'dim 800x600',
'alt A photo of a place',
],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].alt, null);
});
test('parsePlacePhotos leaves alt null when not provided', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [['imeta', 'url https://example.com/photo.jpg', 'dim 800x600']],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].alt, null);
});
test('parsePlacePhotos extracts published_at when present', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 200,
tags: [
['i', 'osm:node:123'],
['published_at', '100'],
['imeta', 'url https://example.com/photo.jpg', 'dim 800x600'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].publishedAt, 100);
assert.strictEqual(photos[0].createdAt, 200);
});
test('parsePlacePhotos sets publishedAt to null when not present', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 200,
tags: [
['i', 'osm:node:123'],
['imeta', 'url https://example.com/photo.jpg', 'dim 800x600'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].publishedAt, null);
});
test('parsePlacePhotos ignores invalid published_at values', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 200,
tags: [
['i', 'osm:node:123'],
['published_at', 'not-a-number'],
['imeta', 'url https://example.com/photo.jpg', 'dim 800x600'],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos.length, 1);
assert.strictEqual(photos[0].publishedAt, null);
});
test('parsePlacePhotos extracts top-level x hashes', function (assert) {
const hash =
'a9c84e183789a74288b8e05d04cc61230e74f386925a953e6b29f957e8cc3a61';
const thumbHash =
'c5a528e20235e16cc1c18090b8f04179de76288ea4e410b0fcb8d1487e416a2d';
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
['i', 'osm:node:123'],
['imeta', 'url https://example.com/photo.jpg', `x ${hash}`],
['x', hash],
['x', thumbHash],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos[0].hash, hash, 'imeta hash is primary');
assert.deepEqual(photos[0].hashes, [hash, thumbHash]);
});
test('parsePlacePhotos falls back to an imeta x hash', function (assert) {
const hash =
'b9c84e183789a74288b8e05d04cc61230e74f386925a953e6b29f957e8cc3a61';
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [['imeta', 'url https://example.com/photo.jpg', `x ${hash}`]],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos[0].hash, hash);
assert.deepEqual(photos[0].hashes, [hash]);
});
test('parsePlacePhotos treats mismatched x hashes as untrusted', function (assert) {
const events = [
{
id: 'event-1',
pubkey: 'pubkey-1',
created_at: 100,
tags: [
['imeta', 'url https://example.com/photo.jpg', `x ${'a'.repeat(64)}`],
['x', 'b'.repeat(64)],
],
},
];
const photos = parsePlacePhotos(events);
assert.strictEqual(photos[0].hash, null);
assert.deepEqual(photos[0].hashes, []);
});
test('uniqNormalizedRelays returns normalized unique relays', function (assert) {
const relays = uniqNormalizedRelays([
'Relay.example.com',
'wss://relay.example.com/',
'wss://other.example.com',
]);
assert.deepEqual(relays, [
'wss://relay.example.com',
'wss://other.example.com',
]);
});
test('mergeRequiredRelays keeps required relays as-is and merges normalized custom relays', function (assert) {
const relays = mergeRequiredRelays(
['wss://required.example.com', 'required-2.example.com'],
['required-2.example.com/', 'wss://custom.example.com']
);
assert.deepEqual(relays, [
'wss://required.example.com',
'required-2.example.com',
'wss://required-2.example.com',
'wss://custom.example.com',
]);
});
test('excludeRequiredRelays removes required relays from normalized custom list', function (assert) {
const relays = excludeRequiredRelays(
[
'wss://required.example.com',
'custom.example.com',
'ws://custom2.example.com',
],
['wss://required.example.com']
);
assert.deepEqual(relays, [
'wss://custom.example.com',
'ws://custom2.example.com',
]);
});
test('excludeRequiredRelays trusts required list without normalizing it', function (assert) {
const relays = excludeRequiredRelays(
['Required.example.com', 'custom.example.com'],
['required.example.com']
);
assert.deepEqual(relays, [
'wss://required.example.com',
'wss://custom.example.com',
]);
});
test('relayUrlMatches returns true for normalized trusted relays', function (assert) {
const trusted = ['wss://nostr.kosmos.org', 'wss://relay.damus.io'];
assert.true(
relayUrlMatches('wss://nostr.kosmos.org', trusted),
'exact match'
);
assert.true(
relayUrlMatches('wss://nostr.kosmos.org/', trusted),
'trailing slash normalized'
);
assert.true(
relayUrlMatches('WSS://Nostr.Kosmos.org/', trusted),
'case-insensitive'
);
assert.true(
relayUrlMatches('nostr.kosmos.org', trusted),
'bare hostname gets wss:// prefix'
);
});
test('relayUrlMatches returns false for non-trusted relays', function (assert) {
const trusted = ['wss://nostr.kosmos.org'];
assert.false(
relayUrlMatches('wss://spam.example.com', trusted),
'untrusted relay'
);
assert.false(relayUrlMatches('', trusted), 'empty url');
assert.false(relayUrlMatches(null, trusted), 'null url');
assert.false(
relayUrlMatches('wss://nostr.kosmos.org', []),
'empty trust list'
);
assert.false(
relayUrlMatches('wss://nostr.kosmos.org', null),
'null trust list'
);
});
module('parseNostrEventReference', function () {
const EVENT_ID =
'1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb';
const NOTE =
'note1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89sazwyqc';
const NEVENT =
'nevent1qvzqqqqpdqpzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqyfhwumn8ghj7un9d3shjtn90psk6urvv5qzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtzltwfa';
const NEVENT_NOHINTS =
'nevent1qqsp77g933m6yf89hc3xerczfjkd44xhgxz46a0dnug5wwagawrwrjcfq0qqc';
const NADDR =
'naddr1qvzqqqrknqpzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqyfhwumn8ghj7un9d3shjtn90psk6urvv5qqcmmnd5axummyv5arzv3najzpek';
test('parses a raw hex event id (case-insensitive)', function (assert) {
assert.deepEqual(parseNostrEventReference(EVENT_ID), { id: EVENT_ID });
assert.deepEqual(parseNostrEventReference(EVENT_ID.toUpperCase()), {
id: EVENT_ID,
});
});
test('parses a note1 event id', function (assert) {
assert.deepEqual(parseNostrEventReference(NOTE), { id: EVENT_ID });
});
test('parses a nevent with relay hints, author and kind', function (assert) {
const pointer = parseNostrEventReference(NEVENT);
assert.strictEqual(pointer.id, EVENT_ID);
assert.deepEqual(pointer.relays, ['wss://relay.example']);
assert.strictEqual(pointer.author, EVENT_ID);
assert.strictEqual(pointer.kind, 360);
});
test('parses a nevent without hints', function (assert) {
const pointer = parseNostrEventReference(NEVENT_NOHINTS);
assert.strictEqual(pointer.id, EVENT_ID);
assert.notOk(pointer.relays?.length);
});
test('parses an naddr into an address pointer', function (assert) {
const pointer = parseNostrEventReference(NADDR);
assert.strictEqual(pointer.kind, 30360);
assert.strictEqual(pointer.pubkey, EVENT_ID);
assert.strictEqual(pointer.identifier, 'osm:node:123');
assert.deepEqual(pointer.relays, ['wss://relay.example']);
});
test('accepts a nostr: scheme prefix', function (assert) {
assert.deepEqual(parseNostrEventReference(`nostr:${NOTE}`), {
id: EVENT_ID,
});
});
test('extracts the reference from a permalink URL', function (assert) {
assert.deepEqual(
parseNostrEventReference(`https://njump.me/${NEVENT}`),
parseNostrEventReference(NEVENT)
);
assert.deepEqual(
parseNostrEventReference(`https://primal.net/e/${EVENT_ID}`),
{ id: EVENT_ID }
);
});
test('parses a raw address coordinate', function (assert) {
assert.deepEqual(
parseNostrEventReference(`30360:${EVENT_ID}:osm:node:123`),
{ kind: 30360, pubkey: EVENT_ID, identifier: 'osm:node:123' }
);
});
test('rejects unsupported input', function (assert) {
assert.strictEqual(parseNostrEventReference(''), null);
assert.strictEqual(parseNostrEventReference('not-a-pointer'), null);
assert.strictEqual(
parseNostrEventReference('naddr1qqqqqqqq'),
null,
'malformed naddr'
);
assert.strictEqual(parseNostrEventReference(null), null);
assert.strictEqual(
parseNostrEventReference('30360:nothex:osm:node:123'),
null
);
});
test('rejects profile identifiers (npub/nprofile)', function (assert) {
// Only event references are openable by the /e/ handler; profiles are
// intentionally out of scope and must fall through to a normal search.
assert.strictEqual(
parseNostrEventReference(
'npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees'
),
null
);
assert.strictEqual(
parseNostrEventReference(
'nprofile1qqsp77g933m6yf89hc3xerczfjkd44xhgxz46a0dnug5wwagawrwrjc267jp9'
),
null
);
});
});
module('nostrEventRouteParam', function () {
const EVENT_ID =
'1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb';
const NOTE =
'note1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89sazwyqc';
const NEVENT =
'nevent1qvzqqqqpdqpzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtqyfhwumn8ghj7un9d3shjtn90psk6urvv5qzq8meqkx80g3yuklzymy0qfx2ekk56aqc2ht4ak03z3em4r4cdcwtzltwfa';
test('returns a bare reference unchanged', function (assert) {
assert.strictEqual(nostrEventRouteParam(EVENT_ID), EVENT_ID);
assert.strictEqual(nostrEventRouteParam(NOTE), NOTE);
assert.strictEqual(nostrEventRouteParam(NEVENT), NEVENT);
});
test('strips a nostr: scheme prefix', function (assert) {
assert.strictEqual(nostrEventRouteParam(`nostr:${NOTE}`), NOTE);
assert.strictEqual(nostrEventRouteParam(`NOSTR:${NOTE}`), NOTE);
});
test('reduces a permalink to its last path segment', function (assert) {
assert.strictEqual(
nostrEventRouteParam(`https://njump.me/${NEVENT}`),
NEVENT
);
assert.strictEqual(
nostrEventRouteParam(`https://primal.net/e/${EVENT_ID}`),
EVENT_ID
);
});
test('passes a raw address coordinate through as one segment', function (assert) {
const coordinate = `30360:${EVENT_ID}:osm:node:123`;
assert.strictEqual(nostrEventRouteParam(coordinate), coordinate);
// Identifiers may themselves contain colons, slashes and spaces and must
// survive as a single dynamic segment.
const messy = `30360:${EVENT_ID}:some/id with:colons`;
assert.strictEqual(nostrEventRouteParam(messy), messy);
});
test('returns null for anything not openable by /e/', function (assert) {
assert.strictEqual(nostrEventRouteParam('Berlin'), null);
assert.strictEqual(nostrEventRouteParam(''), null);
assert.strictEqual(nostrEventRouteParam(null), null);
assert.strictEqual(
nostrEventRouteParam(
'npub1raustrrh5gjwt03zdj8syn9vmt2dwsv9t467m8c3gua636uxu89svgdees'
),
null
);
});
});
module('placeIdentifierFromEvent', function () {
test('returns the i tag place identifier', function (assert) {
assert.strictEqual(
placeIdentifierFromEvent({ tags: [['i', 'osm:node:123']] }),
'osm:node:123'
);
assert.strictEqual(
placeIdentifierFromEvent({
tags: [
['g', 'xn0'],
['i', 'osm:way:9'],
],
}),
'osm:way:9'
);
});
test('returns null without a valid OSM i tag', function (assert) {
assert.strictEqual(placeIdentifierFromEvent({ tags: [] }), null);
assert.strictEqual(
placeIdentifierFromEvent({ tags: [['i', 'geo:1,2']] }),
null
);
assert.strictEqual(
placeIdentifierFromEvent({ tags: [['i', 'osm:foo:1']] }),
null
);
assert.strictEqual(placeIdentifierFromEvent(null), null);
assert.strictEqual(placeIdentifierFromEvent({}), null);
});
});
module('nostrEventLabel', function () {
test('labels a linked place photo', function (assert) {
assert.deepEqual(
nostrEventLabel({ kind: 360, tags: [['i', 'osm:node:123']] }),
{
title: 'Place photo',
description: 'Opens the linked place and photo',
}
);
});
test('labels a linked place review', function (assert) {
assert.deepEqual(
nostrEventLabel({ kind: 30360, tags: [['i', 'osm:way:9']] }),
{
title: 'Place review',
description: 'Opens the linked place and review',
}
);
});
test('keeps the kind title but flags an unlinked event', function (assert) {
assert.deepEqual(nostrEventLabel({ kind: 360, tags: [] }), {
title: 'Place photo',
description: 'Not linked to a place',
});
});
test('falls back to a generic label for other kinds', function (assert) {
assert.deepEqual(
nostrEventLabel({ kind: 1, tags: [['i', 'osm:node:123']] }),
{ title: 'Nostr event', description: 'Opens the linked place' }
);
assert.deepEqual(nostrEventLabel(null), {
title: 'Nostr event',
description: 'Not linked to a place',
});
});
});
});