MC/website/tests/mail-metadata-cache.test.mjs
2026-11-27 23:43:05 +00:00

41 lines
3.5 KiB
JavaScript

import test from 'node:test';import assert from 'node:assert/strict';
import {cacheMailMetadata} from '../node/mail-metadata-cache.mjs';
const status={action:'internal_status',userId:'person-a'};
test('metadata is reused for fifteen seconds, with independent return values',async()=>{
let now=0,calls=0;const cached=cacheMailMetadata(async()=>({ok:true,active:true,capabilities:{read:++calls}}),{clock:()=>now});
const first=await cached(status);first.capabilities.read=999;now=14999;
assert.equal((await cached(status)).capabilities.read,1);assert.equal(calls,1);
now=15000;assert.equal((await cached(status)).capabilities.read,2);
});
test('simultaneous identical checks share one upstream operation',async()=>{
let calls=0,release;const cached=cacheMailMetadata(async()=>{calls++;await new Promise(r=>release=r);return {active:true};});
const requests=Array.from({length:8},()=>cached(status));await new Promise(r=>setImmediate(r));assert.equal(calls,1);release();await Promise.all(requests);
});
test('users, mailbox resources and provisioning parameters stay isolated',async()=>{
let calls=0;const cached=cacheMailMetadata(async()=>({active:true,sequence:++calls}));
for(const payload of [status,{...status,userId:'person-b'},{...status,action:'internal_ensure',mailboxKind:'clan',resourceId:'clan-a'},{...status,action:'internal_ensure',mailboxKind:'clan',resourceId:'clan-b'}])await cached(payload);
assert.equal(calls,4);await cached({...status});assert.equal(calls,4);
});
for(const result of [{active:false},{configured:false},{ok:false},{error:'unavailable'},{active:true,password:'secret'},{active:true,nested:{credential:'secret'}}])test(`unsafe or unavailable result is not retained: ${JSON.stringify(Object.keys(result))}`,async()=>{
let calls=0;const cached=cacheMailMetadata(async()=>{calls++;return result;});await cached(status);await cached(status);assert.equal(calls,2);
});
test('errors are not cached and a later request can recover',async()=>{
let calls=0;const cached=cacheMailMetadata(async()=>{if(++calls===1)throw Error('offline');return {active:true};});
await assert.rejects(cached(status),/offline/);await cached(status);assert.equal(calls,2);
});
test('messages, password reveal and sends are always fresh; writes invalidate metadata',async()=>{
const calls=[];const cached=cacheMailMetadata(async p=>{calls.push(p.action);return {active:true};});
await cached(status);await cached(status);
for(const action of ['internal_list','internal_get','internal_password_reveal']){await cached({...status,action});await cached({...status,action});}
await cached(status);assert.equal(calls.filter(a=>a==='internal_status').length,1);
await cached({...status,action:'set_password',password:'not-a-real-password'});await cached(status);
assert.equal(calls.filter(a=>a==='internal_status').length,2);assert.equal(calls.filter(a=>a==='internal_list').length,2);
});
test('a late status reply cannot repopulate metadata invalidated by a write',async()=>{
let release,calls=0;const cached=cacheMailMetadata(async p=>{if(p.action==='internal_status'){calls++;if(calls===1)await new Promise(r=>release=r);}return {active:true};});
const pending=cached(status);await new Promise(r=>setImmediate(r));await cached({...status,action:'internal_password'});release();await pending;await cached(status);assert.equal(calls,2);
});
test('cache size is bounded and oldest entries are evicted',async()=>{
let calls=0;const cached=cacheMailMetadata(async()=>({active:true,sequence:++calls}),{maxEntries:2});
for(const userId of ['a','b','c','a'])await cached({...status,userId});assert.equal(calls,4);
});