Add ARCHITECTURE.md (layers, flows, folder map, glossary), CONTRIBUTING.md (setup and step-by-step recipes) and a README in each main folder, with an index of docs/. Convert the 511 JavaScript headers from "// Rôle :" lines to JSDoc @file blocks, define a three-level header template in website/AGENTS.md and check the header in tests/file-headers.test.mjs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
101 lines
3.9 KiB
JavaScript
101 lines
3.9 KiB
JavaScript
/**
|
|
* @file 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);
|
|
});
|