mirror of
https://github.com/anomalyco/opencode.git
synced 2026-11-26 23:28:43 +00:00
Add packages/cli-rs, an OpenCode client written in Rust on OpenTUI's Rust crates. It is a client only.
210 lines
8.6 KiB
TypeScript
210 lines
8.6 KiB
TypeScript
// P-TUI comparison (docs/testing.md): captures become H.1 cell fixtures, masked with H.2's `applyMasks`, and compared
|
|
// cell by cell as `opencode_parity::compare` does. Requests follow H.2's P-REQ rule.
|
|
import type { Frontend } from "@opencode/protocol/simulation"
|
|
import type { Deviation } from "../lib/app"
|
|
import type { Fixture } from "../lib/cells"
|
|
import { normalize } from "../cli/client"
|
|
import type { Logged } from "./proxy"
|
|
|
|
// The cells listed in a report before it summarizes the rest, as in crates/parity.
|
|
const REPORT_CELLS = 40
|
|
|
|
const segmenter = new Intl.Segmenter()
|
|
|
|
// One symbol per cell, the trailing cells of a wide grapheme "", as the H.1 format keeps them. Captures carry no
|
|
// palette intent, so a color is plain `#rrggbbaa`.
|
|
export function toFixture(frame: Frontend.CapturedFrame): Fixture {
|
|
const color = (rgba: Frontend.Color) => "#" + rgba.map((part) => part.toString(16).padStart(2, "0")).join("")
|
|
const styles = new Map<string, number>()
|
|
const rows = frame.lines.map((line) => {
|
|
const cells = line.spans.flatMap((span) => {
|
|
const style = `${color(span.fg)} ${color(span.bg)} ${span.attributes & 0xff}`
|
|
const symbols = [...segmenter.segment(span.text)].flatMap(({ segment }) => [
|
|
segment,
|
|
...Array<string>(Math.max(0, Bun.stringWidth(segment) - 1)).fill(""),
|
|
])
|
|
return Array.from({ length: span.width }, (_, index) => ({ symbol: symbols[index] ?? " ", style }))
|
|
})
|
|
return Array.from({ length: frame.cols }, (_, x) => cells[x] ?? { symbol: " ", style: "" })
|
|
})
|
|
return {
|
|
width: frame.cols,
|
|
height: frame.rows,
|
|
rows: rows.map((row) => {
|
|
const symbols = row.map((cell) => cell.symbol)
|
|
return symbols.every((symbol) => [...symbol].length === 1) ? symbols.join("") : symbols
|
|
}),
|
|
runs: rows.map((row) =>
|
|
row.reduce<[number, number][]>((runs, cell) => {
|
|
const style = styles.get(cell.style) ?? styles.set(cell.style, styles.size).get(cell.style)!
|
|
const last = runs.at(-1)
|
|
if (last && last[0] === style) last[1]++
|
|
else runs.push([style, 1])
|
|
return runs
|
|
}, []),
|
|
),
|
|
styles: [...styles.keys()],
|
|
}
|
|
}
|
|
|
|
export function text(fixture: Fixture) {
|
|
return cells(fixture)
|
|
.map((row) =>
|
|
row
|
|
.map((cell) => cell.symbol)
|
|
.join("")
|
|
.trimEnd(),
|
|
)
|
|
.join("\n")
|
|
}
|
|
|
|
// `text`: the differing cells whose symbols differ.
|
|
export type Comparison = { equal: boolean; differences: number; text: number; colorOnly: number; report: string }
|
|
|
|
// Cells differ when their symbol, colors, or attributes differ. The report has the shape of crates/parity's:
|
|
// each differing row from both sides with markers, then the first differing cells.
|
|
export function compareFixtures(expected: Fixture, actual: Fixture): Comparison {
|
|
if (expected.width !== actual.width || expected.height !== actual.height)
|
|
return {
|
|
equal: false,
|
|
differences: expected.width * expected.height,
|
|
text: expected.width * expected.height,
|
|
colorOnly: 0,
|
|
report: `size ${expected.width}x${expected.height} expected, ${actual.width}x${actual.height} actual`,
|
|
}
|
|
const a = cells(expected)
|
|
const b = cells(actual)
|
|
const differences = a.flatMap((row, y) =>
|
|
row.flatMap((cell, x) =>
|
|
cell.symbol === b[y][x].symbol && cell.style === b[y][x].style ? [] : [{ x, y, want: cell, got: b[y][x] }],
|
|
),
|
|
)
|
|
if (differences.length === 0)
|
|
return {
|
|
equal: true,
|
|
differences: 0,
|
|
text: 0,
|
|
colorOnly: 0,
|
|
report: `all ${expected.width * expected.height} cells equal`,
|
|
}
|
|
// The cells whose text matches and whose colors do not, as crates/parity's summary counts them: a background, or
|
|
// a foreground behind a visible symbol.
|
|
const colorOnly = differences.filter(({ want, got }) => {
|
|
if (want.symbol !== got.symbol) return false
|
|
const [wantFg, wantBg] = want.style.split(" ")
|
|
const [gotFg, gotBg] = got.style.split(" ")
|
|
return wantBg !== gotBg || (want.symbol !== " " && wantFg !== gotFg)
|
|
}).length
|
|
const rows = [...new Set(differences.map((cell) => cell.y))]
|
|
const lines = [
|
|
`${differences.length} of ${expected.width * expected.height} cells differ (${expected.width}x${expected.height})` +
|
|
(colorOnly > 0 ? `, ${colorOnly} in color only` : ""),
|
|
...rows.flatMap((y) => {
|
|
const marker = Array.from({ length: expected.width }, (_, x) =>
|
|
differences.some((cell) => cell.x === x && cell.y === y) ? "^" : " ",
|
|
)
|
|
return [
|
|
`row ${y}`,
|
|
` expected |${a[y].map((cell) => cell.symbol).join("")}|`,
|
|
` actual |${b[y].map((cell) => cell.symbol).join("")}|`,
|
|
` ${marker.join("").trimEnd()}`,
|
|
]
|
|
}),
|
|
...differences.slice(0, REPORT_CELLS).map(({ x, y, want, got }) => {
|
|
const fields = [
|
|
...(want.symbol === got.symbol
|
|
? []
|
|
: [`symbol ${JSON.stringify(want.symbol)} -> ${JSON.stringify(got.symbol)}`]),
|
|
...(want.style === got.style ? [] : [`style ${want.style} -> ${got.style}`]),
|
|
]
|
|
return `(${x},${y}) ${fields.join(", ")}`
|
|
}),
|
|
...(differences.length > REPORT_CELLS ? [`... and ${differences.length - REPORT_CELLS} more cells`] : []),
|
|
]
|
|
return {
|
|
equal: false,
|
|
differences: differences.length,
|
|
text: differences.filter(({ want, got }) => want.symbol !== got.symbol).length,
|
|
colorOnly,
|
|
report: lines.join("\n"),
|
|
}
|
|
}
|
|
|
|
// Whether `deviation` allows `comparison`: at most the cells its entries add up to, and cells whose symbols differ
|
|
// only within the entries that are not `styleOnly`.
|
|
export function allows(deviation: Deviation[], comparison: Comparison) {
|
|
const sum = (entries: Deviation[]) => entries.reduce((total, entry) => total + entry.cells, 0)
|
|
return (
|
|
comparison.differences <= sum(deviation) && comparison.text <= sum(deviation.filter((entry) => !entry.styleOnly))
|
|
)
|
|
}
|
|
|
|
function cells(fixture: Fixture) {
|
|
return fixture.rows.map((row, y) => {
|
|
const symbols = typeof row === "string" ? [...row] : row
|
|
const styles = fixture.runs[y].flatMap(([style, count]) => Array<string>(count).fill(fixture.styles[style]))
|
|
return symbols.map((symbol, x) => ({ symbol, style: styles[x] }))
|
|
})
|
|
}
|
|
|
|
// Any other server-minted ID, such as `sh_1abfe888b001Ly5UDI8NNG6vOi` (crates/parity `minted`: a lowercase prefix
|
|
// and at least 20 letters and digits).
|
|
const minted = /\b([a-z]+)_[A-Za-z0-9]{20,}\b/g
|
|
|
|
// H.2's `isRead`: a GET, or a plugin RPC named `get`.
|
|
export function isRead(method: string, path: string) {
|
|
return method === "GET" || /^\/api\/rpc\/[^/]+\/get$/.test(path)
|
|
}
|
|
|
|
export type RequestComparison = {
|
|
equal: boolean
|
|
writes: { a: string[]; b: string[] }
|
|
onlyA: string[]
|
|
onlyB: string[]
|
|
}
|
|
|
|
// Writes must match in order after the normalizers, with their sorted query and any status other than 2xx; reads
|
|
// one side sent and the other did not are listed only.
|
|
export function compareRequests(a: Logged[], b: Logged[], root: string): RequestComparison {
|
|
// A live server stamps real times, so epoch milliseconds are normalized too.
|
|
// Bodies are compared as JSON values, so key order does not count.
|
|
const line = (entry: Logged) => {
|
|
const query = new URLSearchParams(
|
|
Object.keys(entry.query)
|
|
.toSorted()
|
|
.flatMap((key) => entry.query[key].map((value) => [key, value])),
|
|
).toString()
|
|
const status =
|
|
entry.status === undefined || (entry.status >= 200 && entry.status < 300) ? "" : ` -> ${entry.status}`
|
|
const body = entry.body === undefined ? "" : " " + JSON.stringify(sorted(entry.body))
|
|
return normalize(`${entry.method} ${entry.path}${query ? "?" + query : ""}${body}${status}`, { root })
|
|
.replace(minted, "<$1>")
|
|
.replace(/\b1\d{12}\b/g, "<epoch>")
|
|
}
|
|
const writes = (log: Logged[]) => log.filter((entry) => !isRead(entry.method, entry.path)).map(line)
|
|
const reads = (log: Logged[]) => [
|
|
...new Set(
|
|
log
|
|
.filter((entry) => isRead(entry.method, entry.path))
|
|
.map((entry) => normalize(`${entry.method} ${entry.path}`, { root }).replace(minted, "<$1>")),
|
|
),
|
|
]
|
|
const readsA = reads(a)
|
|
const readsB = reads(b)
|
|
const result = {
|
|
writes: { a: writes(a), b: writes(b) },
|
|
onlyA: readsA.filter((read) => !readsB.includes(read)),
|
|
onlyB: readsB.filter((read) => !readsA.includes(read)),
|
|
}
|
|
return { equal: JSON.stringify(result.writes.a) === JSON.stringify(result.writes.b), ...result }
|
|
}
|
|
|
|
function sorted(value: unknown): unknown {
|
|
if (Array.isArray(value)) return value.map(sorted)
|
|
if (typeof value !== "object" || value === null) return value
|
|
return Object.fromEntries(
|
|
Object.entries(value)
|
|
.toSorted(([a], [b]) => a.localeCompare(b))
|
|
.map(([key, item]) => [key, sorted(item)]),
|
|
)
|
|
}
|