microduck-reachy-simulator / source /test /browser /training-exercise-programs.spec.js
laureen
Publish static Space from 6953707
b968036
Raw History Blame Contribute Delete
2.94 kB
import { test, expect } from '@playwright/test';
import { readdir } from 'node:fs/promises';
test('reference C programs compile and handle draw, invalid input, traversal and statistics', async ({ page }) => {
test.setTimeout(180000);
const workerPath = process.env.PLAYWRIGHT_PREVIEW === '1'
? `/bundle/${(await readdir('dist/bundle')).find(name => name.startsWith('compiler.worker-') && name.endsWith('.js'))}`
: '/src/training/compiler.worker.js';
await page.goto('/');
const outputs = await page.evaluate(async workerPath => {
const worker = new Worker(workerPath, { type: 'module' });
const cases = [
['tic-tac-toe.c', ['invalid', '0', '1', '1', '2', '3', '5', '4', '6', '8', '7', '9']],
['tic-tac-toe.c', ['1', '4', '2', '5', '9', '6']],
['robot-choreography.c', ['0', '4', '1', '2', '3', '4']],
['robot-scoreboard.c', ['-1', '20', '10', '0', '10', '10', '10']],
];
const results = [];
try {
for (const [file, values] of cases) {
const source = await (await fetch(`/scripts/${file}`)).text();
results.push(await new Promise((resolve, reject) => {
let text = '', index = 0;
worker.onerror = event => reject(new Error(event.message));
worker.onmessage = ({ data }) => {
if (data.type === 'compiled') {
if (!data.ok) reject(new Error(data.diagnostics));
else worker.postMessage({ type: 'run' });
} else if (data.type === 'output') { text += data.text; worker.postMessage({ type: 'reply' }); }
else if (data.type === 'input') worker.postMessage({ type: 'reply', value: index < values.length ? `${values[index++]}\n` : null });
else if (data.type === 'flush') worker.postMessage({ type: 'reply' });
else if (data.type === 'exited') resolve({ code: data.code, text });
else if (data.type === 'error') reject(new Error(data.text));
};
worker.postMessage({ type: 'compile', source, baseUrl: `${location.origin}/` });
}));
}
} finally { worker.terminate(); }
return results;
}, workerPath);
outputs.forEach(output => expect(output.code).toBe(0));
expect(outputs[0].text).toContain('tictactoe result 0');
expect(outputs[0].text).toContain('Invalid or occupied cell');
expect(outputs[1].text).toContain('tictactoe result 2');
expect(outputs[2].text.match(/robot (?:microduck|reachy) \w+/g)).toEqual([
'robot microduck excited', 'robot microduck sad', 'robot reachy proud1', 'robot reachy reprimand1',
'robot reachy reprimand1', 'robot reachy proud1', 'robot microduck sad', 'robot microduck excited',
]);
expect(outputs[3].text).toContain('total 30, average 10.00, best round 1');
expect(outputs[3].text).toContain('It is a tie!');
expect(outputs[3].text).toContain('robot microduck excited');
expect(outputs[3].text).toContain('robot reachy proud1');
});