Files
puter/src/backend/services/events/deliveryAuthCache.test.ts
T
Daniel Salazar d8ebfc8023 feat: delivery re-check cache, revocation and anchor settle (PUT-1677) (#3681)
* feat: delivery re-check cache, revocation and anchor settle (PUT-1677)

* fix: authorize re-anchors, settle each row once, purge revoked backlog (PUT-1677)

- A path-form row whose anchor is deleted only climbs to an ancestor its
  holder may still watch under the mode it subscribed with; otherwise it ends
  with `anchor_deleted`. It used to land on any surviving ancestor (a guest's
  row on the owner's home), where the re-check denied every delivery but the
  row still held an anchor slot and a filter evaluation there.
- After a climb the new anchor is re-verified and the climb repeated if a
  recursive delete took that level too, instead of leaving the row on a dead uid.
- suspend() is one conditional write per row and reports which rows it was the
  one to suspend; concurrent settles of the same grant (an unshare revokes
  several strings) no longer each purge, forget and notify the same rows.
- One "subscriptions ended" notification per holder and app, carrying the count
  and subjects, instead of one per row.
- A revoke that removed nothing no longer announces; the sweeper purges (not
  defers) the backlog of a permission_revoked row; the reap purges pending
  entries with the row.
- The delivery auth cache indexes entries by subscription so forget() is not a
  scan of the whole cache.
2026-09-02 16:00:14 -07:00

109 lines
3.7 KiB
TypeScript

/*
* Copyright (C) 2024-present Puter Technologies Inc.
*
* This file is part of Puter.
*
* Puter is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { afterEach, describe, expect, it, vi } from 'vitest';
import { DeliveryAuthCache } from './deliveryAuthCache.js';
const key = (
subId: string,
generation: string,
nodeUid = 'node-1',
): { subId: string; generation: string; nodeUid: string } => ({
subId,
generation,
nodeUid,
});
afterEach(() => {
vi.useRealTimers();
});
describe('DeliveryAuthCache', () => {
it('answers what it was told, and nothing it was not', () => {
const cache = new DeliveryAuthCache();
cache.write(key('sub-1', 'g1'), true);
expect(cache.read(key('sub-1', 'g1'))).toBe(true);
expect(cache.read(key('sub-2', 'g1'))).toBeNull();
});
it('keeps a denial as firmly as an approval', () => {
const cache = new DeliveryAuthCache();
cache.write(key('sub-1', 'g1'), false);
expect(cache.read(key('sub-1', 'g1'))).toBe(false);
});
it('has no answer once the permission generation moves', () => {
const cache = new DeliveryAuthCache();
cache.write(key('sub-1', 'g1'), true);
expect(cache.read(key('sub-1', 'g2'))).toBeNull();
});
it('decides per node, because the check is about the node', () => {
const cache = new DeliveryAuthCache();
cache.write(key('sub-1', 'g1', 'allowed'), true);
cache.write(key('sub-1', 'g1', 'closed'), false);
expect(cache.read(key('sub-1', 'g1', 'allowed'))).toBe(true);
expect(cache.read(key('sub-1', 'g1', 'closed'))).toBe(false);
expect(cache.read(key('sub-1', 'g1', 'unseen'))).toBeNull();
});
it('stops trusting an answer nothing has refreshed', () => {
vi.useFakeTimers();
const cache = new DeliveryAuthCache(100, 1_000);
cache.write(key('sub-1', 'g1'), true);
vi.advanceTimersByTime(999);
expect(cache.read(key('sub-1', 'g1'))).toBe(true);
vi.advanceTimersByTime(2);
expect(cache.read(key('sub-1', 'g1'))).toBeNull();
});
it('drops the least recently used rather than growing', () => {
const cache = new DeliveryAuthCache(2);
cache.write(key('sub-1', 'g1'), true);
cache.write(key('sub-2', 'g1'), true);
// Touching the oldest is what makes it the youngest.
cache.read(key('sub-1', 'g1'));
cache.write(key('sub-3', 'g1'), true);
expect(cache.size).toBe(2);
expect(cache.read(key('sub-1', 'g1'))).toBe(true);
expect(cache.read(key('sub-2', 'g1'))).toBeNull();
expect(cache.read(key('sub-3', 'g1'))).toBe(true);
});
it('forgets every answer about one subscription', () => {
const cache = new DeliveryAuthCache();
cache.write(key('sub-1', 'g1', 'a'), true);
cache.write(key('sub-1', 'g2', 'b'), true);
cache.write(key('sub-2', 'g1', 'a'), true);
cache.forget('sub-1');
expect(cache.read(key('sub-1', 'g1', 'a'))).toBeNull();
expect(cache.read(key('sub-1', 'g2', 'b'))).toBeNull();
expect(cache.read(key('sub-2', 'g1', 'a'))).toBe(true);
});
});