forked from unimatrix/web
17 lines
5.7 KiB
JavaScript
17 lines
5.7 KiB
JavaScript
import test from 'node:test';import assert from 'node:assert/strict';import {randomUUID} from 'node:crypto';import {companyFixture} from './company-deed-fixture.mjs';import {ownCompanyMailbox,ensureCompanyMailbox} from '../node/internal-mail-companies.mjs';import {clanMailboxId} from '../node/internal-mail-clans.mjs';import {createInternalMail} from '../node/internal-mail.mjs';
|
|
test('company correspondence reuses the original Clan mailbox without broadening Clan access',async()=>{const f=await companyFixture();try{const companyId=await f.create(),clanId=randomUUID();f.db.sqlite.exec('ALTER TABLE site_clans ADD COLUMN tag TEXT;ALTER TABLE site_clan_members ADD COLUMN minecraft_uuid TEXT;');await f.db.query('INSERT INTO site_clans(id,name,tag,owner_id,active) VALUES(?,?,?,?,1)',[clanId,'Original Clan','OLD',f.people.Owner.id]);await f.db.query("INSERT INTO site_clan_members(clan_id,user_id,clan_role) VALUES(?,?,'owner')",[clanId,f.people.Owner.id]);let calls=[];const broker=async p=>{calls.push(p);return{ok:true,address:p.userId+'.internal@dunerealms.com',total:3,messages:[]};};const source=await ownCompanyMailbox(f.db,f.people.Owner,companyId);assert.equal(source.clanId,clanId);const mailbox=await ensureCompanyMailbox(source,broker,f.db);assert.equal(mailbox.userId,clanMailboxId(clanId));assert.equal(calls[0].mailboxKind,'clan');assert.equal(calls[0].resourceId,clanId);assert.equal(await ownCompanyMailbox(f.db,f.people.Other,companyId),null);
|
|
const api=createInternalMail(f.db,{getIdentity:async()=>f.currentUser(),eligible:async()=>true,broker}),call=(path,body)=>api(new Request('https://cp.dunerealms.com/api/internal-mail/'+path,{method:'POST',headers:{origin:'https://cp.dunerealms.com','content-type':'application/json'},body:JSON.stringify({scope:'company',companyId,...body})}));assert.equal((await call('messages/list',{folder:'INBOX',limit:1,unread:true})).status,200);assert.equal(calls.at(-1).userId,clanMailboxId(clanId));assert.equal((await call('password/reveal',{})).status,403);f.actor('Other');calls=[];assert.equal((await call('messages/list',{})).status,403);assert.equal(calls.length,0);
|
|
}finally{f.db.close();}});
|
|
|
|
test('created subsidiaries inherit and pin mother mail, while bought or conquered subsidiaries keep their origin',async()=>{const f=await companyFixture();try{const {companyMailboxClan}=await import('../node/internal-mail-companies.mjs');const mother=await f.create(),home=randomUUID(),foreign=randomUUID();await f.db.query('INSERT INTO site_clans(id,name,owner_id,active) VALUES(?,?,?,1)',[home,'Home',f.people.Owner.id]);assert.equal(await companyMailboxClan(f.db,mother),home);await f.db.query('INSERT INTO site_clans(id,name,owner_id,active) VALUES(?,?,?,1)',[foreign,'Foreign',f.people.Other.id]);for(const [origin,n] of [['created',0],['purchased',1],['conquered',2]]){const id=randomUUID();await f.db.query("INSERT INTO corrino_companies(id,slug,name,description,founded_by,status,created_at,updated_at) VALUES(?,?,?,'',?,'active',1,1)",[id,'child-'+n,'Child '+n,f.people.Other.id]);await f.db.query('INSERT INTO site_company_relations(company_id,parent_company_id,relation_origin,updated_at) VALUES(?,?,?,1)',[id,mother,origin]);assert.equal(await companyMailboxClan(f.db,id),origin==='created'?home:foreign);if(origin==='created'){await f.db.query("UPDATE site_company_relations SET relation_origin='purchased' WHERE company_id=?",[id]);assert.equal(await companyMailboxClan(f.db,id),home);}}}finally{f.db.close();}});
|
|
|
|
test('a standalone Company receives a default mailbox without requiring a Clan',async()=>{const f=await companyFixture();try{f.db.sqlite.exec('ALTER TABLE site_clans ADD COLUMN tag TEXT;');const {defaultCompanyMailbox}=await import('../node/internal-mail-companies.mjs'),id=await f.create();const company=await defaultCompanyMailbox(f.db,f.people.Owner);assert.equal(company.id,id);assert.equal(company.standalone,true);const calls=[],box=await ensureCompanyMailbox(company,async p=>{calls.push(p);return{ok:true,address:'company.internal@dunerealms.com'};},f.db);assert.equal(box.userId,clanMailboxId(id));assert.equal(calls.length,1);assert.equal(await defaultCompanyMailbox(f.db,f.people.Other),null);}finally{f.db.close();}});
|
|
|
|
test('Company-first then Clan creation reuses the existing inbox without moving or duplicating messages',async()=>{
|
|
const f=await companyFixture();try{
|
|
f.db.sqlite.exec('ALTER TABLE site_clans ADD COLUMN tag TEXT;');const {defaultCompanyMailbox}=await import('../node/internal-mail-companies.mjs'),{ensureClanMailbox}=await import('../node/internal-mail-clans.mjs');const companyId=await f.create(),calls=[],broker=async p=>{calls.push(p);return{ok:true,address:'shared.internal@dunerealms.com'};};
|
|
const company=await defaultCompanyMailbox(f.db,f.people.Owner),original=await ensureCompanyMailbox(company,broker,f.db),clanId=randomUUID();await f.db.query('INSERT INTO site_clans(id,name,tag,owner_id,active) VALUES(?,?,?,?,1)',[clanId,'New Clan','NEW',f.people.Owner.id]);await f.db.query("INSERT INTO site_clan_members(clan_id,user_id,clan_role) VALUES(?,?,'owner')",[clanId,f.people.Owner.id]);
|
|
const clan=await ensureClanMailbox(f.db,{id:clanId,name:'New Clan'},broker);assert.equal(clan.userId,original.userId);assert.equal(calls.at(-1).resourceId,companyId);assert.equal((await ensureClanMailbox(f.db,{id:clanId,name:'New Clan'},broker)).userId,original.userId);assert.equal((await f.db.first('SELECT COUNT(*) n FROM site_clan_company_mail_origins')).n,1);
|
|
const api=createInternalMail(f.db,{getIdentity:async()=>f.people.Owner,eligible:async()=>true,broker,staffAccess:async()=>false,governmentAccess:async()=>null});const r=await api(new Request('https://cp.dunerealms.com/api/internal-mail/me'));assert.equal(r.status,200,await r.clone().text());assert.equal((await r.json()).company.sharedWithClan,true);
|
|
}finally{f.db.close();}
|
|
});
|

Staff