logigate/app/components/editor/BenchDialog.vue

165 lines
8.8 KiB
Vue

<script setup lang="ts">
// The tests of a test bench: a row per test, the component's inputs on the
// left, what its outputs should show on the right, ticks to give it, and
// how each row went. They run again after every change.
import { benchColumns, cellOf, combinationRows, recordExpected } from '~/lib/logic/bench'
import type { BenchRow } from '~/lib/logic/types'
const ed = useEditor()
const close = () => (ed.benchOpen.value = null)
const node = computed(() => ed.def.value.nodes.find(n => n.id === ed.benchOpen.value && n.kind === 'bench'))
watch(node, n => !n && close())
const target = computed(() => (node.value ? ed.benchTarget(node.value) : undefined))
const cols = computed(() => (target.value ? benchColumns(target.value.def) : { ins: [], outs: [] }))
const rows = computed(() => node.value?.bench?.rows ?? [])
const result = computed(() => (node.value ? ed.benchResults.value.get(node.value.id) : undefined))
/** Components in this circuit it can be docked to. */
const chips = computed(() =>
ed.def.value.nodes
.filter(n => n.kind === 'chip' && n.chip)
.map(n => ({ id: n.id, name: `${ed.getDef.value(n.chip!)?.name ?? 'Missing'}${n.label?.trim() && !n.autoLabel ? ` · ${n.label.trim()}` : ''}` })),
)
function onKey(e: KeyboardEvent) {
if (e.key === 'Escape') close()
}
onMounted(() => {
window.addEventListener('keydown', onKey)
if (node.value && !result.value) ed.runBenchNow(node.value.id)
})
onBeforeUnmount(() => window.removeEventListener('keydown', onKey))
const id = () => node.value!.id
const dock = (chipId: string) => ed.dockBench(id(), chipId || null)
function setCell(i: number, side: 'set' | 'expect', name: string, v: string) {
ed.setBench(id(), b => {
const row = b.rows[i]
if (!row) return
const cells = { ...(row[side] ?? {}) }
if (v.trim()) cells[name] = v.trim()
else delete cells[name]
if (Object.keys(cells).length) row[side] = cells
else delete row[side]
})
}
function setTicks(i: number, v: string) {
ed.setBench(id(), b => {
const row = b.rows[i]
if (!row) return
if (v.trim()) row.ticks = v.trim()
else delete row.ticks
})
}
function addRow(at = rows.value.length) {
// A new row starts as a copy of the one above, without its checks.
const above = rows.value[at - 1]
const row: BenchRow = above?.set ? { set: { ...above.set } } : {}
ed.setBench(id(), b => b.rows.splice(at, 0, row))
}
const removeRow = (i: number) => ed.setBench(id(), b => b.rows.splice(i, 1))
function combinations() {
const t = target.value
if (!t) return
const r = combinationRows(t.def)
if ('error' in r) return ed.notify(r.error, 'error')
ed.setBench(id(), b => (b.rows = r))
ed.notify(`${r.length} rows: one for every input combination. Fill in what it should show, or take what it does now.`)
}
function takeExpected() {
const t = target.value
if (!t || !rows.value.length) return
const filled = recordExpected(ed.getDef.value, t.def.id, rows.value)
ed.setBench(id(), b => (b.rows = filled))
ed.notify('Expected outputs set to what it shows now: the tests keep it that way')
}
/** The value a failing output cell showed, when it differs. */
function gotNote(i: number, name: string): string {
const r = result.value?.rows[i]
if (!r || r.ok !== false) return ''
const want = cellOf(rows.value[i]!, 'expect', name)
const got = r.got[name]
if (got === undefined || !want) return ''
return String(got) === want || (/^0[xb]/i.test(want) && Number(want) === got) ? '' : `is ${got}`
}
</script>
<template>
<div class="modal-backdrop" @click.self="close">
<div class="modal bench" role="dialog" aria-labelledby="bench-title">
<div class="flex items-start justify-between gap-4">
<h2 id="bench-title">Tests</h2>
<button class="tb-btn" title="Close (Esc)" @click="close"><Icon name="lucide:x" class="size-4" /></button>
</div>
<p class="hint">
Each row sets the inputs, gives it ticks, then checks the outputs. It runs on its own from all zeros; what it holds carries to the next row.
Inputs: a number (12, 0x0C, 0b1100), <code>P</code> for a clock pulse, empty to leave it. Outputs: what it should show, empty for any.
Ticks: <code>5</code> pulses (the P inputs and its own clocks), or <code>≤1000</code> to wait until the outputs are right.
</p>
<div class="bench-bar">
<label class="bench-target">
<span>Tests</span>
<select class="tb-select" :value="node?.bench?.on ?? ''" @change="dock(($event.target as HTMLSelectElement).value)">
<option value="">nothing yet</option>
<option v-for="c in chips" :key="c.id" :value="c.id">{{ c.name }}</option>
</select>
</label>
<span class="grow" />
<button class="btn-sm" :disabled="!target" title="A row for every combination of the inputs" @click="combinations"><Icon name="lucide:grid-3x3" class="size-3.5" /> Every combination</button>
<button class="btn-sm" :disabled="!target || !rows.length" title="Expected outputs: what it shows now" @click="takeExpected"><Icon name="lucide:camera" class="size-3.5" /> Take what it does</button>
<button class="btn-sm" :disabled="!target || !rows.length" title="Run again, longer runs allowed" @click="node && ed.runBenchNow(node.id, true)"><Icon name="lucide:play" class="size-3.5" /> Run</button>
</div>
<p v-if="!target" class="hint bench-empty">Pick the component it tests above, or drop the bench on one.</p>
<template v-else>
<p v-if="result" class="bench-summary" :class="result.fail || result.error ? 'fail' : result.pass ? 'pass' : ''">
<template v-if="result.error">{{ result.error }}. </template>
<template v-if="result.fail">{{ result.fail }} failing, {{ result.pass }} pass</template>
<template v-else-if="result.pass">All {{ result.pass }} pass</template>
<template v-else-if="!result.error">Nothing checked yet</template>
</p>
<div class="bench-table-wrap">
<table class="bench-table">
<thead>
<tr>
<th class="bench-num">#</th>
<th v-for="c in cols.ins" :key="`i-${c.name}`" :title="c.keys.length > 1 ? `${c.keys.length} bits` : ''">{{ c.name }}<small v-if="c.keys.length > 1">{{ c.keys.length }}</small></th>
<th class="bench-arrow">→</th>
<th v-for="c in cols.outs" :key="`o-${c.name}`" class="out" :title="c.keys.length > 1 ? `${c.keys.length} bits` : ''">{{ c.name }}<small v-if="c.keys.length > 1">{{ c.keys.length }}</small></th>
<th>Ticks</th>
<th />
</tr>
</thead>
<tbody>
<tr v-for="(row, i) in rows" :key="i" :class="result?.rows[i]?.ok === false ? 'fail' : result?.rows[i]?.ok ? 'pass' : ''">
<td class="bench-num tabular-nums">{{ i + 1 }}</td>
<td v-for="c in cols.ins" :key="`i-${c.name}`">
<input :value="cellOf(row, 'set', c.name)" class="bench-cell" spellcheck="false" @change="setCell(i, 'set', c.name, ($event.target as HTMLInputElement).value)" />
</td>
<td class="bench-arrow" :title="result?.rows[i]?.error ?? ''">
<Icon v-if="result?.rows[i]?.ok === false" name="lucide:x" class="size-3.5 text-(--danger)" />
<Icon v-else-if="result?.rows[i]?.ok" name="lucide:check" class="size-3.5 text-(--on)" />
</td>
<td v-for="c in cols.outs" :key="`o-${c.name}`" class="out">
<input :value="cellOf(row, 'expect', c.name)" class="bench-cell" spellcheck="false" :placeholder="String(result?.rows[i]?.got[c.name] ?? '')" @change="setCell(i, 'expect', c.name, ($event.target as HTMLInputElement).value)" />
<span v-if="gotNote(i, c.name)" class="bench-got">{{ gotNote(i, c.name) }}</span>
</td>
<td>
<input :value="row.ticks ?? ''" class="bench-cell ticks" spellcheck="false" :placeholder="result?.rows[i]?.ticks ? String(result.rows[i]!.ticks) : ''" @change="setTicks(i, ($event.target as HTMLInputElement).value)" />
</td>
<td class="bench-actions">
<span v-if="result?.rows[i]?.error" class="bench-err" :title="result.rows[i]!.error">{{ result.rows[i]!.error }}</span>
<button class="icon-btn" title="New row below" @click="addRow(i + 1)"><Icon name="lucide:plus" class="size-3.5" /></button>
<button class="icon-btn" title="Remove this row" @click="removeRow(i)"><Icon name="lucide:trash-2" class="size-3.5" /></button>
</td>
</tr>
</tbody>
</table>
</div>
<button class="btn-sm bench-add" @click="addRow()"><Icon name="lucide:plus" class="size-3.5" /> Row</button>
</template>
</div>
</div>
</template>