Files
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

156 lines
3.9 KiB
JavaScript

import { module, test } from 'qunit';
import { setupTest } from 'dashtab/tests/helpers';
import photoEvents from '../../fixtures/nostr/photo-events.json';
class FakeSocket {
constructor() {
this.listeners = new Map();
this.sent = [];
this.closed = false;
}
addEventListener(type, callback) {
const listeners = this.listeners.get(type) ?? [];
listeners.push(callback);
this.listeners.set(type, listeners);
}
send(data) {
this.sent.push(JSON.parse(data));
}
close() {
this.closed = true;
this.fire('close');
}
fire(type, event = {}) {
for (const callback of this.listeners.get(type) ?? []) {
callback(event);
}
}
}
module('Unit | Service | nostr', function (hooks) {
setupTest(hooks);
function setup(context) {
const service = context.owner.lookup('service:nostr');
const sockets = [];
service.url = 'wss://relay.example.org';
service.createSocket = () => {
const socket = new FakeSocket();
sockets.push(socket);
return socket;
};
return { service, sockets };
}
test('queries a relay and resolves after EOSE', async function (assert) {
const { service, sockets } = setup(this);
const event = photoEvents[0];
const promise = service.query([{ kinds: [20], ids: [event.id] }]);
const socket = sockets[0];
socket.fire('open');
const subscriptionId = socket.sent[0][1];
assert.deepEqual(socket.sent[0], [
'REQ',
subscriptionId,
{ kinds: [20], ids: [event.id] },
]);
socket.fire('message', {
data: JSON.stringify(['EVENT', subscriptionId, event]),
});
socket.fire('message', {
data: JSON.stringify(['EOSE', subscriptionId]),
});
assert.deepEqual(await promise, [event]);
assert.true(socket.closed);
});
test('drops invalid events', async function (assert) {
const { service, sockets } = setup(this);
const event = structuredClone(photoEvents[0]);
event.content = 'tampered';
const promise = service.query([{ kinds: [20] }]);
const socket = sockets[0];
socket.fire('open');
const subscriptionId = socket.sent[0][1];
socket.fire('message', {
data: JSON.stringify(['EVENT', subscriptionId, event]),
});
socket.fire('message', {
data: JSON.stringify(['EOSE', subscriptionId]),
});
assert.deepEqual(await promise, []);
});
test('times out when the relay does not answer', async function (assert) {
const { service } = setup(this);
await assert.rejects(
service.query([{ kinds: [20] }], { timeout: 5 }),
/timed out/
);
});
test('aborts an in-flight query', async function (assert) {
const { service } = setup(this);
const controller = new AbortController();
const promise = service.query([{ kinds: [20] }], {
signal: controller.signal,
timeout: 1000,
});
controller.abort();
await assert.rejects(promise, /aborted/);
});
test('queries the curated feed definitions', async function (assert) {
const { service } = setup(this);
const calls = [];
service.query = async (filters) => {
calls.push(filters);
return [];
};
await service.queryFeedEvents();
assert.deepEqual(calls[0], [
{
kinds: [30006],
authors: [
'1f79058c77a224e5be226c8f024cacdad4d741855d75ed9f11473ba8eb86e1cb',
],
'#t': ['dashtab-photo-feed'],
limit: 20,
},
]);
});
test('chunks photo id queries at 300 ids', async function (assert) {
const { service } = setup(this);
const calls = [];
service.query = async (filters) => {
calls.push(filters[0]);
return [];
};
const ids = Array.from({ length: 301 }, (_, index) =>
String(index).padStart(64, '0')
);
await service.queryPhotos(ids);
assert.strictEqual(calls.length, 2);
assert.strictEqual(calls[0].ids.length, 300);
assert.strictEqual(calls[1].ids.length, 1);
});
});