-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.test.ts
More file actions
250 lines (206 loc) · 7.59 KB
/
Copy pathutils.test.ts
File metadata and controls
250 lines (206 loc) · 7.59 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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* Tests for src/server/utils.ts
*
* Most functions in utils.ts are purely computational with no GAS dependencies.
* getAllFilesRecursive accepts a GAS Folder type but is testable via duck-typed
* fake iterators — no globalThis mocking required.
*/
import {
extractId,
isValidDriveLink,
createSeededRandom,
getAllFilesRecursive,
sampleRows,
truncateText,
flattenArg,
resolveColumns,
} from "../src/server/utils";
import type { DriveFileInfo } from "../src/shared/types";
describe("extractId", () => {
it("extracts ID from a standard Drive file URL", () => {
const url = "https://drive.google.com/file/d/1AbCdEfGhIjKlMnOpQrStUvWxYz012345/view";
expect(extractId(url)).toBe("1AbCdEfGhIjKlMnOpQrStUvWxYz012345");
});
it("extracts ID from a folder URL", () => {
const url = "https://drive.google.com/drive/folders/1AbCdEfGhIjKlMnOpQrStUvWxYz012345";
expect(extractId(url)).toBe("1AbCdEfGhIjKlMnOpQrStUvWxYz012345");
});
it("returns the input if it already looks like an ID", () => {
const id = "1AbCdEfGhIjKlMnOpQrStUvWxYz012345";
expect(extractId(id)).toBe(id);
});
it("returns empty string for null/undefined/non-string", () => {
expect(extractId(null)).toBe("");
expect(extractId(undefined)).toBe("");
expect(extractId(123)).toBe("");
});
it("returns the raw input for short strings with no match", () => {
expect(extractId("short")).toBe("short");
});
});
describe("isValidDriveLink", () => {
it("returns true for drive.google.com URLs", () => {
expect(isValidDriveLink("https://drive.google.com/file/d/abc123/view")).toBe(true);
});
it("returns true for URLs containing /d/", () => {
expect(isValidDriveLink("https://docs.google.com/document/d/abc123/edit")).toBe(true);
});
it("returns false for non-Drive URLs", () => {
expect(isValidDriveLink("https://example.com/file.pdf")).toBe(false);
});
it("returns false for non-string inputs", () => {
expect(isValidDriveLink(null)).toBe(false);
expect(isValidDriveLink(42)).toBe(false);
expect(isValidDriveLink(undefined)).toBe(false);
});
});
describe("createSeededRandom", () => {
it("produces deterministic output for a given seed", () => {
const rng1 = createSeededRandom(42);
const rng2 = createSeededRandom(42);
const seq1 = Array.from({ length: 10 }, () => rng1());
const seq2 = Array.from({ length: 10 }, () => rng2());
expect(seq1).toEqual(seq2);
});
it("produces different output for different seeds", () => {
const rng1 = createSeededRandom(42);
const rng2 = createSeededRandom(99);
expect(rng1()).not.toEqual(rng2());
});
it("produces values in [0, 1)", () => {
const rng = createSeededRandom(123);
for (let i = 0; i < 100; i++) {
const val = rng();
expect(val).toBeGreaterThanOrEqual(0);
expect(val).toBeLessThan(1);
}
});
it("produces values in [0, 1) when called without a seed", () => {
const rng = createSeededRandom();
const val = rng();
expect(val).toBeGreaterThanOrEqual(0);
expect(val).toBeLessThan(1);
});
});
describe("sampleRows", () => {
const data = [["a"], ["b"], ["c"], ["d"], ["e"]];
it("returns the correct number of rows", () => {
expect(sampleRows(data, 3, 42)).toHaveLength(3);
});
it("produces reproducible output for the same seed", () => {
const first = sampleRows(data, 3, 42);
const second = sampleRows(data, 3, 42);
expect(first).toEqual(second);
});
it("produces different output for different seeds", () => {
const first = sampleRows(data, 3, 42);
const second = sampleRows(data, 3, 99);
expect(first).not.toEqual(second);
});
it("returns all rows when sampleSize equals data length", () => {
const result = sampleRows(data, 5, 42);
expect(result).toHaveLength(5);
expect(result).toEqual(expect.arrayContaining(data));
});
});
describe("truncateText", () => {
it("returns short text unchanged", () => {
expect(truncateText("hello", 100)).toBe("hello");
});
it("returns text at exact limit unchanged", () => {
const text = "a".repeat(100);
expect(truncateText(text, 100)).toBe(text);
});
it("truncates text over the limit and appends suffix", () => {
const text = "a".repeat(101);
const result = truncateText(text, 100);
expect(result).toBe("a".repeat(100) + "... [TRUNCATED]");
});
});
describe("getAllFilesRecursive", () => {
function makeFileIterator(urls: string[]) {
let i = 0;
return { hasNext: () => i < urls.length, next: () => ({ getUrl: () => urls[i++] }) };
}
function makeFolderIterator(folders: object[]) {
let i = 0;
return { hasNext: () => i < folders.length, next: () => folders[i++] };
}
function makeFolder(urls: string[], subfolders: object[] = []) {
return {
getFiles: () => makeFileIterator(urls),
getFolders: () => makeFolderIterator(subfolders),
};
}
it("collects file URLs from a flat folder", () => {
const folder = makeFolder([
"https://drive.google.com/file/d/abc",
"https://drive.google.com/file/d/def",
]);
const result: DriveFileInfo[] = [];
getAllFilesRecursive(folder as any, result);
expect(result).toEqual([
{ url: "https://drive.google.com/file/d/abc" },
{ url: "https://drive.google.com/file/d/def" },
]);
});
it("recurses into subfolders", () => {
const subfolder = makeFolder(["https://drive.google.com/file/d/xyz"]);
const rootFolder = makeFolder(["https://drive.google.com/file/d/abc"], [subfolder]);
const result: DriveFileInfo[] = [];
getAllFilesRecursive(rootFolder as any, result);
expect(result).toEqual([
{ url: "https://drive.google.com/file/d/abc" },
{ url: "https://drive.google.com/file/d/xyz" },
]);
});
it("returns an empty list for an empty folder", () => {
const folder = makeFolder([]);
const result: DriveFileInfo[] = [];
getAllFilesRecursive(folder as any, result);
expect(result).toHaveLength(0);
});
});
describe("flattenArg", () => {
it("wraps a scalar string in an array", () => {
expect(flattenArg("hello")).toEqual(["hello"]);
});
it("flattens a vertical range (multiple rows, one column)", () => {
expect(flattenArg([["row1"], ["row2"], ["row3"]])).toEqual(["row1", "row2", "row3"]);
});
it("flattens a horizontal range (one row, multiple columns)", () => {
expect(flattenArg([["col1", "col2", "col3"]])).toEqual(["col1", "col2", "col3"]);
});
it("filters empty strings from ranges", () => {
expect(flattenArg([["text", "", "more"]])).toEqual(["text", "more"]);
});
it("filters null values from ranges", () => {
expect(flattenArg([["a", null, "b"]])).toEqual(["a", "b"]);
});
it("returns an empty array for null input", () => {
expect(flattenArg(null)).toEqual([]);
});
it("returns an empty array for an empty string scalar", () => {
expect(flattenArg("")).toEqual([]);
});
it("converts non-string scalars to strings", () => {
expect(flattenArg(42)).toEqual(["42"]);
});
});
describe("resolveColumns", () => {
it("returns indices for all found names", () => {
expect(resolveColumns(["a", "b", "c"], ["a", "c"])).toEqual([0, 2]);
});
it("returns -1 for names not in headers", () => {
expect(resolveColumns(["a", "b"], ["c"])).toEqual([-1]);
});
it("returns empty array for empty names list", () => {
expect(resolveColumns(["a", "b"], [])).toEqual([]);
});
it("returns -1 for all names when headers is empty", () => {
expect(resolveColumns([], ["a"])).toEqual([-1]);
});
it("preserves the order of the names argument", () => {
expect(resolveColumns(["x", "y", "z"], ["z", "x"])).toEqual([2, 0]);
});
});