@tenonhq/dovetail-core 0.0.132 → 0.0.134

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,459 @@
1
+ "use strict";
2
+ /**
3
+ * `dove refresh` writes one folder per record, and says so when it cannot.
4
+ *
5
+ * Regression targets, each taken from a clean resync of a real instance:
6
+ *
7
+ * - LETTER-CASE TWINS. The instance lists "Search default" and "Search
8
+ * Default" as two records. Both were written to one folder on macOS and
9
+ * Windows, and the record written last won.
10
+ * - NAME TWINS. Two access controls with one name and the same first 8 id
11
+ * characters. Once the instance lists both, both need a folder.
12
+ * - THE DOWNLOAD NAMES A RECORD DIFFERENTLY. bulkDownload names records from
13
+ * the records in that one request, so a namesake pulled alone came back
14
+ * under the bare name and was written to a second folder. The manifest is
15
+ * the one naming authority: the download's own names never pick a folder.
16
+ * - A PATH GIT IGNORES. A client script named `console.log` lands in a folder
17
+ * a `*.log` ignore rule hides, so it never reached the repository.
18
+ * - A FOLDER THAT CANNOT BE CREATED took the whole application down with it.
19
+ *
20
+ * These run against the real file system (and a real `git init` for the ignore
21
+ * case) so they hold on case-insensitive and case-sensitive disks alike.
22
+ */
23
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
24
+ if (k2 === undefined) k2 = k;
25
+ var desc = Object.getOwnPropertyDescriptor(m, k);
26
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
27
+ desc = { enumerable: true, get: function() { return m[k]; } };
28
+ }
29
+ Object.defineProperty(o, k2, desc);
30
+ }) : (function(o, m, k, k2) {
31
+ if (k2 === undefined) k2 = k;
32
+ o[k2] = m[k];
33
+ }));
34
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
35
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
36
+ }) : function(o, v) {
37
+ o["default"] = v;
38
+ });
39
+ var __importStar = (this && this.__importStar) || (function () {
40
+ var ownKeys = function(o) {
41
+ ownKeys = Object.getOwnPropertyNames || function (o) {
42
+ var ar = [];
43
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
44
+ return ar;
45
+ };
46
+ return ownKeys(o);
47
+ };
48
+ return function (mod) {
49
+ if (mod && mod.__esModule) return mod;
50
+ var result = {};
51
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
52
+ __setModuleDefault(result, mod);
53
+ return result;
54
+ };
55
+ })();
56
+ Object.defineProperty(exports, "__esModule", { value: true });
57
+ const cp = __importStar(require("child_process"));
58
+ const fs = __importStar(require("fs"));
59
+ const os = __importStar(require("os"));
60
+ const path = __importStar(require("path"));
61
+ // ---------- mocks (must come before importing the module under test) ----------
62
+ var mockLogger = {
63
+ info: jest.fn(),
64
+ debug: jest.fn(),
65
+ warn: jest.fn(),
66
+ error: jest.fn(),
67
+ success: jest.fn(),
68
+ setLogLevel: jest.fn(),
69
+ getLogLevel: function () { return "warn"; },
70
+ };
71
+ var mockClient = {
72
+ getManifest: jest.fn(),
73
+ getMissingFiles: jest.fn(),
74
+ };
75
+ jest.mock("../Logger", function () { return { logger: mockLogger }; });
76
+ jest.mock("../FileLogger", function () {
77
+ return { fileLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() } };
78
+ });
79
+ jest.mock("../snClient", function () {
80
+ return {
81
+ defaultClient: function () { return mockClient; },
82
+ unwrapSNResponse: function (p) { return Promise.resolve(p); },
83
+ processPushResponse: jest.fn(),
84
+ retryOnErr: jest.fn(),
85
+ retryOnHttpErr: jest.fn(),
86
+ unwrapTableAPIFirstItem: jest.fn(),
87
+ };
88
+ });
89
+ var mockConfig = {
90
+ getConfig: jest.fn().mockReturnValue({ scopes: { x_cadso_work: {} }, tableOptions: {} }),
91
+ getManifest: jest.fn().mockResolvedValue({ x_cadso_work: { scope: "x_cadso_work", tables: {} } }),
92
+ getSourcePathForScope: jest.fn(),
93
+ getSourcePath: jest.fn(),
94
+ getManifestPath: jest.fn().mockReturnValue("/tmp/dove.manifest.json"),
95
+ resolveConfigForScope: jest.fn().mockImplementation(function () {
96
+ return {
97
+ tables: ["sys_ux_screen", "sys_security_acl", "sys_hub_action_input", "sys_ux_client_script"],
98
+ fieldOverrides: {},
99
+ apiIncludes: {},
100
+ apiExcludes: {},
101
+ };
102
+ }),
103
+ isMultiScopeManifest: jest.fn().mockReturnValue(true),
104
+ updateManifest: jest.fn(),
105
+ };
106
+ jest.mock("../config", function () { return mockConfig; });
107
+ jest.mock("progress", function () {
108
+ return jest.fn().mockImplementation(function () { return { tick: jest.fn() }; });
109
+ });
110
+ // Real FileUtils, so the assertions read the actual disk. Only the scope
111
+ // manifest write is captured instead of written.
112
+ var mockWriteScopeManifest = jest.fn().mockResolvedValue(undefined);
113
+ jest.mock("../FileUtils", function () {
114
+ var actual = jest.requireActual("../FileUtils");
115
+ return Object.assign({}, actual, { writeScopeManifest: mockWriteScopeManifest });
116
+ });
117
+ const AppUtils = __importStar(require("../appUtils"));
118
+ const commands_1 = require("../commands");
119
+ var tmpRoot;
120
+ function serverManifest(rows) {
121
+ var tables = {};
122
+ rows.forEach(function (r) {
123
+ if (!tables[r.table])
124
+ tables[r.table] = { records: {} };
125
+ tables[r.table].records[r.name] = {
126
+ name: r.name,
127
+ sys_id: r.sys_id,
128
+ files: [{ name: "script", type: "js" }],
129
+ };
130
+ });
131
+ return { scope: "x_cadso_work", tables: tables };
132
+ }
133
+ function serverDownload(rows) {
134
+ var tables = {};
135
+ rows.forEach(function (r) {
136
+ var name = r.downloadName === undefined ? r.name : r.downloadName;
137
+ if (!tables[r.table])
138
+ tables[r.table] = { records: {} };
139
+ tables[r.table].records[name] = {
140
+ name: name,
141
+ sys_id: r.sys_id,
142
+ files: [{ name: "script", type: "js", content: r.script }],
143
+ };
144
+ });
145
+ return tables;
146
+ }
147
+ function serve(rows) {
148
+ mockClient.getManifest.mockResolvedValue(serverManifest(rows));
149
+ mockClient.getMissingFiles.mockResolvedValue(serverDownload(rows));
150
+ }
151
+ /** Folder names on disk, exactly as the disk spells them. */
152
+ function foldersOf(table) {
153
+ try {
154
+ return fs.readdirSync(path.join(tmpRoot, table)).sort();
155
+ }
156
+ catch (e) {
157
+ return [];
158
+ }
159
+ }
160
+ function scriptIn(table, folder) {
161
+ return fs.readFileSync(path.join(tmpRoot, table, folder, "script.js"), "utf8");
162
+ }
163
+ function writtenManifest() {
164
+ expect(mockWriteScopeManifest).toHaveBeenCalled();
165
+ return mockWriteScopeManifest.mock.calls[0][1];
166
+ }
167
+ function manifestKeys(table) {
168
+ return Object.keys(writtenManifest().tables[table].records).sort();
169
+ }
170
+ function git(args) {
171
+ cp.execFileSync("git", args, { cwd: tmpRoot, stdio: "ignore" });
172
+ }
173
+ beforeEach(function () {
174
+ jest.clearAllMocks();
175
+ // realpath: os.tmpdir() is a symlink on macOS, and git reports real paths.
176
+ tmpRoot = fs.realpathSync(fs.mkdtempSync(path.join(os.tmpdir(), "dove-collisions-")));
177
+ mockConfig.getSourcePathForScope.mockReturnValue(tmpRoot);
178
+ mockConfig.getSourcePath.mockReturnValue(tmpRoot);
179
+ });
180
+ afterEach(function () {
181
+ try {
182
+ fs.rmSync(tmpRoot, { recursive: true, force: true });
183
+ }
184
+ catch (e) { }
185
+ });
186
+ // ---------- letter-case twins ----------
187
+ describe("refresh: names that differ only in letter case", function () {
188
+ var rows = [
189
+ { table: "sys_ux_screen", name: "Search default", sys_id: "7daaf59947d56910fc4c1ae8036d43c0", script: "// lower" },
190
+ { table: "sys_ux_screen", name: "Search Default", sys_id: "fb55e60db195a5101b815df7b6dd8425", script: "// upper" },
191
+ { table: "sys_ux_screen", name: "Home", sys_id: "0a1b2c3d47d56910fc4c1ae8036d43c0", script: "// home" },
192
+ ];
193
+ test("each record gets its own folder, on any disk", async function () {
194
+ serve(rows);
195
+ var result = await AppUtils.syncManifest("x_cadso_work");
196
+ expect(result.failedScopes).toEqual([]);
197
+ expect(foldersOf("sys_ux_screen")).toEqual([
198
+ "Home",
199
+ "Search Default (fb55e60d)",
200
+ "Search default (7daaf599)",
201
+ ]);
202
+ // Neither record's content was overwritten by its twin.
203
+ expect(scriptIn("sys_ux_screen", "Search default (7daaf599)")).toBe("// lower");
204
+ expect(scriptIn("sys_ux_screen", "Search Default (fb55e60d)")).toBe("// upper");
205
+ });
206
+ test("the manifest lists every record under the folder it was written to", async function () {
207
+ serve(rows);
208
+ await AppUtils.syncManifest("x_cadso_work");
209
+ expect(manifestKeys("sys_ux_screen")).toEqual(foldersOf("sys_ux_screen"));
210
+ var records = writtenManifest().tables.sys_ux_screen.records;
211
+ expect(records["Search default (7daaf599)"].sys_id).toBe("7daaf59947d56910fc4c1ae8036d43c0");
212
+ expect(records["Search Default (fb55e60d)"].sys_id).toBe("fb55e60db195a5101b815df7b6dd8425");
213
+ });
214
+ test("a record whose name is unique ignoring case is not renamed", async function () {
215
+ serve(rows);
216
+ await AppUtils.syncManifest("x_cadso_work");
217
+ expect(foldersOf("sys_ux_screen")).toContain("Home");
218
+ });
219
+ });
220
+ // ---------- name twins ----------
221
+ describe("refresh: same name and same first 8 id characters", function () {
222
+ var A = "15f0e0ea33dfc7107b18bc534d5c7bc4";
223
+ var B = "15f0e0ea33dfc7107b18bc534d5c7bf5";
224
+ test("both twins are written and listed once the instance sends both", async function () {
225
+ // An instance that no longer collapses the pair sends two entries.
226
+ serve([
227
+ { table: "sys_security_acl", name: "x_cadso_journey_fire_log (" + A + ")", sys_id: A, script: "// read" },
228
+ { table: "sys_security_acl", name: "x_cadso_journey_fire_log (" + B + ")", sys_id: B, script: "// write" },
229
+ ]);
230
+ await AppUtils.syncManifest("x_cadso_work");
231
+ var folders = foldersOf("sys_security_acl");
232
+ expect(folders).toEqual([
233
+ "x_cadso_journey_fire_log (" + A + ")",
234
+ "x_cadso_journey_fire_log (" + B + ")",
235
+ ]);
236
+ expect(manifestKeys("sys_security_acl")).toEqual(folders);
237
+ expect(scriptIn("sys_security_acl", folders[0])).toBe("// read");
238
+ expect(scriptIn("sys_security_acl", folders[1])).toBe("// write");
239
+ });
240
+ test("a download that names both twins alike still writes two folders", async function () {
241
+ // The manifest tells them apart; the download repeats one 8-character name.
242
+ mockClient.getManifest.mockResolvedValue(serverManifest([
243
+ { table: "sys_security_acl", name: "x_cadso_journey_fire_log (" + A + ")", sys_id: A, script: "" },
244
+ { table: "sys_security_acl", name: "x_cadso_journey_fire_log (" + B + ")", sys_id: B, script: "" },
245
+ ]));
246
+ mockClient.getMissingFiles.mockResolvedValue({
247
+ sys_security_acl: {
248
+ records: {
249
+ first: { name: "x_cadso_journey_fire_log (15f0e0ea)", sys_id: A, files: [{ name: "script", type: "js", content: "// read" }] },
250
+ second: { name: "x_cadso_journey_fire_log (15f0e0ea)", sys_id: B, files: [{ name: "script", type: "js", content: "// write" }] },
251
+ },
252
+ },
253
+ });
254
+ await AppUtils.syncManifest("x_cadso_work");
255
+ expect(foldersOf("sys_security_acl")).toEqual([
256
+ "x_cadso_journey_fire_log (" + A + ")",
257
+ "x_cadso_journey_fire_log (" + B + ")",
258
+ ]);
259
+ expect(scriptIn("sys_security_acl", "x_cadso_journey_fire_log (" + A + ")")).toBe("// read");
260
+ expect(scriptIn("sys_security_acl", "x_cadso_journey_fire_log (" + B + ")")).toBe("// write");
261
+ });
262
+ });
263
+ // ---------- the manifest names the folder, not the download ----------
264
+ describe("refresh: the download's own names never pick the folder", function () {
265
+ test("a namesake the download returns under the bare name lands in its manifest folder", async function () {
266
+ serve([
267
+ {
268
+ table: "sys_hub_action_input",
269
+ name: "Audience (0616384a)",
270
+ sys_id: "0616384ac3f492d085b196c4e40131aa",
271
+ script: "// a",
272
+ downloadName: "Audience",
273
+ },
274
+ {
275
+ table: "sys_hub_action_input",
276
+ name: "Audience (92953c0a)",
277
+ sys_id: "92953c0ac3f492d085b196c4e40131bb",
278
+ script: "// b",
279
+ downloadName: "Audience (92953c0a)",
280
+ },
281
+ ]);
282
+ await AppUtils.syncManifest("x_cadso_work");
283
+ // No second, bare "Audience" folder for a record the manifest files elsewhere.
284
+ expect(foldersOf("sys_hub_action_input")).toEqual(["Audience (0616384a)", "Audience (92953c0a)"]);
285
+ expect(scriptIn("sys_hub_action_input", "Audience (0616384a)")).toBe("// a");
286
+ });
287
+ });
288
+ // ---------- names the project configured ----------
289
+ describe("refresh: an id the configuration put in the name is kept", function () {
290
+ var HOME = "0a1b2c3d47d56910fc4c1ae8036d43c0";
291
+ afterEach(function () {
292
+ mockConfig.getConfig.mockReturnValue({ scopes: { x_cadso_work: {} }, tableOptions: {} });
293
+ });
294
+ test("differentiatorField: \"sys_id\" keeps `Name (sys_id)` as the folder", async function () {
295
+ mockConfig.getConfig.mockReturnValue({
296
+ scopes: { x_cadso_work: {} },
297
+ tableOptions: { sys_ux_screen: { differentiatorField: "sys_id" } },
298
+ });
299
+ serve([{ table: "sys_ux_screen", name: "Home (" + HOME + ")", sys_id: HOME, script: "// home" }]);
300
+ await AppUtils.syncManifest("x_cadso_work");
301
+ expect(foldersOf("sys_ux_screen")).toEqual(["Home (" + HOME + ")"]);
302
+ expect(manifestKeys("sys_ux_screen")).toEqual(["Home (" + HOME + ")"]);
303
+ });
304
+ test("a nameTemplate table is named exactly as the template says", async function () {
305
+ mockConfig.getConfig.mockReturnValue({
306
+ scopes: { x_cadso_work: {} },
307
+ tableOptions: { sys_ux_screen: { nameTemplate: "{name} ({sys_id})" } },
308
+ });
309
+ serve([{ table: "sys_ux_screen", name: "Home (" + HOME + ")", sys_id: HOME, script: "// home" }]);
310
+ await AppUtils.syncManifest("x_cadso_work");
311
+ expect(foldersOf("sys_ux_screen")).toEqual(["Home (" + HOME + ")"]);
312
+ });
313
+ test("a table with no such option still has the instance's suffix recomputed", async function () {
314
+ serve([{ table: "sys_ux_screen", name: "Home (" + HOME.substring(0, 8) + ")", sys_id: HOME, script: "// home" }]);
315
+ await AppUtils.syncManifest("x_cadso_work");
316
+ // The only record of that name: no namesake, so no suffix.
317
+ expect(foldersOf("sys_ux_screen")).toEqual(["Home"]);
318
+ });
319
+ });
320
+ // ---------- paths git ignores ----------
321
+ describe("refresh: a folder the repository ignores is reported, not lost", function () {
322
+ var rows = [
323
+ { table: "sys_ux_client_script", name: "console.log", sys_id: "c0ffee0047d56910fc4c1ae8036d43c0", script: "// hidden" },
324
+ { table: "sys_ux_client_script", name: "onLoad", sys_id: "0d15ea5e47d56910fc4c1ae8036d43c0", script: "// seen" },
325
+ ];
326
+ beforeEach(function () {
327
+ git(["init", "-q"]);
328
+ fs.writeFileSync(path.join(tmpRoot, ".gitignore"), "# build output\n*.log\n");
329
+ });
330
+ test("the ignored record is named, with the rule that hides it", async function () {
331
+ serve(rows);
332
+ var result = await AppUtils.syncManifest("x_cadso_work");
333
+ expect(result.notWritten).toHaveLength(1);
334
+ var lost = result.notWritten[0];
335
+ expect(lost.scope).toBe("x_cadso_work");
336
+ expect(lost.table).toBe("sys_ux_client_script");
337
+ expect(lost.sysId).toBe("c0ffee0047d56910fc4c1ae8036d43c0");
338
+ expect(lost.folder).toBe("console.log");
339
+ expect(lost.path).toBe(path.join(tmpRoot, "sys_ux_client_script", "console.log"));
340
+ expect(lost.reason).toContain("ignored by git");
341
+ expect(lost.reason).toContain("*.log");
342
+ expect(lost.reason).toContain(".gitignore:2");
343
+ });
344
+ test("the files are still on disk, so fixing the ignore rule is enough", async function () {
345
+ serve(rows);
346
+ await AppUtils.syncManifest("x_cadso_work");
347
+ expect(scriptIn("sys_ux_client_script", "console.log")).toBe("// hidden");
348
+ });
349
+ test("a record git can see is not reported", async function () {
350
+ serve([rows[1]]);
351
+ var result = await AppUtils.syncManifest("x_cadso_work");
352
+ expect(result.notWritten).toEqual([]);
353
+ });
354
+ test("an ignored file inside a visible folder is reported too", async function () {
355
+ fs.writeFileSync(path.join(tmpRoot, ".gitignore"), "script.js\n");
356
+ serve([rows[1]]);
357
+ var result = await AppUtils.syncManifest("x_cadso_work");
358
+ expect(result.notWritten).toHaveLength(1);
359
+ expect(result.notWritten[0].folder).toBe("onLoad");
360
+ expect(result.notWritten[0].reason).toContain("script.js");
361
+ });
362
+ test("a file git already tracks is not reported, whatever the ignore rules say", async function () {
363
+ serve(rows);
364
+ await AppUtils.syncManifest("x_cadso_work");
365
+ git(["add", "-f", "--", "sys_ux_client_script/console.log"]);
366
+ var result = await AppUtils.syncManifest("x_cadso_work");
367
+ expect(result.notWritten).toEqual([]);
368
+ });
369
+ test("outside a git repository nothing is reported", async function () {
370
+ fs.rmSync(path.join(tmpRoot, ".git"), { recursive: true, force: true });
371
+ serve(rows);
372
+ var result = await AppUtils.syncManifest("x_cadso_work");
373
+ expect(result.notWritten).toEqual([]);
374
+ });
375
+ });
376
+ // ---------- folders that cannot be created ----------
377
+ describe("refresh: a folder that cannot be created is reported, and the rest is written", function () {
378
+ var rows = [
379
+ { table: "sys_ux_client_script", name: "blocked", sys_id: "b10cced047d56910fc4c1ae8036d43c0", script: "// blocked" },
380
+ { table: "sys_ux_client_script", name: "onLoad", sys_id: "0d15ea5e47d56910fc4c1ae8036d43c0", script: "// seen" },
381
+ ];
382
+ beforeEach(function () {
383
+ // A regular file sits where the record's folder has to go.
384
+ fs.mkdirSync(path.join(tmpRoot, "sys_ux_client_script"), { recursive: true });
385
+ fs.writeFileSync(path.join(tmpRoot, "sys_ux_client_script", "blocked"), "not a folder");
386
+ });
387
+ test("the record is named with the reason", async function () {
388
+ serve(rows);
389
+ var result = await AppUtils.syncManifest("x_cadso_work");
390
+ expect(result.failedScopes).toEqual([]);
391
+ expect(result.notWritten).toHaveLength(1);
392
+ expect(result.notWritten[0].sysId).toBe("b10cced047d56910fc4c1ae8036d43c0");
393
+ expect(result.notWritten[0].folder).toBe("blocked");
394
+ expect(result.notWritten[0].reason).toContain("could not be written");
395
+ });
396
+ test("every other record of the application is still written", async function () {
397
+ serve(rows);
398
+ await AppUtils.syncManifest("x_cadso_work");
399
+ expect(scriptIn("sys_ux_client_script", "onLoad")).toBe("// seen");
400
+ });
401
+ });
402
+ // ---------- the command ----------
403
+ describe("refreshCommand: records that were not written fail the command", function () {
404
+ var originalExitCode;
405
+ beforeEach(function () {
406
+ originalExitCode = process.exitCode;
407
+ process.exitCode = undefined;
408
+ });
409
+ afterEach(function () {
410
+ process.exitCode = originalExitCode;
411
+ jest.restoreAllMocks();
412
+ });
413
+ function errorLines() {
414
+ return mockLogger.error.mock.calls.map(function (c) { return String(c[0]); });
415
+ }
416
+ test("prints every record by name, exits non-zero, and does not claim success", async function () {
417
+ jest.spyOn(AppUtils, "syncManifest").mockResolvedValue({
418
+ failedScopes: [],
419
+ notWritten: [
420
+ {
421
+ scope: "x_cadso_work",
422
+ table: "sys_ux_client_script",
423
+ sysId: "c0ffee0047d56910fc4c1ae8036d43c0",
424
+ folder: "console.log",
425
+ path: "/repo/src/Work/sys_ux_client_script/console.log",
426
+ reason: "ignored by git (.gitignore:2 *.log)",
427
+ },
428
+ ],
429
+ });
430
+ await (0, commands_1.refreshCommand)({});
431
+ expect(process.exitCode).toBe(1);
432
+ expect(mockLogger.success).not.toHaveBeenCalled();
433
+ var lines = errorLines();
434
+ expect(lines.some(function (l) { return l.indexOf("1 record") !== -1 && l.indexOf("not written") !== -1; })).toBe(true);
435
+ expect(lines.some(function (l) {
436
+ return l.indexOf("/repo/src/Work/sys_ux_client_script/console.log") !== -1 &&
437
+ l.indexOf("c0ffee0047d56910fc4c1ae8036d43c0") !== -1 &&
438
+ l.indexOf("ignored by git (.gitignore:2 *.log)") !== -1;
439
+ })).toBe(true);
440
+ });
441
+ test("end to end: a refresh that hits an ignored folder exits non-zero", async function () {
442
+ git(["init", "-q"]);
443
+ fs.writeFileSync(path.join(tmpRoot, ".gitignore"), "*.log\n");
444
+ serve([
445
+ { table: "sys_ux_client_script", name: "console.log", sys_id: "c0ffee0047d56910fc4c1ae8036d43c0", script: "// hidden" },
446
+ ]);
447
+ await (0, commands_1.refreshCommand)({ scope: "x_cadso_work" });
448
+ expect(process.exitCode).toBe(1);
449
+ expect(errorLines().join("\n")).toContain("console.log");
450
+ });
451
+ test("a clean refresh still succeeds", async function () {
452
+ serve([
453
+ { table: "sys_ux_client_script", name: "onLoad", sys_id: "0d15ea5e47d56910fc4c1ae8036d43c0", script: "// seen" },
454
+ ]);
455
+ await (0, commands_1.refreshCommand)({ scope: "x_cadso_work" });
456
+ expect(process.exitCode).toBeUndefined();
457
+ expect(mockLogger.success).toHaveBeenCalledWith("Refresh complete!");
458
+ });
459
+ });
@@ -0,0 +1,133 @@
1
+ "use strict";
2
+ // getManifest and getMissingFiles (bulkDownload) are the two reads every
3
+ // refresh/download/pull path uses, so layout elements are added there once.
4
+ // Checks that `layoutElements` never reaches the instance, that a filter such as
5
+ // `query: "sys_userISEMPTY"` still does, that the child rows are read through the
6
+ // Table API, and that nothing changes for a mirror that does not set the option.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ jest.mock("../Logger", function () {
9
+ return {
10
+ logger: {
11
+ info: jest.fn(),
12
+ debug: jest.fn(),
13
+ warn: jest.fn(),
14
+ error: jest.fn(),
15
+ getLogLevel: function () { return "debug"; },
16
+ },
17
+ };
18
+ });
19
+ jest.mock("../FileLogger", function () {
20
+ return { fileLogger: { debug: jest.fn(), info: jest.fn(), warn: jest.fn(), error: jest.fn() } };
21
+ });
22
+ const snClient_1 = require("../snClient");
23
+ const SHARED = "1".repeat(32);
24
+ const PERSONAL = "2".repeat(32);
25
+ function okResponse(result) {
26
+ return { status: 200, data: { result: result }, headers: {}, statusText: "OK", config: {} };
27
+ }
28
+ function makeClient(post, get) {
29
+ const c = (0, snClient_1.snClient)("https://x.service-now.com/", "u", "p");
30
+ c.client.post = post;
31
+ c.client.get = get;
32
+ return c;
33
+ }
34
+ const LAYOUT_OPTIONS = {
35
+ sys_ui_list: { layoutElements: true, query: "sys_userISEMPTY", nameTemplate: "{name}.{view}" },
36
+ };
37
+ describe("snClient layout elements hook", function () {
38
+ it("getManifest strips layoutElements, forwards the personal-layout filter, and lists the columns file", async function () {
39
+ // The instance applies `query`, so only the shared layout comes back.
40
+ const post = jest.fn().mockResolvedValue(okResponse({
41
+ scope: "x_cadso_work",
42
+ tables: {
43
+ sys_ui_list: {
44
+ records: {
45
+ "x_cadso_work_task.Default view": {
46
+ name: "x_cadso_work_task.Default view",
47
+ sys_id: SHARED,
48
+ files: [{ name: "name", type: "txt" }],
49
+ },
50
+ },
51
+ },
52
+ },
53
+ }));
54
+ const get = jest.fn();
55
+ const c = makeClient(post, get);
56
+ const config = { includes: {}, excludes: {}, tableOptions: LAYOUT_OPTIONS };
57
+ const resp = await c.getManifest("x_cadso_work", config);
58
+ const body = post.mock.calls[0][1];
59
+ expect(body.tableOptions).toEqual({ sys_ui_list: { query: "sys_userISEMPTY", nameTemplate: "{name}.{view}" } });
60
+ expect(JSON.stringify(body)).not.toContain("layoutElements");
61
+ expect(resp.data.result.tables.sys_ui_list.records["x_cadso_work_task.Default view"].files).toEqual([
62
+ { name: "name", type: "txt" },
63
+ { name: "columns", type: "txt" },
64
+ ]);
65
+ // Structure only: no child rows are read yet.
66
+ expect(get).not.toHaveBeenCalled();
67
+ // The caller's config is not mutated.
68
+ expect(LAYOUT_OPTIONS.sys_ui_list.layoutElements).toBe(true);
69
+ });
70
+ it("getMissingFiles asks the instance for real fields only and fills the columns from the Table API", async function () {
71
+ const post = jest.fn().mockResolvedValue(okResponse({
72
+ sys_ui_list: {
73
+ records: {
74
+ "x_cadso_work_task.Default view": {
75
+ name: "x_cadso_work_task.Default view",
76
+ sys_id: SHARED,
77
+ files: [
78
+ { name: "name", type: "txt", content: "x_cadso_work_task" },
79
+ { name: "metaData", type: "json", content: "{}" },
80
+ ],
81
+ },
82
+ },
83
+ },
84
+ }));
85
+ const get = jest.fn().mockResolvedValue(okResponse([
86
+ { sys_id: "9".repeat(32), list_id: SHARED, element: "short_description", position: "1" },
87
+ { sys_id: "8".repeat(32), list_id: SHARED, element: "number", position: "0" },
88
+ ]));
89
+ const c = makeClient(post, get);
90
+ const resp = await c.getMissingFiles({ sys_ui_list: { [SHARED]: [{ name: "name", type: "txt" }, { name: "columns", type: "txt" }] } }, LAYOUT_OPTIONS);
91
+ const body = post.mock.calls[0][1];
92
+ expect(body.missingFiles).toEqual({ sys_ui_list: { [SHARED]: [{ name: "name", type: "txt" }] } });
93
+ expect(JSON.stringify(body.tableOptions)).not.toContain("layoutElements");
94
+ expect(get).toHaveBeenCalledTimes(1);
95
+ const url = get.mock.calls[0][0];
96
+ const params = get.mock.calls[0][1].params;
97
+ expect(url).toBe("api/now/table/sys_ui_list_element");
98
+ expect(params.sysparm_query).toBe("list_idIN" + SHARED + "^ORDERBYsys_id");
99
+ expect(params.sysparm_exclude_reference_link).toBe("true");
100
+ // A long IN list makes the instance refuse to build its pagination Link
101
+ // header ("Pagination not supported", HTTP 400); paging is done by offset here.
102
+ expect(params.sysparm_suppress_pagination_header).toBe("true");
103
+ expect(params.sysparm_limit).toBe("1000");
104
+ expect(params.sysparm_offset).toBe("0");
105
+ const files = resp.data.result.sys_ui_list.records["x_cadso_work_task.Default view"].files;
106
+ expect(files[files.length - 1]).toEqual({ name: "columns", type: "txt", content: "number\nshort_description\n" });
107
+ });
108
+ it("never reads or writes a personal layout the instance left out", async function () {
109
+ const post = jest.fn().mockResolvedValue(okResponse({ sys_ui_list: { records: {} } }));
110
+ const get = jest.fn().mockResolvedValue(okResponse([]));
111
+ const c = makeClient(post, get);
112
+ await c.getMissingFiles({ sys_ui_list: { [SHARED]: [{ name: "columns", type: "txt" }] } }, LAYOUT_OPTIONS);
113
+ const query = get.mock.calls[0][1].params.sysparm_query;
114
+ expect(query).not.toContain(PERSONAL);
115
+ });
116
+ it("changes nothing for a mirror that does not set layoutElements", async function () {
117
+ const result = {
118
+ scope: "x_cadso_work",
119
+ tables: {
120
+ sys_ui_list: {
121
+ records: { a: { name: "a", sys_id: SHARED, files: [{ name: "name", type: "txt" }] } },
122
+ },
123
+ },
124
+ };
125
+ const post = jest.fn().mockResolvedValue(okResponse(JSON.parse(JSON.stringify(result))));
126
+ const get = jest.fn();
127
+ const c = makeClient(post, get);
128
+ const config = { includes: {}, excludes: {}, tableOptions: { sys_ui_list: { query: "sys_userISEMPTY" } } };
129
+ const resp = await c.getManifest("x_cadso_work", config);
130
+ expect(resp.data.result).toEqual(result);
131
+ expect(get).not.toHaveBeenCalled();
132
+ });
133
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tenonhq/dovetail-core",
3
- "version": "0.0.132",
3
+ "version": "0.0.134",
4
4
  "description": "Next-gen file syncer",
5
5
  "license": "GPL-3.0",
6
6
  "main": "./dist/index.js",
@@ -22,7 +22,7 @@
22
22
  "node": ">=22"
23
23
  },
24
24
  "devDependencies": {
25
- "@tenonhq/dovetail-types": "~0.0.24",
25
+ "@tenonhq/dovetail-types": "~0.0.25",
26
26
  "@types/dotenv": "^6.1.1",
27
27
  "@types/inquirer": "^8.2.10",
28
28
  "@types/jest": "^29.5.5",
@@ -39,7 +39,7 @@
39
39
  },
40
40
  "dependencies": {
41
41
  "@tenonhq/dovetail-clickup": "~0.0.15",
42
- "@tenonhq/dovetail-schema": "~0.0.17",
42
+ "@tenonhq/dovetail-schema": "~0.0.18",
43
43
  "axios": "^1.20.0",
44
44
  "axios-cookiejar-support": "^4.0.7",
45
45
  "axios-rate-limit": "^1.3.0",