|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | +import { dirname, join, resolve } from "node:path"; |
| 4 | +import { fileURLToPath } from "node:url"; |
| 5 | +import { afterEach, describe, expect, it } from "vitest"; |
| 6 | +import type { AgentOs } from "../src/index.js"; |
| 7 | +import { createTerm, diffGrids } from "./helpers/term-diff.js"; |
| 8 | + |
| 9 | +const HERE = dirname(fileURLToPath(import.meta.url)); |
| 10 | +const REPO_ROOT = resolve(HERE, "../../.."); |
| 11 | +const VIM_PACKAGE_BIN = resolve( |
| 12 | + REPO_ROOT, |
| 13 | + "../secure-exec/registry/software/vim/dist/package/bin/vim", |
| 14 | +); |
| 15 | +const NATIVE_VIM = "/usr/bin/vim"; |
| 16 | +const REF_SCRIPT = join(HERE, "helpers", "native-vim-ref.py"); |
| 17 | + |
| 18 | +const COLS = 80; |
| 19 | +const ROWS = 24; |
| 20 | +const sleep = (ms: number) => new Promise((r) => setTimeout(r, ms)); |
| 21 | + |
| 22 | +// Shared, ordered key sequence driven identically against native + wasm vim. |
| 23 | +// `wait` is the settle time (seconds native / ms wasm) after the keys. |
| 24 | +const STEPS = [ |
| 25 | + { label: "insert", keys: "i", wait: 0.5 }, |
| 26 | + { label: "type", keys: "The quick brown fox", wait: 0.6 }, |
| 27 | + { label: "newline", keys: "\rjumps over", wait: 0.6 }, |
| 28 | + { label: "esc", keys: "\x1b", wait: 0.4 }, |
| 29 | + { label: "gg", keys: "gg", wait: 0.4 }, |
| 30 | + { label: "cmdline", keys: ":set number\r", wait: 0.5 }, |
| 31 | + { label: "write", keys: ":w\r", wait: 0.6 }, |
| 32 | +]; |
| 33 | + |
| 34 | +interface Snap { |
| 35 | + label: string; |
| 36 | + raw: Uint8Array; |
| 37 | +} |
| 38 | + |
| 39 | +// Force identical, version-independent settings on both vims so a native-9.0 |
| 40 | +// vs wasm-9.2 default difference (e.g. `ruler`) can't masquerade as a behavior |
| 41 | +// difference. With `set ruler` on both, the cursor position on the status line |
| 42 | +// is itself part of the 1:1 assertion. |
| 43 | +const VIM_ARGS = ["-c", "set ruler noshowcmd"]; |
| 44 | +const BASENAME = "parity.txt"; |
| 45 | + |
| 46 | +function nativeSnaps(file: string): Snap[] { |
| 47 | + const spec = { |
| 48 | + vim: NATIVE_VIM, |
| 49 | + cols: COLS, |
| 50 | + rows: ROWS, |
| 51 | + file, |
| 52 | + vimArgs: VIM_ARGS, |
| 53 | + openWait: 1.4, |
| 54 | + steps: STEPS.map((s) => ({ |
| 55 | + label: s.label, |
| 56 | + keys_b64: Buffer.from(s.keys, "utf8").toString("base64"), |
| 57 | + wait: s.wait, |
| 58 | + })), |
| 59 | + }; |
| 60 | + const out = execFileSync("python3", [REF_SCRIPT, JSON.stringify(spec)], { |
| 61 | + encoding: "utf8", |
| 62 | + maxBuffer: 32 * 1024 * 1024, |
| 63 | + }); |
| 64 | + const parsed = JSON.parse(out) as { |
| 65 | + snaps: { label: string; raw_b64: string }[]; |
| 66 | + }; |
| 67 | + return parsed.snaps.map((s) => ({ |
| 68 | + label: s.label, |
| 69 | + raw: new Uint8Array(Buffer.from(s.raw_b64, "base64")), |
| 70 | + })); |
| 71 | +} |
| 72 | + |
| 73 | +const canRun = existsSync(VIM_PACKAGE_BIN) && existsSync(NATIVE_VIM); |
| 74 | + |
| 75 | +describe.skipIf(!canRun)("vim wasm vs native — 1:1 PTY parity", () => { |
| 76 | + let vm: AgentOs | undefined; |
| 77 | + afterEach(async () => { |
| 78 | + await vm?.dispose(); |
| 79 | + vm = undefined; |
| 80 | + }); |
| 81 | + |
| 82 | + it("renders identically to native vim through the same key sequence", async () => { |
| 83 | + const { AgentOs } = await import("../src/index.js"); |
| 84 | + const common = (await import("@agentos-software/common")).default; |
| 85 | + const vimPkg = (await import("@agentos-software/vim")).default; |
| 86 | + |
| 87 | + // Reference: native vim over a real PTY. |
| 88 | + const native = nativeSnaps(`/tmp/${BASENAME}`); |
| 89 | + |
| 90 | + // Under test: wasm vim as a CHILD of the brush shell (the `just shell` |
| 91 | + // path), so this exercises PTY-slave inheritance too. |
| 92 | + vm = await AgentOs.create({ software: [common, vimPkg] }); |
| 93 | + await vm.mkdir("/work", { recursive: true }); |
| 94 | + const { shellId } = vm.openShell({ |
| 95 | + command: "sh", |
| 96 | + args: ["--input-backend", "minimal", "-i"], |
| 97 | + cols: COLS, |
| 98 | + rows: ROWS, |
| 99 | + cwd: "/work", |
| 100 | + env: { TERM: "xterm", LANG: "C.UTF-8", PS1: "$ " }, |
| 101 | + }); |
| 102 | + let cumulative = Buffer.alloc(0); |
| 103 | + let writes = Promise.resolve(); |
| 104 | + vm.onShellData(shellId, (data) => { |
| 105 | + const b = Buffer.from(data); |
| 106 | + cumulative = Buffer.concat([cumulative, b]); |
| 107 | + writes = writes.then(() => {}); |
| 108 | + }); |
| 109 | + const settle = async (ms: number) => { |
| 110 | + await sleep(ms); |
| 111 | + await writes; |
| 112 | + }; |
| 113 | + await settle(3000); |
| 114 | + await vm.writeShell( |
| 115 | + shellId, |
| 116 | + `vim -N -u NONE -i NONE -n -c 'set ruler noshowcmd' /work/${BASENAME}\r`, |
| 117 | + ); |
| 118 | + await settle(2500); |
| 119 | + // Snapshot the region since vim launched (strip the shell prompt + command |
| 120 | + // echo that precede vim's own output). |
| 121 | + const markerIdx = cumulative.lastIndexOf(Buffer.from("\x1b[", "utf8")); |
| 122 | + void markerIdx; |
| 123 | + const vimStart = cumulative.length; |
| 124 | + const wasmSnaps: Snap[] = []; |
| 125 | + // Re-capture from a clean emulator per step: feed all bytes from vim start. |
| 126 | + let base = cumulative.subarray(0, vimStart); |
| 127 | + void base; |
| 128 | + const capture = (label: string) => { |
| 129 | + wasmSnaps.push({ label, raw: new Uint8Array(cumulative) }); |
| 130 | + }; |
| 131 | + capture("open"); |
| 132 | + for (const step of STEPS) { |
| 133 | + await vm.writeShell(shellId, step.keys); |
| 134 | + await settle(step.wait * 1000); |
| 135 | + capture(step.label); |
| 136 | + } |
| 137 | + |
| 138 | + // Drive each side's stream through its own PERSISTENT emulator as |
| 139 | + // incremental deltas (bytes since the previous snapshot), then compare the |
| 140 | + // live grids step by step. Both snapshot lists are cumulative, so the delta |
| 141 | + // is the tail past the previous cumulative length. This mirrors how a real |
| 142 | + // terminal consumes bytes exactly once — full-replaying the cumulative |
| 143 | + // buffer into a fresh emulator would double-draw vim's scrolled redraws. |
| 144 | + const nativeTerm = createTerm(COLS, ROWS); |
| 145 | + const wasmTerm = createTerm(COLS, ROWS); |
| 146 | + let prevN = 0; |
| 147 | + let prevW = 0; |
| 148 | + const report: string[] = []; |
| 149 | + let allEqual = true; |
| 150 | + for (let i = 0; i < native.length; i++) { |
| 151 | + await nativeTerm.write(native[i].raw.subarray(prevN)); |
| 152 | + await wasmTerm.write(wasmSnaps[i].raw.subarray(prevW)); |
| 153 | + prevN = native[i].raw.length; |
| 154 | + prevW = wasmSnaps[i].raw.length; |
| 155 | + const d = diffGrids(native[i].label, nativeTerm.grid(), wasmTerm.grid(), { |
| 156 | + normalizeBasename: BASENAME, |
| 157 | + }); |
| 158 | + report.push(...d.lines); |
| 159 | + if (!d.equal) allEqual = false; |
| 160 | + } |
| 161 | + if (!allEqual) { |
| 162 | + throw new Error(`vim wasm/native parity mismatch:\n${report.join("\n")}`); |
| 163 | + } |
| 164 | + expect(allEqual).toBe(true); |
| 165 | + }, 120_000); |
| 166 | +}); |
0 commit comments