openvibers/openvibe.ai Commits
AI tests: remove the abuse-pow nonce and credits-run cancel races (#343)
Changes
@@@ -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');@@@ -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];
@@ -32,7 +32,10 @@ t.test('a solved challenge is accepted exactly once', async () => {
@@ -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 () => {
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 pow = createPow({ db: tdb.db, clock, bits: 5, ttlMs: 60_000 });
const one = await pow.issue({});
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 two = await pow.issue({ deviceHash: 'b'.repeat(64) });
const good = `${two.challenge}:${solve(two.challenge, 5)}`;
const good = `${two.challenge}:${solve(two.challenge, 5)}`;
assert.strictEqual(await pow.verify(good, { deviceHash: 'c'.repeat(64) }), false, 'issued for one device');
assert.strictEqual(await pow.verify(good, { deviceHash: 'c'.repeat(64) }), false, 'issued for one device');
@@ -404,6 +404,11 @@ t.test('an abort mid-stream settles the partial usage, never a release', async (
@@ -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.ok(run, 'the run reached a terminal state');
assert.strictEqual(run.status, 'cancelled');
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.releases.length, 0, 'an abort settles; it does not release');
assert.strictEqual(billing.settlements.size, 1);
assert.strictEqual(billing.settlements.size, 1);
const settleCall = billing.calls.filter((c) => c.path.endsWith('/settle'))[0];
const settleCall = billing.calls.filter((c) => c.path.endsWith('/settle'))[0];