import assert from "node:assert/strict"; import { test } from "node:test"; import { initialLine, step, type LineState } from "../src/terminal/line-editor.ts"; const type = (keys: string[], state: LineState = initialLine()) => keys.reduce((s, key) => step(s, key).state, state); test("typing, moving, and deleting edit the line", () => { const state = type(["e", "c", "o", "\x1b[D", "h", "\x1b[F", "!", "\x7f"]); assert.deepEqual([state.buffer, state.cursor], ["echo", 4]); assert.equal(type(["\x17"], state).buffer, ""); }); test("Enter submits and records the line; Up and Down walk the history and restore the draft", () => { const first = step(type(["l", "s"]), "\r"); assert.equal(first.submit, "ls"); const drafted = type(["p", "w"], first.state); const up = step(drafted, "\x1b[A"); assert.equal(up.echo, "ls"); assert.equal(step(up.state, "\x1b[B").echo, "pw"); }); test("a paste with line breaks submits all but its last line, which stays on the prompt", () => { const result = step(initialLine(), "echo a\recho b\rec"); assert.equal(result.submit, "echo a\necho b"); assert.equal(result.echo, "ec"); }); test("^C abandons the line and ^L asks for a clear screen", () => { assert.equal(step(type(["x"]), "\x03").interrupt, true); assert.equal(step(initialLine(), "\x0c").clear, true); });