-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile-pane-table-columns.test.ts
More file actions
123 lines (111 loc) · 4.58 KB
/
Copy pathfile-pane-table-columns.test.ts
File metadata and controls
123 lines (111 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { describe, expect, it } from "vitest";
import {
FILE_PANE_DEFAULT_VISIBILITY,
filePaneNextSortState,
filePaneSortRows,
filePaneVisibleDataColumns,
filePaneVisibleResizableKeysFromDisplayOrder,
normalizeFilePaneColumnOrder,
type FilePaneDirRow,
type FilePaneSortState,
} from "./file-pane-table-columns";
function row(partial: Partial<FilePaneDirRow> & Pick<FilePaneDirRow, "name" | "isDir">): FilePaneDirRow {
return {
size: 0,
mtime: null,
modeDisplay: "",
modeOctal: "",
userDisplay: "",
groupDisplay: "",
...partial,
sortWithDirectories: partial.sortWithDirectories ?? partial.isDir,
};
}
describe("filePaneSortRows", () => {
it("lists directories before files regardless of sort column", () => {
const rows: FilePaneDirRow[] = [
row({ name: "a.txt", isDir: false, size: 1 }),
row({ name: "z_dir", isDir: true }),
];
const sort: FilePaneSortState = { column: "name", direction: "asc" };
const out = filePaneSortRows(rows, sort);
expect(out.map((r) => r.name)).toEqual(["z_dir", "a.txt"]);
});
it("sorts by name ascending among same kind", () => {
const rows: FilePaneDirRow[] = [
row({ name: "b", isDir: false }),
row({ name: "a", isDir: false }),
];
const out = filePaneSortRows(rows, { column: "name", direction: "asc" });
expect(out.map((r) => r.name)).toEqual(["a", "b"]);
});
it("sorts by size descending with stable name tie-break", () => {
const rows: FilePaneDirRow[] = [
row({ name: "small", isDir: false, size: 10 }),
row({ name: "big", isDir: false, size: 100 }),
row({ name: "mid", isDir: false, size: 50 }),
];
const out = filePaneSortRows(rows, { column: "size", direction: "desc" });
expect(out.map((r) => r.name)).toEqual(["big", "mid", "small"]);
});
it("treats directory size as zero for size sort", () => {
const rows: FilePaneDirRow[] = [
row({ name: "huge", isDir: false, size: 999 }),
row({ name: "dir", isDir: true, size: 1 }),
];
const out = filePaneSortRows(rows, { column: "size", direction: "asc" });
expect(out[0]!.isDir).toBe(true);
expect(out[1]!.name).toBe("huge");
});
it("sorts by kind label among files", () => {
const rows: FilePaneDirRow[] = [
row({ name: "lnk", isDir: false, modeDisplay: "lrwxrwxrwx" }),
row({ name: "txt", isDir: false, modeDisplay: "-rw-r--r--" }),
];
const out = filePaneSortRows(rows, { column: "kind", direction: "asc" });
expect(out.map((r) => r.name)).toEqual(["txt", "lnk"]);
});
it("groups symlink-to-directory with directories when sortWithDirectories is true", () => {
const rows: FilePaneDirRow[] = [
row({ name: "a.txt", isDir: false, sortWithDirectories: false }),
row({ name: "zebra_link", isDir: false, sortWithDirectories: true, modeDisplay: "lrwxrwxrwx" }),
row({ name: "realdir", isDir: true, sortWithDirectories: true }),
];
const out = filePaneSortRows(rows, { column: "name", direction: "asc" });
expect(out.map((r) => r.name)).toEqual(["realdir", "zebra_link", "a.txt"]);
});
});
describe("filePaneNextSortState", () => {
it("starts ascending when switching columns", () => {
const next = filePaneNextSortState({ column: "name", direction: "desc" }, "size");
expect(next).toEqual({ column: "size", direction: "asc" });
});
it("toggles direction when clicking the same column", () => {
const next = filePaneNextSortState({ column: "name", direction: "asc" }, "name");
expect(next).toEqual({ column: "name", direction: "desc" });
});
});
describe("normalizeFilePaneColumnOrder", () => {
it("dedupes and appends missing columns", () => {
const out = normalizeFilePaneColumnOrder(["name", "size", "name", "group"]);
expect(out).toContain("name");
expect(out).toContain("size");
expect(out.indexOf("name")).toBeLessThan(out.indexOf("size"));
expect(out.length).toBe(8);
});
});
describe("filePaneVisibleDataColumns with column order", () => {
it("follows custom order and visibility", () => {
const vis = { ...FILE_PANE_DEFAULT_VISIBILITY, permissions: true };
const order = normalizeFilePaneColumnOrder(["modified", "name", "size", "permissions"]);
const cols = filePaneVisibleDataColumns(vis, order);
expect(cols[0]).toBe("modified");
expect(cols[1]).toBe("name");
});
});
describe("filePaneVisibleResizableKeysFromDisplayOrder", () => {
it("matches left-to-right visible resizable columns", () => {
const keys = filePaneVisibleResizableKeysFromDisplayOrder(["name", "octal", "size", "modified"]);
expect(keys).toEqual(["name", "size"]);
});
});