41 lines
1.8 KiB
JavaScript
41 lines
1.8 KiB
JavaScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { loadSessionsSequentially, populateVfs } from '../src/platform/runtime/session-loading.js';
|
|
|
|
test('policy loading keeps every action, compiles one at a time and rolls back on failure', async () => {
|
|
let active = 0, peak = 0;
|
|
const released = [];
|
|
const create = async (id) => {
|
|
peak = Math.max(peak, ++active);
|
|
await new Promise((r) => setTimeout(r, 1));
|
|
active--;
|
|
return { id, release: async () => released.push(id) };
|
|
};
|
|
const entries = ['walk', 'sit', 'kick', 'roll'].map((id) => [id, id]);
|
|
const sessions = await loadSessionsSequentially(entries, create);
|
|
assert.deepEqual(Object.keys(sessions), entries.map(([id]) => id));
|
|
assert.equal(peak, 1);
|
|
assert.deepEqual(released, []);
|
|
await assert.rejects(loadSessionsSequentially(entries, create, async (s) => {
|
|
if (s.id === 'kick') throw new Error('validation failed');
|
|
}), /validation failed/);
|
|
assert.deepEqual(released.sort(), ['kick', 'sit', 'walk']);
|
|
});
|
|
|
|
test('VFS copies downloads incrementally; failure drains pending downloads before cleanup', async () => {
|
|
const added = [], assets = [1, 2, 3, 4].map((i) => ({ vfs: String(i) }));
|
|
await populateVfs({ addBuffer: (path, bytes) => added.push([path, [...bytes]]) }, assets,
|
|
async (a) => Uint8Array.of(Number(a.vfs)).buffer);
|
|
assert.deepEqual(added, assets.map((a) => [a.vfs, [Number(a.vfs)]]));
|
|
let pending = 0;
|
|
await assert.rejects(populateVfs({ addBuffer: () => assert.fail('no writes after failure') }, assets, async (a) => {
|
|
pending++;
|
|
try {
|
|
await new Promise((r) => setTimeout(r, a.vfs === '1' ? 1 : 15));
|
|
if (a.vfs === '1') throw new Error('aborted');
|
|
return new ArrayBuffer(1);
|
|
} finally { pending--; }
|
|
}), /aborted/);
|
|
assert.equal(pending, 0);
|
|
});
|