Avvistamenti
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
demo-kit/ è quello che simula il dispositivo per la demo nella pagina di inizio. Il dispositivo stesso non ne ha bisogno.
// The field log's web server without Connectivitizer: `node --test` (or `npm test`) in this folder.
import assert from 'node:assert/strict';
import { after, before, describe, test } from 'node:test';
import { createFieldLogServer } from './server.mjs';
const JPEG = Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0, 0x10, 0x4a, 0x46, 0x49, 0x46, 0, 1, 0xff, 0xd9]);
const WEBM = Buffer.concat([Buffer.from([0x1a, 0x45, 0xdf, 0xa3]), Buffer.alloc(32)]);
/** A server on a free port with `options`; `call(path, init)` fetches from it. */
async function start(options = {}) {
const server = createFieldLogServer({ sample: false, ...options });
await new Promise((resolve) => server.listen(0, '127.0.0.1', resolve));
const base = `http://127.0.0.1:${server.address().port}`;
return {
call: (path, init) => fetch(base + path, init),
stop: () => new Promise((resolve) => { server.closeAllConnections(); server.close(resolve); }),
};
}
const post = (body, type = 'application/json') => ({ method: 'POST', headers: { 'Content-Type': type }, body: typeof body === 'string' ? body : JSON.stringify(body) });
const put = (body, type) => ({ method: 'PUT', headers: { 'Content-Type': type }, body });
describe('the field log', () => {
let log;
before(async () => (log = await start()));
after(() => log.stop());
test('serves its screen and manifest', async () => {
const manifest = await (await log.call('/.cz/ui.json')).json();
assert.equal(manifest.entry, '/.cz/ui.html');
assert.equal(manifest.name, 'Sightings');
assert.equal(manifest.appThemes[0].id, 'meadow');
assert.equal(Object.keys(manifest.appThemes[0].palette).length, 14);
const page = await log.call('/.cz/ui.html');
assert.equal(page.status, 200);
assert.match(page.headers.get('content-type'), /^text\/html/);
assert.match(page.headers.get('content-security-policy'), /form-action 'none'/);
const html = await page.text();
assert.ok(Buffer.byteLength(html) < 2 * 1024 * 1024);
assert.doesNotMatch(html, /localStorage|sessionStorage|document\.cookie|eval\(|new Function|innerHTML/);
});
test('keeps an entry with a note, a location, a photo and a voice note', async () => {
const created = await log.call('/entries', post({ note: ' Post 14 leaning\r\n', location: { lat: 52.5162754321, lon: 13.3777041, accuracy: 12.4 } }));
assert.equal(created.status, 201);
const entry = await created.json();
assert.match(entry.id, /^[0-9a-f]{12}$/);
assert.equal(entry.note, 'Post 14 leaning');
assert.deepEqual(entry.location, { lat: 52.516275, lon: 13.377704, accuracy: 12 });
assert.equal((await log.call(`/entries/${entry.id}/photo`, put(JPEG, 'image/jpeg'))).status, 200);
assert.equal((await log.call(`/entries/${entry.id}/audio`, put(WEBM, 'audio/webm;codecs=opus'))).status, 200);
const [listed] = await (await log.call('/entries')).json();
assert.equal(listed.id, entry.id);
assert.deepEqual(listed.photo, { type: 'image/jpeg', size: JPEG.length });
assert.deepEqual(listed.audio, { type: 'audio/webm', size: WEBM.length });
const photo = await log.call(`/entries/${entry.id}/photo`);
assert.equal(photo.headers.get('content-type'), 'image/jpeg');
assert.equal(photo.headers.get('x-content-type-options'), 'nosniff');
assert.deepEqual(Buffer.from(await photo.arrayBuffer()), JPEG);
const audio = await log.call(`/entries/${entry.id}/audio`);
assert.equal(audio.headers.get('content-type'), 'audio/webm');
assert.equal((await log.call(`/entries/${entry.id}`, { method: 'DELETE' })).status, 204);
assert.equal((await log.call(`/entries/${entry.id}/photo`)).status, 404);
assert.equal((await log.call(`/entries/${entry.id}`, { method: 'DELETE' })).status, 404);
});
test('keeps the species and the count, and does without them', async () => {
const created = await log.call('/entries', post({ species: ' Goldfinch \n', count: 4, note: 'On the thistles' }));
assert.equal(created.status, 201);
const entry = await created.json();
assert.equal(entry.species, 'Goldfinch');
assert.equal(entry.count, 4);
assert.equal((await (await log.call(`/entries/${entry.id}`)).json()).species, 'Goldfinch');
// Without them, as the page sent entries before there were species and counts.
const plain = await (await log.call('/entries', post({ note: 'Just a note' }))).json();
assert.equal(plain.species, '');
assert.equal(plain.count, null);
assert.equal((await log.call('/entries', post({ species: 'x'.repeat(80), count: 999 }))).status, 201);
assert.equal((await log.call('/entries', post({ species: 'é'.repeat(80), count: 1 }))).status, 201);
const longName = await log.call('/entries', post({ species: 'x'.repeat(81) }));
assert.equal(longName.status, 400);
assert.match(await longName.text(), /longer than 80 characters/);
for (const count of [0, 1000, -3, 2.5, '4', true]) {
const refused = await log.call('/entries', post({ species: 'Robin', count }));
assert.equal(refused.status, 400, `count ${JSON.stringify(count)}`);
assert.match(await refused.text(), /whole number from 1 to 999/);
}
assert.equal((await log.call('/entries', post({ species: 7 }))).status, 400);
});
test('changes the words of an entry, and only what the change names', async () => {
const entry = await (await log.call('/entries', post({ species: 'Robin', count: 1, note: 'On the fence', location: { lat: 52.5, lon: 13.4, accuracy: 5 } }))).json();
const change = (body) => ({ method: 'PUT', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(body) });
const changed = await log.call(`/entries/${entry.id}`, change({ species: 'Song thrush', count: 2 }));
assert.equal(changed.status, 200);
const after = await changed.json();
assert.equal(after.species, 'Song thrush');
assert.equal(after.count, 2);
assert.equal(after.note, 'On the fence');
assert.deepEqual(after.location, { lat: 52.5, lon: 13.4, accuracy: 5 });
const cleared = await (await log.call(`/entries/${entry.id}`, change({ species: '', count: null, note: '' }))).json();
assert.equal(cleared.species, '');
assert.equal(cleared.count, null);
assert.equal(cleared.note, '');
assert.equal((await log.call(`/entries/${entry.id}`, change({ count: 1000 }))).status, 400);
assert.equal((await log.call(`/entries/${entry.id}`, change({ species: 'x'.repeat(81) }))).status, 400);
assert.equal((await log.call(`/entries/${entry.id}`, { method: 'PUT', headers: { 'Content-Type': 'text/plain' }, body: '{}' })).status, 415);
assert.equal((await log.call('/entries/000000000000', change({ count: 2 }))).status, 404);
assert.equal((await (await log.call(`/entries/${entry.id}`)).json()).count, null);
});
test('lists the newest first', async () => {
const first = await (await log.call('/entries', post({ note: 'first' }))).json();
const second = await (await log.call('/entries', post({ note: 'second' }))).json();
const ids = (await (await log.call('/entries')).json()).map((entry) => entry.id);
assert.ok(ids.indexOf(second.id) < ids.indexOf(first.id));
});
test('refuses what does not fit, with a sentence', async () => {
const tooLong = await log.call('/entries', post({ note: 'x'.repeat(1001) }));
assert.equal(tooLong.status, 413);
assert.match(await tooLong.text(), /longer than 1000 characters/);
assert.equal((await log.call('/entries', post({ note: 'é'.repeat(1000) }))).status, 201);
assert.equal((await log.call('/entries', post('note=hello', 'application/x-www-form-urlencoded'))).status, 415);
assert.equal((await log.call('/entries', post('{nope'))).status, 400);
assert.equal((await log.call('/entries', post({ note: 5 }))).status, 400);
assert.equal((await log.call('/entries', post({ location: { lat: 91, lon: 0 } }))).status, 400);
assert.equal((await log.call('/entries', post({ location: { lat: 0, lon: 'east' } }))).status, 400);
assert.equal((await log.call('/entries', { ...post({ note: 'x' }), headers: { 'Content-Type': 'application/json', 'Sec-Fetch-Site': 'cross-site' } })).status, 403);
const { id } = await (await log.call('/entries', post({ note: 'with media' }))).json();
const bigPhoto = await log.call(`/entries/${id}/photo`, put(Buffer.concat([JPEG, Buffer.alloc(1_500_000)]), 'image/jpeg'));
assert.equal(bigPhoto.status, 413);
assert.equal(await bigPhoto.text(), 'The photo is larger than 1.5 MB.');
const bigAudio = await log.call(`/entries/${id}/audio`, put(Buffer.concat([WEBM, Buffer.alloc(1_000_000)]), 'audio/webm'));
assert.equal(bigAudio.status, 413);
assert.equal(await bigAudio.text(), 'The voice note is larger than 1.0 MB.');
assert.equal((await log.call(`/entries/${id}/photo`, put(JPEG, 'image/svg+xml'))).status, 415);
assert.equal((await log.call(`/entries/${id}/photo`, put(Buffer.from('<svg/>'), 'image/png'))).status, 415);
assert.equal((await log.call(`/entries/${id}/audio`, put(JPEG, 'audio/webm'))).status, 415);
assert.equal((await log.call('/entries/000000000000/photo', put(JPEG, 'image/jpeg'))).status, 404);
assert.equal((await log.call('/entries/../../etc/passwd')).status, 404);
assert.equal((await log.call('/entries', { method: 'PATCH' })).status, 405);
const [entry] = await (await log.call('/entries')).json();
assert.equal(entry.photo, null);
assert.equal(entry.audio, null);
});
test('says what it takes', async () => {
const info = await (await log.call('/info')).json();
assert.deepEqual(info.limits, { maxEntries: 30, maxNoteChars: 1000, maxSpeciesChars: 80, maxCount: 999, maxPhotoBytes: 1_500_000, maxAudioBytes: 1_000_000, keepHours: 24 });
});
});
test('keeps at most maxEntries, the oldest go first', async () => {
const log = await start({ maxEntries: 3, sample: true });
try {
assert.equal((await (await log.call('/entries')).json()).length, 1);
for (const note of ['a', 'b', 'c', 'd']) await log.call('/entries', post({ note }));
const notes = (await (await log.call('/entries')).json()).map((entry) => entry.note);
assert.deepEqual(notes, ['d', 'c', 'b']);
} finally {
await log.stop();
}
});
test('forgets entries after keepHours, but not the example entry', async () => {
let clock = Date.parse('2026-11-19T08:00:00Z');
const log = await start({ keepHours: 1, sample: true, now: () => clock });
try {
await log.call('/entries', post({ note: 'morning' }));
assert.equal((await (await log.call('/entries')).json()).length, 2);
clock += 61 * 60_000;
const left = await (await log.call('/entries')).json();
assert.equal(left.length, 1);
assert.equal(left[0].sample, true);
} finally {
await log.stop();
}
});