Files
subcast/server/test/sync.test.js

92 lines
3.8 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { createHash } from 'node:crypto';
import { request, startServer } from './helpers.js';
const hexSha = (s) => createHash('sha256').update(s).digest('hex');
test('E2EE sync spaces', async (t) => {
await t.test('snapshot & episode CRUD, auth checks, quota enforcement, and cascade delete', async () => {
const srv = await startServer({
SYNC_MAX_SPACE_BYTES: String(256 * 1024),
SYNC_MAX_OBJECT_BYTES: String(128 * 1024),
});
t.after(() => srv.stop());
const spaceId = hexSha('space-1').slice(0, 32);
const token = hexSha('token-1');
const wrongToken = hexSha('wrong-token');
const auth = { Authorization: `Bearer ${token}` };
const wrongAuth = { Authorization: `Bearer ${wrongToken}` };
// Missing space returns 404
const miss = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/snapshot`, { headers: auth });
assert.equal(miss.status, 404);
// Unauthenticated returns 401
const unauth = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/snapshot`);
assert.equal(unauth.status, 401);
// Write encrypted snapshot
const snapCipher = Buffer.from([1, 2, 3, 4, 5, 6, 7, 8, 9, 10]);
const putSnap = await request(srv.port, 'PUT', `/api/v1/sync/${spaceId}/snapshot`, {
body: snapCipher,
headers: { ...auth, 'Content-Type': 'application/octet-stream' },
});
assert.equal(putSnap.status, 200);
assert.equal(putSnap.json.ok, true);
assert.equal(putSnap.json.bytes, snapCipher.length);
// Wrong token rejected
const badGet = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/snapshot`, { headers: wrongAuth });
assert.equal(badGet.status, 401);
// Exact binary round-trip
const getSnap = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/snapshot`, { headers: auth });
assert.equal(getSnap.status, 200);
assert.deepEqual(getSnap.raw, snapCipher);
// Write episode bundle and overwrite it
const epId = 'a1b2c3d4e5f607189012';
const ep1 = Buffer.alloc(100 * 1024, 0xaa);
const putEp1 = await request(srv.port, 'PUT', `/api/v1/sync/${spaceId}/episodes/${epId}`, {
body: ep1,
headers: { ...auth, 'Content-Type': 'application/octet-stream' },
});
assert.equal(putEp1.status, 200);
const getEp = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/episodes/${epId}`, { headers: auth });
assert.equal(getEp.status, 200);
assert.deepEqual(getEp.raw, ep1);
// Second episode exceeds 256 KB space quota
const ep2 = Buffer.alloc(120 * 1024, 0xbb);
const putEp2 = await request(srv.port, 'PUT', `/api/v1/sync/${spaceId}/episodes/ep_two`, {
body: ep2,
headers: { ...auth, 'Content-Type': 'application/octet-stream' },
});
assert.equal(putEp2.status, 200);
const ep3 = Buffer.alloc(60 * 1024, 0xcc);
const putEp3 = await request(srv.port, 'PUT', `/api/v1/sync/${spaceId}/episodes/ep_three`, {
body: ep3,
headers: { ...auth, 'Content-Type': 'application/octet-stream' },
});
assert.equal(putEp3.status, 413);
assert.equal(putEp3.json.error, 'quota_exceeded');
// Delete with wrong token fails, then right token deletes and cascades
const badDel = await request(srv.port, 'DELETE', `/api/v1/sync/${spaceId}`, { headers: wrongAuth });
assert.equal(badDel.status, 401);
const del = await request(srv.port, 'DELETE', `/api/v1/sync/${spaceId}`, { headers: auth });
assert.equal(del.status, 200);
assert.equal(del.json.deleted, true);
const afterDelSnap = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/snapshot`, { headers: auth });
assert.equal(afterDelSnap.status, 404);
const afterDelEp = await request(srv.port, 'GET', `/api/v1/sync/${spaceId}/episodes/${epId}`, { headers: auth });
assert.equal(afterDelEp.status, 404);
});
});