sokko-g/tests/toolbox-share-quota.test.mjs
Shinuwa bec9ea5b2f
All checks were successful
Validate and deploy Sokko G / validate (push) Successful in 20m39s
Validate and deploy Sokko G / deploy (push) Successful in 12s
Send a toolbox through an encrypted link
Sharing a toolbox meant exporting a JSON file and passing it around by
hand. A toolbox can now be sent from the site while staying local first:
the browser compresses and encrypts the v2 export with AES-GCM, and the
key lives only in the link fragment, so the server keeps for at most
24 hours a copy it cannot read.

- Add the /api/shares service: upload, metadata, download and revoke,
  with files in SHARES_DIR, a purge every 10 minutes, and single-use
  links (the default) deleted only once fully downloaded.
- Cap the server load: 50 MiB per share, total storage, free disk
  space, daily volume, concurrent and stalled uploads, reads per link,
  plus a wide per-connection cap kept in memory without storing IPs.
- Limit each browser to 2 shares and 100 MiB per day and 2 active
  links, and show the existing link of a toolbox already shared.
- Add the send dialog (card and game drawer) and the /receive page,
  which previews or directly imports through the existing import path.
- Move shared HTTP helpers to services/http.js for both APIs.
- Document the format, delivery, server layout and privacy wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-11 16:26:05 +01:00

98 lines
3.9 KiB
JavaScript

// Rôle : vérifie les limites d'envoi de toolbox appliquées par le navigateur.
import assert from "node:assert/strict";
import { test } from "node:test";
import { createShareQuota, SENT_SHARES_STORAGE_KEY, SHARE_LOG_STORAGE_KEY } from "../website/src/features/toolboxes/sharing/shareQuota.js";
const DAY = 24 * 60 * 60 * 1000;
const MIB = 1024 * 1024;
function memoryStorage() {
const values = new Map();
return {
values,
getItem: (key) => (values.has(key) ? values.get(key) : null),
setItem: (key, value) => values.set(key, String(value)),
removeItem: (key) => values.delete(key)
};
}
function setup() {
const storage = memoryStorage();
const clock = { value: Date.UTC(2026, 10, 11, 12) };
const quota = createShareQuota({ storage, now: () => clock.value });
let counter = 0;
const send = (toolboxId, bytes = MIB) => {
counter += 1;
return quota.record({ id: `share${counter}`, toolboxId, toolboxName: toolboxId, expiresAt: clock.value + DAY, deleteToken: `token${counter}`, link: `https://sokkog.test/#/receive/share${counter}#key` }, bytes);
};
return { storage, clock, quota, send };
}
test("a toolbox that was already sent keeps its existing link", () => {
const { quota, send } = setup();
assert.equal(quota.activeShareFor("tbx1"), null);
const entry = send("tbx1");
assert.deepEqual(quota.activeShareFor("tbx1"), entry);
assert.equal(entry.link, "https://sokkog.test/#/receive/share1#key");
assert.equal(quota.activeShareFor("tbx2"), null);
});
test("at most two shares can be active at once", () => {
const { quota, send, clock } = setup();
send("tbx1");
assert.deepEqual(quota.check(), { ok: true });
clock.value += 1000;
send("tbx2");
assert.deepEqual(quota.check(), { ok: false, reason: "active" });
});
test("revoking frees an active slot but not the daily quota", () => {
const { quota, send, clock } = setup();
const started = clock.value;
const first = send("tbx1");
clock.value += 60 * 60 * 1000;
send("tbx2");
quota.forget(first.id);
assert.equal(quota.activeShares().length, 1);
assert.deepEqual(quota.check(), { ok: false, reason: "daily-count", retryAt: started + DAY });
clock.value = started + DAY + 1;
quota.forget("share2");
assert.deepEqual(quota.check(), { ok: true });
});
test("no more than 100 MiB can be sent within 24 hours", () => {
const { quota, send, clock } = setup();
const started = clock.value;
send("tbx1", 60 * MIB);
quota.forget("share1");
assert.deepEqual(quota.check(30 * MIB), { ok: true });
assert.deepEqual(quota.check(50 * MIB), { ok: false, reason: "daily-bytes", retryAt: started + DAY });
clock.value += 2 * 60 * 60 * 1000;
assert.equal(quota.check(0).ok, true);
});
test("expired shares and old log entries are dropped", () => {
const { quota, send, clock, storage } = setup();
send("tbx1");
send("tbx2");
clock.value += DAY;
assert.deepEqual(quota.activeShares(), []);
assert.deepEqual(quota.check(), { ok: true });
assert.equal(storage.values.has(SENT_SHARES_STORAGE_KEY), false);
assert.equal(storage.values.has(SHARE_LOG_STORAGE_KEY), false);
});
test("corrupted or unavailable storage never blocks sharing", () => {
const { storage, quota } = setup();
storage.setItem(SENT_SHARES_STORAGE_KEY, "{not json");
storage.setItem(SHARE_LOG_STORAGE_KEY, JSON.stringify([{ at: "x" }, null]));
assert.deepEqual(quota.activeShares(), []);
assert.deepEqual(quota.check(), { ok: true });
const throwing = { getItem() { throw new Error("blocked"); }, setItem() { throw new Error("blocked"); }, removeItem() { throw new Error("blocked"); } };
const blocked = createShareQuota({ storage: throwing });
assert.deepEqual(blocked.check(), { ok: true });
assert.doesNotThrow(() => blocked.record({ id: "a", toolboxId: "t", expiresAt: Date.now() + DAY, deleteToken: "x" }, 1));
assert.doesNotThrow(() => blocked.forget("a"));
assert.equal(createShareQuota({ storage: null }).check().ok, true);
});