85 lines
2.9 KiB
TypeScript
85 lines
2.9 KiB
TypeScript
import assert from "node:assert";
|
|
import { describe, it } from "node:test";
|
|
import { isNativeModifierPressed } from "../src/native-modifiers.ts";
|
|
import { normalizeAppleTerminalInput, ProcessTerminal } from "../src/terminal.ts";
|
|
|
|
function withEnv(name: string, value: string | undefined, fn: () => void): void {
|
|
const previous = process.env[name];
|
|
if (value === undefined) delete process.env[name];
|
|
else process.env[name] = value;
|
|
try {
|
|
fn();
|
|
} finally {
|
|
if (previous === undefined) delete process.env[name];
|
|
else process.env[name] = previous;
|
|
}
|
|
}
|
|
|
|
describe("normalizeAppleTerminalInput", () => {
|
|
it("rewrites Apple Terminal Return to CSI-u Shift+Enter when Shift is pressed", () => {
|
|
assert.equal(normalizeAppleTerminalInput("\r", true, true), "\x1b[13;2u");
|
|
});
|
|
|
|
it("leaves Apple Terminal Return unchanged when Shift is not pressed", () => {
|
|
assert.equal(normalizeAppleTerminalInput("\r", true, false), "\r");
|
|
});
|
|
|
|
it("leaves non-Apple Terminal Return unchanged when Shift is pressed", () => {
|
|
assert.equal(normalizeAppleTerminalInput("\r", false, true), "\r");
|
|
});
|
|
|
|
it("leaves non-Return input unchanged", () => {
|
|
assert.equal(normalizeAppleTerminalInput("\x1b[13;2u", true, true), "\x1b[13;2u");
|
|
assert.equal(normalizeAppleTerminalInput("a", true, true), "a");
|
|
});
|
|
|
|
it("treats native helper failure as Shift not pressed", () => {
|
|
withEnv("PI_TUI_DISABLE_NATIVE_MODIFIERS", "1", () => {
|
|
assert.equal(isNativeModifierPressed("shift"), false);
|
|
assert.equal(normalizeAppleTerminalInput("\r", true, isNativeModifierPressed("shift")), "\r");
|
|
});
|
|
});
|
|
});
|
|
|
|
describe("ProcessTerminal dimensions", () => {
|
|
it("falls back to COLUMNS and LINES before default dimensions", () => {
|
|
const previousColumnsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "columns");
|
|
const previousRowsDescriptor = Object.getOwnPropertyDescriptor(process.stdout, "rows");
|
|
const previousColumns = process.env.COLUMNS;
|
|
const previousLines = process.env.LINES;
|
|
|
|
try {
|
|
Object.defineProperty(process.stdout, "columns", { value: undefined, configurable: true });
|
|
Object.defineProperty(process.stdout, "rows", { value: undefined, configurable: true });
|
|
process.env.COLUMNS = "123";
|
|
process.env.LINES = "45";
|
|
|
|
const terminal = new ProcessTerminal();
|
|
|
|
assert.equal(terminal.columns, 123);
|
|
assert.equal(terminal.rows, 45);
|
|
} finally {
|
|
if (previousColumnsDescriptor) {
|
|
Object.defineProperty(process.stdout, "columns", previousColumnsDescriptor);
|
|
} else {
|
|
Reflect.deleteProperty(process.stdout, "columns");
|
|
}
|
|
if (previousRowsDescriptor) {
|
|
Object.defineProperty(process.stdout, "rows", previousRowsDescriptor);
|
|
} else {
|
|
Reflect.deleteProperty(process.stdout, "rows");
|
|
}
|
|
if (previousColumns === undefined) {
|
|
delete process.env.COLUMNS;
|
|
} else {
|
|
process.env.COLUMNS = previousColumns;
|
|
}
|
|
if (previousLines === undefined) {
|
|
delete process.env.LINES;
|
|
} else {
|
|
process.env.LINES = previousLines;
|
|
}
|
|
}
|
|
});
|
|
});
|