Doubao-powered voice input for Linux, Wayland and Omarchy
import { readFile, readdir } from 'node:fs/promises';
import path from 'node:path';
export const SHELL_CHECKS = [
{
key: 'shutdown',
label: 'Shutdown is readable',
},
{
key: 'focus',
label: 'Menu focus highlight is clean',
},
{
key: 'bar',
label: 'Bar is vertical on the left',
},
];
export async function findHtmlFiles(directory) {
const results = [];
async function visit(current) {
for (const entry of await readdir(current, { withFileTypes: true })) {
const item = path.join(current, entry.name);
if (entry.isDirectory()) await visit(item);
else if (entry.isFile() && entry.name.endsWith('.html'))
results.push(item);
}
}
await visit(directory);
const testRunReports = results.filter((file) =>
path.basename(file).startsWith('test-run-') ||
path.basename(path.dirname(file)).startsWith('test-run-'),
);
return (testRunReports.length ? testRunReports : results).sort();
}
// Some report versions put raw control characters inside JSON strings.
export function normalizeJsonControlCharacters(source) {
let normalized = '';
let insideString = false;
let escaped = false;
for (const character of source) {
if (!insideString) {
normalized += character;
if (character === '"') insideString = true;
continue;
}
if (escaped) {
normalized += character;
escaped = false;
} else if (character === '\\') {
normalized += character;
escaped = true;
} else if (character === '"') {
normalized += character;
insideString = false;
} else if (character.charCodeAt(0) <= 0x1f) {
normalized += `\\u${character.charCodeAt(0).toString(16).padStart(4, '0')}`;
} else {
normalized += character;
}
}
return normalized;
}
export function reportDumps(html) {
return [
...html.matchAll(
/<script\s+type=["']midscene_web_dump["'][^>]*>\s*(\{[\s\S]*?)<\/script>/g,
),
].map((match) => JSON.parse(normalizeJsonControlCharacters(match[1])));
}
export function testRunDump(html) {
const match = html.match(
/<script\s+type=["']midscene_test_run_dump["'][^>]*>\s*(\{[\s\S]*?)<\/script>/,
);
return match
? JSON.parse(normalizeJsonControlCharacters(match[1]))
: null;
}
export async function findLatestTestReport(
directory,
{ projectName, required = true } = {},
) {
const reports = await Promise.all(
(await findHtmlFiles(directory)).map(async (file) => {
const html = await readFile(file, 'utf8');
const run = testRunDump(html);
return {
file,
html,
run,
startedAt: Date.parse(run?.startedAt ?? '') || 0,
};
}),
);
const matchingReports = projectName
? reports.filter((report) =>
report.run?.projects?.some((project) => project.name === projectName),
)
: reports;
const latest = matchingReports
.sort((left, right) => left.startedAt - right.startedAt)
.at(-1);
if (!latest) {
if (!required) return null;
throw new Error(
projectName
? `No Midscene Test HTML report found for project ${projectName}`
: 'No Midscene Test HTML report found',
);
}
return latest;
}
const INLINE_REPORT_IMAGE_RE =
/<script\s+type=["']midscene-image["']\s+data-id=["']([^"']+)["'][^>]*>\s*data:image\/(jpeg|png|webp);base64,([A-Za-z0-9+/=]+)\s*<\/script>/g;
const REPORT_IMAGE_EXTENSIONS = new Set(['jpg', 'jpeg', 'png', 'webp']);
// Midscene reports embed screenshots as inline scripts until 1.12.x;
// 1.13.0 writes them to a sibling `screenshots/` directory instead
// (data-screenshot-mode="directory"). Load both so either report shape works.
export async function loadReportImages(html, reportFile) {
const images = new Map();
for (const match of html.matchAll(INLINE_REPORT_IMAGE_RE)) {
images.set(match[1], {
extension: match[2] === 'jpeg' ? 'jpg' : match[2],
bytes: Buffer.from(match[3], 'base64'),
});
}
if (!reportFile) return images;
const screenshotDirectory = path.join(path.dirname(reportFile), 'screenshots');
let entries = [];
try {
entries = await readdir(screenshotDirectory);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
}
for (const entryName of entries.sort()) {
const extension = entryName.split('.').pop()?.toLowerCase();
const id = entryName.slice(0, entryName.lastIndexOf('.'));
if (
!id ||
!REPORT_IMAGE_EXTENSIONS.has(extension) ||
images.has(id)
) {
continue;
}
images.set(id, {
extension: extension === 'jpeg' ? 'jpg' : extension,
bytes: await readFile(path.join(screenshotDirectory, entryName)),
});
}
return images;
}
export function extractShellEvidence(
html,
{ allowIncomplete = false, images = null } = {},
) {
const imageMap =
images ??
(() => {
const inline = new Map();
for (const match of html.matchAll(INLINE_REPORT_IMAGE_RE)) {
inline.set(match[1], {
extension: match[2] === 'jpeg' ? 'jpg' : match[2],
bytes: Buffer.from(match[3], 'base64'),
});
}
return inline;
})();
const finished = [];
for (const dump of reportDumps(html)) {
for (const execution of dump.executions ?? []) {
for (const task of execution.tasks ?? []) {
if (
task.status !== 'finished' ||
task.subType !== 'Assert' ||
typeof task.output !== 'boolean'
) continue;
finished.push(task);
}
}
}
return SHELL_CHECKS.map((check, index) => {
// The report embeds assertions from every retry. A successful shell case
// contributes the final three assertions; earlier failed attempts must not
// displace them in the post-run visual verifier.
const task = finished.slice(-SHELL_CHECKS.length)[index];
if (!task) {
if (allowIncomplete)
return { ...check, passed: false, screenshot: null, missing: true };
throw new Error(`Missing finished Omarchy assertion: ${check.label}`);
}
const screenshot = imageMap.get(task.uiContext?.screenshot?.id);
if (!screenshot) {
if (allowIncomplete)
return { ...check, passed: false, screenshot: null, missing: true };
throw new Error(`Missing Omarchy screenshot: ${check.label}`);
}
return { ...check, passed: task.output === true, screenshot };
});
}
export async function findShellReport(directory) {
for (const file of await findHtmlFiles(directory)) {
const html = await readFile(file, 'utf8');
const run = testRunDump(html);
if (run?.projects?.some((project) => project.name === 'omarchy-shell')) {
return {
file,
html,
checks: extractShellEvidence(html, {
images: await loadReportImages(html, file),
}),
};
}
}
throw new Error('No Omarchy shell Midscene HTML report found');
}