openvibers/openvibe.ai Commits

AI tests: remove the abuse-pow nonce and credits-run cancel races (#343)

OP OpenVibers committed 6 days ago · d633a8728a · 2 files, +9 −1

abuse-pow: nonce 0 satisfies a 5-bit challenge about one time in 32, so it
is not a reliable wrong nonce. Search for a nonce that is actually under
the target before asserting verify() refuses it.

credits-run: the cancel route marks a run cancelled synchronously, but the
worker settles (or releases) the hold while unwinding the stream. Wait for
that settlement before asserting on Billing instead of racing it.

Changes

ddiff --git a/test/abuse-pow.test.js b/test/abuse-pow.test.js
iindex addb74a..eb3bf0e 100644
---- a/test/abuse-pow.test.js
++++ b/test/abuse-pow.test.js
@@@ -32,7 +32,10 @@ t.test('a solved challenge is accepted exactly once', async () => {
t.test('a wrong nonce, an expired challenge and another device are refused', async () => {
const pow = createPow({ db: tdb.db, clock, bits: 5, ttlMs: 60_000 });
const one = await pow.issue({});
-- assert.strictEqual(await pow.verify(`${one.challenge}:0`, {}), false);
++ // Nonce 0 solves roughly one challenge in 32 at 5 bits, so it is not a reliable "wrong" nonce.
++ let wrong = 0;
++ while (zeroBits(digest(one.challenge, wrong)) >= 5) wrong++;
++ assert.strictEqual(await pow.verify(`${one.challenge}:${wrong}`, {}), false);
const two = await pow.issue({ deviceHash: 'b'.repeat(64) });
const good = `${two.challenge}:${solve(two.challenge, 5)}`;
assert.strictEqual(await pow.verify(good, { deviceHash: 'c'.repeat(64) }), false, 'issued for one device');
ddiff --git a/test/credits-run.test.js b/test/credits-run.test.js
iindex d598ce9..b1f6d38 100644
---- a/test/credits-run.test.js
++++ b/test/credits-run.test.js
@@@ -404,6 +404,11 @@ t.test('an abort mid-stream settles the partial usage, never a release', async (
}
assert.ok(run, 'the run reached a terminal state');
assert.strictEqual(run.status, 'cancelled');
++ // The cancel route marks the run cancelled before the worker unwinds the provider stream; the
++ // settle (or release) follows in that unwind. Wait for it instead of assuming it already happened.
++ for (let i = 0; i < 500 && billing.settlements.size === 0 && billing.releases.length === 0; i++) {
++ await new Promise((r) => setTimeout(r, 20));
++ }
assert.strictEqual(billing.releases.length, 0, 'an abort settles; it does not release');
assert.strictEqual(billing.settlements.size, 1);
const settleCall = billing.calls.filter((c) => c.path.endsWith('/settle'))[0];