Add import template into tui. Fix tests that fail on macos. Fix some updates.
This commit is contained in:
44
tests/tui/format-dialog-message.test.ts
Normal file
44
tests/tui/format-dialog-message.test.ts
Normal file
@@ -0,0 +1,44 @@
|
||||
import { describe, expect, test } from "vitest";
|
||||
|
||||
import {
|
||||
formatDialogMessageLines,
|
||||
getMessageContentWidth,
|
||||
getMessageDialogWidth,
|
||||
} from "../../src/tui/utils/format-dialog-message.js";
|
||||
|
||||
describe("formatDialogMessageLines", () => {
|
||||
test("drops empty lines from leading newlines", () => {
|
||||
const lines = formatDialogMessageLines("\n- first\n- second", 80);
|
||||
|
||||
expect(lines).toEqual(["- first", "- second"]);
|
||||
});
|
||||
|
||||
test("keeps short lines unchanged", () => {
|
||||
const lines = formatDialogMessageLines("- actions.receive: Invalid", 80);
|
||||
|
||||
expect(lines).toEqual(["- actions.receive: Invalid"]);
|
||||
});
|
||||
|
||||
test("breaks long dot-separated paths at segment boundaries", () => {
|
||||
const line =
|
||||
"- actions.requestFungibleTokens.roles.receiver.requirements: Unrecognized key: \"generate\"";
|
||||
const lines = formatDialogMessageLines(line, 56);
|
||||
|
||||
expect(lines.length).toBeGreaterThan(1);
|
||||
expect(lines.join("\n")).toContain("actions.requestFungibleTokens.");
|
||||
expect(lines.every((entry) => entry.length <= 58)).toBe(true);
|
||||
expect(lines[1]?.startsWith(" ")).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("dialog width helpers", () => {
|
||||
test("getMessageDialogWidth respects terminal bounds", () => {
|
||||
expect(getMessageDialogWidth(120)).toBe(100);
|
||||
expect(getMessageDialogWidth(80)).toBe(76);
|
||||
expect(getMessageDialogWidth(40)).toBe(60);
|
||||
});
|
||||
|
||||
test("getMessageContentWidth subtracts border and padding", () => {
|
||||
expect(getMessageContentWidth(76)).toBe(70);
|
||||
});
|
||||
});
|
||||
114
tests/tui/list-directory-entries.test.ts
Normal file
114
tests/tui/list-directory-entries.test.ts
Normal file
@@ -0,0 +1,114 @@
|
||||
import { afterEach, beforeEach, describe, expect, test } from "vitest";
|
||||
import {
|
||||
existsSync,
|
||||
mkdirSync,
|
||||
mkdtempSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { listDirectoryEntries } from "../../src/tui/utils/list-directory-entries.js";
|
||||
|
||||
describe("listDirectoryEntries", () => {
|
||||
let tempRoot: string;
|
||||
|
||||
beforeEach(() => {
|
||||
tempRoot = mkdtempSync(path.join(tmpdir(), "xo-file-picker-"));
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
if (existsSync(tempRoot)) {
|
||||
rmSync(tempRoot, { recursive: true, force: true });
|
||||
}
|
||||
});
|
||||
|
||||
test("includes parent entry and sorts directories before files", () => {
|
||||
mkdirSync(path.join(tempRoot, "beta-dir"));
|
||||
mkdirSync(path.join(tempRoot, "alpha-dir"));
|
||||
writeFileSync(path.join(tempRoot, "zebra.json"), "{}");
|
||||
writeFileSync(path.join(tempRoot, "apple.txt"), "x");
|
||||
|
||||
const childDir = path.join(tempRoot, "child");
|
||||
mkdirSync(childDir);
|
||||
writeFileSync(path.join(childDir, "nested.json"), "{}");
|
||||
|
||||
const rootResult = listDirectoryEntries(tempRoot);
|
||||
expect(rootResult.error).toBeUndefined();
|
||||
expect(rootResult.entries.map((entry) => entry.name)).toEqual([
|
||||
"..",
|
||||
"alpha-dir",
|
||||
"beta-dir",
|
||||
"child",
|
||||
"apple.txt",
|
||||
"zebra.json",
|
||||
]);
|
||||
|
||||
const childResult = listDirectoryEntries(childDir);
|
||||
expect(childResult.entries[0]).toMatchObject({
|
||||
name: "..",
|
||||
kind: "parent",
|
||||
absolutePath: tempRoot,
|
||||
});
|
||||
expect(childResult.entries.slice(1).map((entry) => entry.name)).toEqual([
|
||||
"nested.json",
|
||||
]);
|
||||
});
|
||||
|
||||
test("filters files by extension when extensions are provided", () => {
|
||||
writeFileSync(path.join(tempRoot, "template.json"), "{}");
|
||||
writeFileSync(path.join(tempRoot, "readme.md"), "# hi");
|
||||
writeFileSync(path.join(tempRoot, "UPPER.JSON"), "{}");
|
||||
|
||||
const result = listDirectoryEntries(tempRoot, { extensions: ["json"] });
|
||||
|
||||
expect(result.error).toBeUndefined();
|
||||
expect(result.entries.map((entry) => entry.name)).toEqual([
|
||||
"..",
|
||||
"template.json",
|
||||
"UPPER.JSON",
|
||||
]);
|
||||
});
|
||||
|
||||
test("shows all files when extensions are omitted", () => {
|
||||
writeFileSync(path.join(tempRoot, "a.json"), "{}");
|
||||
writeFileSync(path.join(tempRoot, "b.txt"), "x");
|
||||
|
||||
const result = listDirectoryEntries(tempRoot);
|
||||
|
||||
expect(result.entries.map((entry) => entry.name)).toEqual([
|
||||
"..",
|
||||
"a.json",
|
||||
"b.txt",
|
||||
]);
|
||||
});
|
||||
|
||||
test("omits parent entry at filesystem root", () => {
|
||||
const rootResult = listDirectoryEntries(path.parse(tempRoot).root);
|
||||
|
||||
expect(rootResult.error).toBeUndefined();
|
||||
expect(rootResult.entries.some((entry) => entry.kind === "parent")).toBe(
|
||||
false,
|
||||
);
|
||||
});
|
||||
|
||||
test("returns error for missing directory without throwing", () => {
|
||||
const missingPath = path.join(tempRoot, "does-not-exist");
|
||||
|
||||
const result = listDirectoryEntries(missingPath);
|
||||
|
||||
expect(result.entries).toEqual([]);
|
||||
expect(result.error).toContain("Directory does not exist");
|
||||
});
|
||||
|
||||
test("returns error when path is a file", () => {
|
||||
const filePath = path.join(tempRoot, "file.txt");
|
||||
writeFileSync(filePath, "hello");
|
||||
|
||||
const result = listDirectoryEntries(filePath);
|
||||
|
||||
expect(result.entries).toEqual([]);
|
||||
expect(result.error).toContain("Not a directory");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user