Back to Black Site
SOURCE / PINNED RELEASE

Made of little things.

Black Site

Release
666bc7c74ebd…
Author-recorded commit
05e95af79676…
License
LICENSE
Author’s source reference
nostr://npub12lm3rxsva8kfempyjkxupt09e02n3fezurkyadyqfejdc5e0r2kscqty6r/wss%3A%2F%2Fgit.napplet.soy%2F/n-d2225ddc391

Archive hash verified: 1544e072e245c4a8…. The source-to-build association is the author’s claim; it has not been independently rebuilt.

tests/map-event.test.mjs
// Custom map events (spec/map-event.md): template, parsing, resource URLs and byte checks.
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { readFile } from 'node:fs/promises';
import { dirname, join } from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';
import { MAP_EVENT_KIND, mapEventTemplate, mapResourceUrls, parseMapEvent, readMapBytes } from '../spec/lib/map-event.mjs';

const root = dirname(dirname(fileURLToPath(import.meta.url)));
const bytes = await readFile(join(root, 'spec/examples/maps/outpost.json'));
const map = JSON.parse(bytes.toString('utf8'));
const sha256 = createHash('sha256').update(bytes).digest('hex');
const sha256Hex = async (buf) => createHash('sha256').update(buf).digest('hex');
const pubkey = 'a'.repeat(64);
const upload = { sha256, size: bytes.byteLength, url: `https://blossom.example.com/${sha256}.json`, fallbackUrls: ['https://cdn.example.org/x'] };
const signed = (template) => ({ ...template, pubkey, id: 'b'.repeat(64), sig: 'c'.repeat(128) });

test('template carries the map address, hash and Blossom servers', () => {
  const ev = mapEventTemplate(map, upload, { hashtags: ['CQB'], image: 'https://blossom.example.com/thumb.png' });
  assert.equal(ev.kind, MAP_EVENT_KIND);
  const tag = (n) => ev.tags.filter((t) => t[0] === n).map((t) => t[1]);
  assert.deepEqual(tag('d'), ['outpost']);
  assert.deepEqual(tag('title'), ['OUTPOST']);
  assert.deepEqual(tag('format'), []);
  assert.deepEqual(tag('version'), ['1']);
  assert.deepEqual(tag('x'), [sha256]);
  assert.deepEqual(tag('m'), ['application/json']);
  assert.deepEqual(tag('size'), [String(bytes.byteLength)]);
  assert.deepEqual(tag('server'), ['https://blossom.example.com', 'https://cdn.example.org']);
  assert.deepEqual(tag('t'), ['cqb']);
  assert.throws(() => mapEventTemplate({ ...map, version: 9 }, upload), /map is invalid/);
});

test('a published event parses back to its map reference', () => {
  const parsed = parseMapEvent(signed(mapEventTemplate(map, upload)));
  assert.equal(parsed.ok, true);
  assert.equal(parsed.map.address, `${MAP_EVENT_KIND}:${pubkey}:outpost`);
  assert.deepEqual(mapResourceUrls(parsed.map, ['https://author.example.net/']), [
    `blossom:${sha256}`,
    `https://blossom.example.com/${sha256}.json`,
    `https://cdn.example.org/${sha256}.json`,
    `https://author.example.net/${sha256}.json`,
  ]);
});

test('bad events are rejected with reasons', () => {
  const ev = signed(mapEventTemplate(map, upload));
  const withTags = (edit) => ({ ...ev, tags: edit(ev.tags.map((t) => [...t])) });
  assert.deepEqual(parseMapEvent({ ...ev, kind: 30078 }).errors, [`kind must be ${MAP_EVENT_KIND}`]);
  assert.deepEqual(parseMapEvent(withTags((t) => t.filter((x) => x[0] !== 'x'))).errors, ['x tag must be the 64-char lowercase hex SHA-256 of the map JSON']);
  assert.deepEqual(parseMapEvent(withTags((t) => t.map((x) => (x[0] === 'version' ? ['version', '2'] : x)))).errors, ['map version 2 is newer than this game supports (1)']);
  assert.deepEqual(parseMapEvent(withTags((t) => t.map((x) => (x[0] === 'server' ? ['server', 'http://insecure.example.com'] : x)))).map.servers, []);
});

test('downloaded bytes are checked against the event and the format', async () => {
  const parsed = parseMapEvent(signed(mapEventTemplate(map, upload))).map;
  assert.deepEqual(await readMapBytes(bytes, parsed, sha256Hex), { ok: true, map });
  const tampered = Buffer.from(bytes.toString('utf8').replace('OUTPOST', 'OUTPOSX'));
  assert.deepEqual((await readMapBytes(tampered, parsed, sha256Hex)).errors, ['map JSON does not match the x tag hash']);
  const renamed = Buffer.from(JSON.stringify({ ...map, id: 'other' }));
  const p2 = { ...parsed, size: undefined };
  assert.deepEqual((await readMapBytes(renamed, p2)).errors, ['map id "other" does not match the d tag "outpost"']);
  assert.deepEqual((await readMapBytes(Buffer.from('{nope'), p2)).errors, ['map is not valid UTF-8 JSON']);
});