Files
ghostwire/tests/self_and_contacts.cjs
T

37 lines
1.9 KiB
JavaScript

const assert=require('node:assert/strict');
const vm=require('node:vm');
const fs=require('node:fs');
const probes=JSON.parse(fs.readFileSync(process.argv[2],'utf8'));
const me={toString:()=> 'self@lid'};
const other={toString:()=> 'other@lid'};
let sends=0;
const data={id:{remote:me,toString:()=> 'self-message'},body:'fixture'};
const chat={id:me,msgs:{get:()=>({ack:3})}};
const modules={
WAWebUserPrefsMeUser:{getMaybeMeLidUser:()=>me,isMeAccount:v=>v===me},
WAWebFindChatAction:{findOrCreateLatestChat:async()=>({chat})},
WAWebSendTextMsgChatAction:{createTextMsgData:async()=>data,addAndSendTextMsg:async()=>{sends++;}},
WAWebSchemaMessage:{getMessageTable:()=>({get:async()=>({body:'fixture',t:123})})},
WAWebDBMessageSerialization:{messageFromDbRow:v=>v},
WAWebSchemaContact_DO_NOT_USE_DIRECTLY:{getContactTable:()=>({all:async()=>[
{id:'a@lid',name:'Grüße 😺',phoneNumber:'123',unexpectedSecret:{value:'never export'}},
{id:'b@lid',name:null,isBusiness:true},
]})},
};
const run=code=>vm.runInNewContext(code,{require:name=>modules[name]});
(async()=>{
const good=await run(probes.send);
assert.equal(sends,1);assert.equal(good.persisted,true);assert.equal(good.plaintext_matches,true);
chat.id=other;
await assert.rejects(run(probes.send),/Refusing to send outside own account/);
assert.equal(sends,1);
chat.id=me;data.id.remote=other;
await assert.rejects(run(probes.send),/not addressed to self/);
assert.equal(sends,1);
const contacts=await run(probes.contacts);
assert.equal(contacts.count,2);assert.equal(contacts.contacts[0].name,'Grüße 😺');
assert.equal('unexpectedSecret' in contacts.contacts[0],false);
assert.equal(contacts.cdp,false);assert.equal(contacts.ui_automation,false);
console.log('PASS: send once, own-chat guard, own-recipient guard, plaintext readback, contact fields');
})().catch(error=>{console.error(error);process.exitCode=1;});