app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

inbox-budget.test.ts (3627B)


      1 import { beforeAll, test, expect } from 'vitest';
      2 import * as Budget from '../../src/lib/messaging/inbox-budget.ts';
      3 import * as Cleanup from '../../src/lib/persistence/inbox-retention.ts';
      4 import type { PrivateSession } from '../../src/lib/runtime/private-session.ts';
      5 import type { PrivateStorageRepository } from '../../src/lib/persistence/private-storage.ts';
      6 const id = (n: number) => n.toString(16).padStart(64, '0');
      7 beforeAll(() => {
      8   expect(Budget.inboxBudgetSnapshot).toBeTypeOf('function');
      9   expect(Cleanup.selectReceivedCleanupIds).toBeTypeOf('function');
     10   expect(Cleanup.captureReceivedCleanup).toBeTypeOf('function');
     11 });
     12 test('finite live retention and independent unresolved outbox caps remain exact', () =>
     13   expect(Budget.inboxBudgetSnapshot()).toEqual({
     14     finite: { deliveries: 500, bytes: 8388608, milliseconds: 15000 },
     15     live: { deliveries: 120, bytes: 2097152, milliseconds: 60000 },
     16     received: { records: 2000, bytes: 50331648 },
     17     outbox: { records: 100, bytes: 8388608 }
     18   }));
     19 test('review selection deduplicates only exact outer IDs preserving order', () =>
     20   expect(Cleanup.selectReceivedCleanupIds([id(2), id(1), id(2)])).toEqual([
     21     id(2),
     22     id(1)
     23   ]));
     24 test('reviewed ID selection and budget snapshots are detached copies', () => {
     25   const input = [id(1)];
     26   const selected = Cleanup.selectReceivedCleanupIds(input)!;
     27   input[0] = id(2);
     28   expect(selected).toEqual([id(1)]);
     29   const view: { received: { records: number } } = Budget.inboxBudgetSnapshot();
     30   view.received.records = 1;
     31   expect(Budget.inboxBudgetSnapshot().received.records).toBe(2000);
     32 });
     33 test('malformed tail never silently truncates reviewed cleanup selection', () =>
     34   expect(
     35     Cleanup.selectReceivedCleanupIds([
     36       ...Array.from({ length: 20 }, (_, n) => id(n)),
     37       'not-an-event-id'
     38     ])
     39   ).toBeUndefined());
     40 test('empty oversized uppercase and nonarray review selections refuse without reset', () => {
     41   for (const value of [
     42     [],
     43     Array.from({ length: 2001 }, (_, n) => id(n)),
     44     ['A'.repeat(64)],
     45     {},
     46     null
     47   ])
     48     expect(Cleanup.selectReceivedCleanupIds(value)).toBeUndefined();
     49   expect(
     50     Cleanup.selectReceivedCleanupIds(
     51       Array.from({ length: 2000 }, (_, n) => id(n))
     52     )?.length
     53   ).toBe(2000);
     54 });
     55 test('ciphertext capacity exposes explicit local-copy review and never automatic eviction', () =>
     56   expect(Budget.inboxRecoveryChoices('capacity')).toEqual([
     57     'review_local_received_copies',
     58     'review_relay_recovery'
     59   ]));
     60 test('transport exhaustion exposes reviewed recovery while unrelated states grant no action', () => {
     61   expect(Budget.inboxRecoveryChoices('budget')).toEqual([
     62     'wait_then_review_refresh',
     63     'review_history_scope',
     64     'review_local_received_copies'
     65   ]);
     66   expect(Budget.inboxRecoveryChoices(null)).toEqual([]);
     67   expect(Budget.inboxRecoveryChoices('complete')).toEqual([]);
     68 });
     69 test('SSR copied cleanup authority cannot admit or delete any local copy', async () => {
     70   expect(
     71     await Cleanup.captureReceivedCleanup(
     72       {} as PrivateStorageRepository,
     73       {} as PrivateSession,
     74       [id(1)],
     75       'reviewed_received_cleanup_selection'
     76     )
     77   ).toBeUndefined();
     78   expect(
     79     await Cleanup.deleteReviewedReceivedCopies(
     80       {} as Cleanup.ReceivedCleanup,
     81       'reviewed_delete_local_received_copies'
     82     )
     83   ).toEqual({ status: 'invalid' });
     84 });
     85 
     86 test('actual finite elapsed reason exposes reviewed transport recovery without deadline widening', () =>
     87   expect(Budget.inboxRecoveryChoices('elapsed')).toEqual([
     88     'wait_then_review_refresh',
     89     'review_history_scope',
     90     'review_local_received_copies'
     91   ]));