negentropy/test/js/harness.js

54 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2023-03-10 12:19:11 -05:00
const readline = require('readline');
2023-11-01 16:53:46 -04:00
const { Negentropy, NegentropyStorageVector } = require('../../js/Negentropy.js');
2023-03-10 12:19:11 -05:00
let frameSizeLimit = 0;
if (process.env.FRAMESIZELIMIT) frameSizeLimit = parseInt(process.env.FRAMESIZELIMIT);
2023-03-10 12:19:11 -05:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
terminal: false
2023-03-10 12:19:11 -05:00
});
2023-11-01 16:53:46 -04:00
let ne;
let storage = new NegentropyStorageVector();
2023-03-10 12:19:11 -05:00
rl.on('line', async (line) => {
2023-03-10 12:19:11 -05:00
let items = line.split(',');
if (items[0] == "item") {
if (items.length !== 3) throw Error("too few items");
let created = parseInt(items[1]);
let id = items[2].trim();
2023-11-01 16:53:46 -04:00
storage.insert(created, id);
} else if (items[0] == "seal") {
2023-11-01 16:53:46 -04:00
storage.seal();
ne = new Negentropy(storage, frameSizeLimit);
} else if (items[0] == "initiate") {
let q = await ne.initiate();
if (frameSizeLimit && q.length/2 > frameSizeLimit) throw Error("frameSizeLimit exceeded");
console.log(`msg,${q}`);
} else if (items[0] == "msg") {
let q = items[1];
let [newQ, haveIds, needIds] = await ne.reconcile(q);
q = newQ;
2023-03-10 12:19:11 -05:00
for (let id of haveIds) console.log(`have,${id}`);
for (let id of needIds) console.log(`need,${id}`);
2023-03-10 12:19:11 -05:00
2023-09-15 17:44:43 -04:00
if (frameSizeLimit && q !== null && q.length/2 > frameSizeLimit) throw Error("frameSizeLimit exceeded");
if (q === null) {
console.log(`done`);
2023-03-10 12:19:11 -05:00
} else {
console.log(`msg,${q}`);
2023-03-10 12:19:11 -05:00
}
} else {
throw Error(`unknown cmd: ${items[0]}`);
2023-03-10 12:19:11 -05:00
}
});
rl.on('close', () => {
process.exit(0);
});