@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,314 @@
1
+ "use strict";
2
+ /**
3
+ * Record folder naming: one folder per record, on every disk.
4
+ *
5
+ * Three ways a mirror lost records, each reproduced here from the shape of the
6
+ * records that were lost on a real instance:
7
+ *
8
+ * 1. NAME TWINS. Two records of one table share their display name AND the
9
+ * first 8 characters of their sys_id (access controls created in one
10
+ * transaction differ only in the last few characters). `Name (first 8)`
11
+ * was the same folder for both, so one of them had no folder and no
12
+ * manifest entry.
13
+ * 2. LETTER-CASE TWINS. "Search default" and "Search Default" were treated as
14
+ * two names, got no suffix, and are one folder on macOS and Windows.
15
+ * 3. Anything else that would make two records share a folder must fail
16
+ * loudly instead of dropping one.
17
+ *
18
+ * The rule under test keeps every name a record has today unless that record
19
+ * collides, so an existing mirror renames as little as possible.
20
+ */
21
+ Object.defineProperty(exports, "__esModule", { value: true });
22
+ const folderNaming_1 = require("../folderNaming");
23
+ // assignFolderNames answers in input order; index the answers by sys_id.
24
+ const names = (records) => {
25
+ const out = {};
26
+ (0, folderNaming_1.assignFolderNames)(records).forEach((folder, index) => {
27
+ out[records[index].sys_id] = folder;
28
+ });
29
+ return out;
30
+ };
31
+ const folders = (records) => Object.values(names(records)).sort();
32
+ // Real shapes: sys_ids generated in one transaction share all but the tail.
33
+ const FIRE_LOG_A = "15f0e0ea33dfc7107b18bc534d5c7bc4";
34
+ const FIRE_LOG_B = "15f0e0ea33dfc7107b18bc534d5c7bf5";
35
+ const ANNOUNCE_A = "6d462a3bc3744714d4ddf1db05013163";
36
+ const ANNOUNCE_B = "6d462a3bc3744714d4ddf1db0501317b";
37
+ const ANNOUNCE_C = "6d462a3bc3744714d4ddf1db05013193";
38
+ describe("assignFolderNames: records that do not collide keep their name", () => {
39
+ it("leaves a unique display name plain", () => {
40
+ expect(names([
41
+ { name: "Send Email", sys_id: "dc2ef56133a0e2507b18bc534d5c7bf5" },
42
+ { name: "Send Text", sys_id: "0d4626f933e826507b18bc534d5c7b37" },
43
+ ])).toEqual({
44
+ dc2ef56133a0e2507b18bc534d5c7bf5: "Send Email",
45
+ "0d4626f933e826507b18bc534d5c7b37": "Send Text",
46
+ });
47
+ });
48
+ it("keeps the 8-character suffix for namesakes whose ids differ in the first 8", () => {
49
+ // As the instance sends them: already suffixed.
50
+ expect(folders([
51
+ { name: "Audience (0616384a)", sys_id: "0616384ac3f492d085b196c4e40131aa" },
52
+ { name: "Audience (92953c0a)", sys_id: "92953c0ac3f492d085b196c4e40131bb" },
53
+ ])).toEqual(["Audience (0616384a)", "Audience (92953c0a)"]);
54
+ });
55
+ it("suffixes every namesake when the instance did not", () => {
56
+ // No record keeps the bare name: which one would is an accident of order.
57
+ expect(folders([
58
+ { name: "Blueprint", sys_id: "1607d7f0c373f2d0d4ddf1db05013101" },
59
+ { name: "Blueprint", sys_id: "46ae2decc33f32d0d4ddf1db050131d9" },
60
+ ])).toEqual(["Blueprint (1607d7f0)", "Blueprint (46ae2dec)"]);
61
+ });
62
+ it("keeps the sys_id fallback for a name no disk can hold", () => {
63
+ expect(names([
64
+ { name: "x_cadso_work_task.*", sys_id: "bc91c2a5f334621048dff31590812771" },
65
+ { name: "Email Status Change ", sys_id: "fefb8758330ede107b18bc534d5c7bfb" },
66
+ { name: "", sys_id: "03227e9bc3850f10d4ddf1db050131b0" },
67
+ ])).toEqual({
68
+ bc91c2a5f334621048dff31590812771: "bc91c2a5f334621048dff31590812771",
69
+ fefb8758330ede107b18bc534d5c7bfb: "fefb8758330ede107b18bc534d5c7bfb",
70
+ "03227e9bc3850f10d4ddf1db050131b0": "03227e9bc3850f10d4ddf1db050131b0",
71
+ });
72
+ });
73
+ });
74
+ describe("assignFolderNames: name twins (same name, same first 8 id characters)", () => {
75
+ it("gives each twin its own folder: the suffix grows to the full id", () => {
76
+ const out = names([
77
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_A },
78
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_B },
79
+ ]);
80
+ expect(out[FIRE_LOG_A]).toBe("x_cadso_journey_fire_log (" + FIRE_LOG_A + ")");
81
+ expect(out[FIRE_LOG_B]).toBe("x_cadso_journey_fire_log (" + FIRE_LOG_B + ")");
82
+ });
83
+ it("does not leave either twin in the ambiguous 8-character folder", () => {
84
+ // Whichever twin kept `Name (15f0e0ea)` would hand it to the other the day
85
+ // it is deleted, and a push to that folder would write the wrong record.
86
+ expect(folders([
87
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_A },
88
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_B },
89
+ ])).not.toContain("x_cadso_journey_fire_log (15f0e0ea)");
90
+ });
91
+ it("handles a three-way twin group: one folder per record", () => {
92
+ const out = folders([
93
+ { name: "x_cadso_core_announcement", sys_id: ANNOUNCE_A },
94
+ { name: "x_cadso_core_announcement", sys_id: ANNOUNCE_B },
95
+ { name: "x_cadso_core_announcement", sys_id: ANNOUNCE_C },
96
+ ]);
97
+ expect(out).toHaveLength(3);
98
+ expect(new Set(out).size).toBe(3);
99
+ expect(out).toEqual([
100
+ "x_cadso_core_announcement (" + ANNOUNCE_A + ")",
101
+ "x_cadso_core_announcement (" + ANNOUNCE_B + ")",
102
+ "x_cadso_core_announcement (" + ANNOUNCE_C + ")",
103
+ ]);
104
+ });
105
+ it("lengthens only the records that collide, not their other namesakes", () => {
106
+ const other = "aa11bb22cc33dd44ee55ff6600112233";
107
+ const out = names([
108
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_A },
109
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_B },
110
+ { name: "x_cadso_journey_fire_log", sys_id: other },
111
+ ]);
112
+ expect(out[other]).toBe("x_cadso_journey_fire_log (aa11bb22)");
113
+ expect(out[FIRE_LOG_A]).toBe("x_cadso_journey_fire_log (" + FIRE_LOG_A + ")");
114
+ expect(out[FIRE_LOG_B]).toBe("x_cadso_journey_fire_log (" + FIRE_LOG_B + ")");
115
+ });
116
+ it("resolves twins the instance already suffixed with 8 characters", () => {
117
+ // What an instance that still names by `Name (first 8)` would send if it
118
+ // listed both: the same name twice.
119
+ const out = names([
120
+ { name: "Instance Step (c63eea7d)", sys_id: "c63eea7d33591a107b18bc534d5c7b12" },
121
+ { name: "Instance Step (c63eea7d)", sys_id: "c63eea7d33591a107b18bc534d5c7b3d" },
122
+ ]);
123
+ expect(out).toEqual({
124
+ c63eea7d33591a107b18bc534d5c7b12: "Instance Step (c63eea7d33591a107b18bc534d5c7b12)",
125
+ c63eea7d33591a107b18bc534d5c7b3d: "Instance Step (c63eea7d33591a107b18bc534d5c7b3d)",
126
+ });
127
+ });
128
+ it("never returns fewer folders than records", () => {
129
+ const records = [];
130
+ for (let i = 0; i < 40; i++) {
131
+ // 40 records, one name, ids identical in the first 30 characters.
132
+ records.push({
133
+ name: "x_cadso_work_goal",
134
+ sys_id: "c7fdaa3d470ef510b0361ae8036d43" + (i < 16 ? "0" : "") + i.toString(16),
135
+ });
136
+ }
137
+ const out = folders(records);
138
+ expect(out).toHaveLength(40);
139
+ expect(new Set(out.map(folderNaming_1.foldFolderName)).size).toBe(40);
140
+ });
141
+ });
142
+ describe("assignFolderNames: letter-case and normalization twins", () => {
143
+ it("suffixes names that differ only in letter case", () => {
144
+ expect(names([
145
+ { name: "Search default", sys_id: "7daaf59947d56910fc4c1ae8036d43c0" },
146
+ { name: "Search Default", sys_id: "fb55e60db195a5101b815df7b6dd8425" },
147
+ ])).toEqual({
148
+ "7daaf59947d56910fc4c1ae8036d43c0": "Search default (7daaf599)",
149
+ fb55e60db195a5101b815df7b6dd8425: "Search Default (fb55e60d)",
150
+ });
151
+ });
152
+ it("covers the other pairs lost on a case-insensitive disk", () => {
153
+ expect(folders([
154
+ { name: "Attachment", sys_id: "a77c5d71c3af025085b196c4e40131a4" },
155
+ { name: "attachment", sys_id: "67ac6651c34b421085b196c4e40131ea" },
156
+ { name: "instanceStepGr", sys_id: "12f10af8c3591610d4ddf1db05013195" },
157
+ { name: "instanceStepGR", sys_id: "c283177233fd1a107b18bc534d5c7ba6" },
158
+ ])).toEqual([
159
+ "Attachment (a77c5d71)",
160
+ "attachment (67ac6651)",
161
+ "instanceStepGR (c2831772)",
162
+ "instanceStepGr (12f10af8)",
163
+ ]);
164
+ });
165
+ it("produces folders that are distinct on a case-insensitive disk", () => {
166
+ const out = folders([
167
+ { name: "Link to destination relay (Lead - Overview)", sys_id: "132cdca9c372821085b196c4e4013139" },
168
+ { name: "Link to destination Relay (Lead - Overview)", sys_id: "d6e4b328c3f492d085b196c4e40131e4" },
169
+ ]);
170
+ expect(new Set(out.map(folderNaming_1.foldFolderName)).size).toBe(2);
171
+ });
172
+ it("suffixes names that differ only in Unicode normalization form", () => {
173
+ const composed = "Café"; // é as one code point
174
+ const decomposed = "Café"; // e + combining acute
175
+ const out = names([
176
+ { name: composed, sys_id: "aaaaaaaa000000000000000000000001" },
177
+ { name: decomposed, sys_id: "bbbbbbbb000000000000000000000002" },
178
+ ]);
179
+ expect(out["aaaaaaaa000000000000000000000001"]).toBe(composed + " (aaaaaaaa)");
180
+ expect(out["bbbbbbbb000000000000000000000002"]).toBe(decomposed + " (bbbbbbbb)");
181
+ });
182
+ it("counts a differently-cased name against namesakes the instance suffixed", () => {
183
+ // The instance counts exact names: two "Skip" got a suffix, "skip" did not.
184
+ expect(folders([
185
+ { name: "Skip (497eb251)", sys_id: "497eb251c3591610d4ddf1db05013100" },
186
+ { name: "Skip (74d107d5)", sys_id: "74d107d5c3591610d4ddf1db05013101" },
187
+ { name: "skip", sys_id: "0a0b0c0dc3591610d4ddf1db05013102" },
188
+ ])).toEqual(["Skip (497eb251)", "Skip (74d107d5)", "skip (0a0b0c0d)"]);
189
+ });
190
+ it("lengthens case twins that also share the first 8 id characters", () => {
191
+ const out = folders([
192
+ { name: "Report", sys_id: "abcdef0011111111111111111111111a" },
193
+ { name: "REPORT", sys_id: "abcdef0011111111111111111111111b" },
194
+ ]);
195
+ expect(out).toEqual([
196
+ "REPORT (abcdef0011111111111111111111111b)",
197
+ "Report (abcdef0011111111111111111111111a)",
198
+ ]);
199
+ });
200
+ it("does not rename a record whose name is unique ignoring case", () => {
201
+ expect(folders([
202
+ { name: "Nav Header API Success", sys_id: "11111111c3591610d4ddf1db05013100" },
203
+ { name: "Nav Header API Failure", sys_id: "22222222c3591610d4ddf1db05013100" },
204
+ ])).toEqual(["Nav Header API Failure", "Nav Header API Success"]);
205
+ });
206
+ });
207
+ describe("assignFolderNames: deterministic", () => {
208
+ const records = [
209
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_A },
210
+ { name: "Search default", sys_id: "7daaf59947d56910fc4c1ae8036d43c0" },
211
+ { name: "Send Email", sys_id: "dc2ef56133a0e2507b18bc534d5c7bf5" },
212
+ { name: "x_cadso_journey_fire_log", sys_id: FIRE_LOG_B },
213
+ { name: "Search Default", sys_id: "fb55e60db195a5101b815df7b6dd8425" },
214
+ { name: "x_cadso_work_task.*", sys_id: "bc91c2a5f334621048dff31590812771" },
215
+ ];
216
+ it("gives the same names whatever order the records arrive in", () => {
217
+ const forward = names(records);
218
+ const backward = names(records.slice().reverse());
219
+ expect(backward).toEqual(forward);
220
+ });
221
+ it("is idempotent: naming the named records again changes nothing", () => {
222
+ const first = names(records);
223
+ const renamed = records.map((r) => ({ name: first[r.sys_id], sys_id: r.sys_id }));
224
+ expect(names(renamed)).toEqual(first);
225
+ });
226
+ it("treats one sys_id listed twice as one record, not as two namesakes", () => {
227
+ expect((0, folderNaming_1.assignFolderNames)([
228
+ { name: "Send Email", sys_id: "dc2ef56133a0e2507b18bc534d5c7bf5" },
229
+ { name: "Send Email", sys_id: "dc2ef56133a0e2507b18bc534d5c7bf5" },
230
+ ])).toEqual(["Send Email", "Send Email"]);
231
+ });
232
+ it("answers in input order, one name per record", () => {
233
+ expect((0, folderNaming_1.assignFolderNames)([
234
+ { name: "Search Default", sys_id: "fb55e60db195a5101b815df7b6dd8425" },
235
+ { name: "Send Email", sys_id: "dc2ef56133a0e2507b18bc534d5c7bf5" },
236
+ { name: "Search default", sys_id: "7daaf59947d56910fc4c1ae8036d43c0" },
237
+ ])).toEqual(["Search Default (fb55e60d)", "Send Email", "Search default (7daaf599)"]);
238
+ });
239
+ });
240
+ describe("assignFolderNames: names the project configured", () => {
241
+ // tableOptions.differentiatorField: "sys_id" and a nameTemplate ending in
242
+ // "({sys_id})" put the record's own id at the end of its name on purpose.
243
+ // That is the name the project asked for, not a suffix naming added.
244
+ const HOME = "0a1b2c3d47d56910fc4c1ae8036d43c0";
245
+ it("keeps an id the configuration put in the name", () => {
246
+ expect((0, folderNaming_1.assignFolderNames)([{ name: "Home (" + HOME + ")", sys_id: HOME }], { configuredNames: true })).toEqual(["Home (" + HOME + ")"]);
247
+ });
248
+ it("still gives every record its own folder", () => {
249
+ expect((0, folderNaming_1.assignFolderNames)([
250
+ { name: "Search default", sys_id: "7daaf59947d56910fc4c1ae8036d43c0" },
251
+ { name: "Search Default", sys_id: "fb55e60db195a5101b815df7b6dd8425" },
252
+ { name: "x_cadso_journey_fire_log (15f0e0ea)", sys_id: FIRE_LOG_A },
253
+ { name: "x_cadso_journey_fire_log (15f0e0ea)", sys_id: FIRE_LOG_B },
254
+ ], { configuredNames: true })).toEqual([
255
+ "Search default (7daaf599)",
256
+ "Search Default (fb55e60d)",
257
+ "x_cadso_journey_fire_log (15f0e0ea) (" + FIRE_LOG_A + ")",
258
+ "x_cadso_journey_fire_log (15f0e0ea) (" + FIRE_LOG_B + ")",
259
+ ]);
260
+ });
261
+ it("without the option, the same name is read as a suffix and recomputed", () => {
262
+ expect((0, folderNaming_1.assignFolderNames)([{ name: "Home (" + HOME + ")", sys_id: HOME }])).toEqual(["Home"]);
263
+ });
264
+ });
265
+ describe("assignFolderNames: refuses to drop a record", () => {
266
+ it("throws, naming the records, when two cannot be told apart", () => {
267
+ // No sys_id to tell them apart: there is no honest folder name for both.
268
+ const run = () => (0, folderNaming_1.assignFolderNames)([
269
+ { name: "Orphan", sys_id: "" },
270
+ { name: "orphan", sys_id: "" },
271
+ { name: "Other", sys_id: "cccccccc000000000000000000000003" },
272
+ ]);
273
+ expect(run).toThrow(folderNaming_1.FolderNameCollisionError);
274
+ expect(run).toThrow(/Orphan/);
275
+ });
276
+ it("rejects input that is not a list of records", () => {
277
+ expect(() => (0, folderNaming_1.assignFolderNames)(null)).toThrow(TypeError);
278
+ });
279
+ it("tells records apart whose ids are also Object.prototype members", () => {
280
+ // Nothing a real instance sends; the bookkeeping must not trip on it.
281
+ expect((0, folderNaming_1.assignFolderNames)([
282
+ { name: "Dup", sys_id: "constructor" },
283
+ { name: "Dup", sys_id: "toString" },
284
+ { name: "Dup", sys_id: "__proto__" },
285
+ ])).toEqual(["Dup (construc)", "Dup (toString)", "Dup (__proto_)"]);
286
+ });
287
+ it("refuses malformed entries with the same error, not a crash", () => {
288
+ const broken = [null, null];
289
+ expect(() => (0, folderNaming_1.assignFolderNames)(broken)).toThrow(folderNaming_1.FolderNameCollisionError);
290
+ });
291
+ });
292
+ describe("baseRecordName", () => {
293
+ it("removes the id suffix the instance added", () => {
294
+ expect((0, folderNaming_1.baseRecordName)("Audience (0616384a)", "0616384ac3f492d085b196c4e40131aa")).toBe("Audience");
295
+ expect((0, folderNaming_1.baseRecordName)("x_cadso_journey_fire_log (" + FIRE_LOG_A + ")", FIRE_LOG_A)).toBe("x_cadso_journey_fire_log");
296
+ });
297
+ it("keeps parentheses that are part of the display name", () => {
298
+ expect((0, folderNaming_1.baseRecordName)("Check for Journey (Email Event)", "be252ec7c3dad690d4ddf1db050131d2")).toBe("Check for Journey (Email Event)");
299
+ // Another record's id prefix is not this record's suffix.
300
+ expect((0, folderNaming_1.baseRecordName)("Audience (0616384a)", "92953c0ac3f492d085b196c4e40131bb")).toBe("Audience (0616384a)");
301
+ // Fewer than 8 characters is not a suffix Dovetail writes.
302
+ expect((0, folderNaming_1.baseRecordName)("Step (0616)", "0616384ac3f492d085b196c4e40131aa")).toBe("Step (0616)");
303
+ });
304
+ });
305
+ describe("foldFolderName", () => {
306
+ it("is equal for names a case-insensitive disk cannot tell apart", () => {
307
+ expect((0, folderNaming_1.foldFolderName)("Search default")).toBe((0, folderNaming_1.foldFolderName)("Search Default"));
308
+ expect((0, folderNaming_1.foldFolderName)("Café")).toBe((0, folderNaming_1.foldFolderName)("Café"));
309
+ expect((0, folderNaming_1.foldFolderName)("STRASSE")).toBe((0, folderNaming_1.foldFolderName)("straße"));
310
+ });
311
+ it("is different for names that really differ", () => {
312
+ expect((0, folderNaming_1.foldFolderName)("Send Email")).not.toBe((0, folderNaming_1.foldFolderName)("Send Text"));
313
+ });
314
+ });
@@ -0,0 +1,259 @@
1
+ "use strict";
2
+ // Layout elements (layoutElements.ts): the child rows of a list layout, a form
3
+ // section and a form are mirrored as one ordered, line-per-item text file in the
4
+ // parent layout's folder. Covers config validation, formatting and ordering, the
5
+ // manifest and bulkDownload rewrites, the Table API reads, and the path test
6
+ // push uses to refuse these files.
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ const layoutElements_1 = require("../layoutElements");
9
+ const LIST_A = "a".repeat(32);
10
+ const LIST_B = "b".repeat(32);
11
+ const SECTION = "c".repeat(32);
12
+ const FORM = "d".repeat(32);
13
+ const MAIN_SECTION = "e".repeat(32);
14
+ const DETAILS_SECTION = "f".repeat(32);
15
+ function id(n) {
16
+ const s = String(n);
17
+ return "0".repeat(32 - s.length) + s;
18
+ }
19
+ // A reader that answers from in-memory child rows, honoring the IN list, the
20
+ // page size and the offset, and records every call.
21
+ function fakeReader(rowsByTable, parentField) {
22
+ const calls = [];
23
+ const read = async function (table, query, fields, limit, offset) {
24
+ calls.push({ table: table, query: query, fields: fields, limit: limit, offset: offset });
25
+ const m = /^([a-z_]+)IN([0-9a-f,]+)\^ORDERBYsys_id$/.exec(query);
26
+ if (!m)
27
+ throw new Error("unexpected query " + query);
28
+ const ids = m[2].split(",");
29
+ const all = (rowsByTable[table] || [])
30
+ .filter(function (r) { return ids.indexOf(String(r[parentField[table]])) !== -1; })
31
+ .slice()
32
+ .sort(function (x, y) { return String(x.sys_id) < String(y.sys_id) ? -1 : 1; });
33
+ return all.slice(offset, offset + limit);
34
+ };
35
+ return { read: read, calls: calls };
36
+ }
37
+ const PARENT_FIELD = {
38
+ sys_ui_list_element: "list_id",
39
+ sys_ui_element: "sys_ui_section",
40
+ sys_ui_form_section: "sys_ui_form",
41
+ };
42
+ describe("layoutElementTablesFor", function () {
43
+ it("is off by default: no option, no tables", function () {
44
+ expect((0, layoutElements_1.layoutElementTablesFor)(undefined)).toEqual([]);
45
+ expect((0, layoutElements_1.layoutElementTablesFor)({})).toEqual([]);
46
+ expect((0, layoutElements_1.layoutElementTablesFor)({ sys_ui_list: { query: "sys_userISEMPTY" } })).toEqual([]);
47
+ });
48
+ it("returns the layout tables that set layoutElements: true", function () {
49
+ const opts = {
50
+ sys_ui_list: { layoutElements: true },
51
+ sys_ui_section: { layoutElements: true },
52
+ sys_ui_form: { layoutElements: true },
53
+ };
54
+ expect((0, layoutElements_1.layoutElementTablesFor)(opts).sort()).toEqual(["sys_ui_form", "sys_ui_list", "sys_ui_section"]);
55
+ });
56
+ it("ignores and reports a value that is not true, or a table that has no layout elements", function () {
57
+ const warnings = [];
58
+ const opts = {
59
+ sys_ui_list: { layoutElements: "yes" },
60
+ sys_ui_section: { layoutElements: false },
61
+ sys_script: { layoutElements: true },
62
+ };
63
+ expect((0, layoutElements_1.layoutElementTablesFor)(opts, function (m) { warnings.push(m); })).toEqual([]);
64
+ expect(warnings.length).toBe(2);
65
+ expect(warnings.join("\n")).toContain("sys_ui_list");
66
+ expect(warnings.join("\n")).toContain("sys_script");
67
+ });
68
+ });
69
+ describe("formatLayoutElements", function () {
70
+ it("writes a list's columns in position order, one per line, with aggregates", function () {
71
+ const rows = [
72
+ { sys_id: id(3), list_id: LIST_A, element: "state", position: "2", sum: "false", average_value: "false", min_value: "false", max_value: "false" },
73
+ { sys_id: id(1), list_id: LIST_A, element: "number", position: "0", sum: "false", average_value: "false", min_value: "false", max_value: "false" },
74
+ { sys_id: id(2), list_id: LIST_A, element: "assigned_to.name", position: "1", sum: "false", average_value: "false", min_value: "false", max_value: "false" },
75
+ { sys_id: id(4), list_id: LIST_A, element: "amount", position: "10", sum: "true", average_value: "true", min_value: "false", max_value: "true" },
76
+ ];
77
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_list", rows)).toBe("number\nassigned_to.name\nstate\namount [sum, average, max]\n");
78
+ });
79
+ it("writes a section's fields in position order with the element type when it adds something", function () {
80
+ const rows = [
81
+ { sys_id: id(5), sys_ui_section: SECTION, element: ".end_split", type: ".end_split", position: "4" },
82
+ { sys_id: id(1), sys_ui_section: SECTION, element: ".begin_split", type: ".begin_split", position: "0" },
83
+ { sys_id: id(2), sys_ui_section: SECTION, element: "name", type: "", position: "1" },
84
+ { sys_id: id(3), sys_ui_section: SECTION, element: ".split", type: ".split", position: "2" },
85
+ { sys_id: id(4), sys_ui_section: SECTION, element: "activity.xml", type: "formatter", position: "3" },
86
+ { sys_id: id(6), sys_ui_section: SECTION, element: "x_cadso_work_task.project", type: "list", position: "5" },
87
+ ];
88
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_section", rows)).toBe(".begin_split\nname\n.split\nactivity.xml [formatter]\n.end_split\nx_cadso_work_task.project [list]\n");
89
+ });
90
+ it("writes a form's sections in position order by caption", function () {
91
+ const rows = [
92
+ { sys_id: id(2), sys_ui_form: FORM, position: "1", sys_ui_section: DETAILS_SECTION, "sys_ui_section.sys_id": DETAILS_SECTION, "sys_ui_section.caption": "Details" },
93
+ { sys_id: id(1), sys_ui_form: FORM, position: "0", sys_ui_section: MAIN_SECTION, "sys_ui_section.sys_id": MAIN_SECTION, "sys_ui_section.caption": "" },
94
+ { sys_id: id(3), sys_ui_form: FORM, position: "2", sys_ui_section: id(99), "sys_ui_section.sys_id": "", "sys_ui_section.caption": "" },
95
+ ];
96
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_form", rows)).toBe("(no caption)\nDetails\n(missing section " + id(99) + ")\n");
97
+ });
98
+ it("orders equal positions by the line, then by the row id, so the file is stable across pulls", function () {
99
+ const rows = [
100
+ { sys_id: id(9), list_id: LIST_A, element: "b", position: "1" },
101
+ { sys_id: id(8), list_id: LIST_A, element: "a", position: "1" },
102
+ { sys_id: id(7), list_id: LIST_A, element: "a", position: "1" },
103
+ ];
104
+ const once = (0, layoutElements_1.formatLayoutElements)("sys_ui_list", rows);
105
+ expect(once).toBe("a\na\nb\n");
106
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_list", rows.slice().reverse())).toBe(once);
107
+ });
108
+ it("writes an empty list as an empty file", function () {
109
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_list", [])).toBe("");
110
+ });
111
+ it("reads reference values whether or not the reference link is excluded", function () {
112
+ const rows = [
113
+ { sys_id: id(1), sys_ui_form: { value: FORM, link: "x" }, position: "0", sys_ui_section: { value: MAIN_SECTION, link: "y" }, "sys_ui_section.sys_id": MAIN_SECTION, "sys_ui_section.caption": "Main" },
114
+ ];
115
+ expect((0, layoutElements_1.formatLayoutElements)("sys_ui_form", rows)).toBe("Main\n");
116
+ });
117
+ });
118
+ describe("addLayoutElementFiles", function () {
119
+ function manifestTables() {
120
+ return {
121
+ sys_ui_list: {
122
+ records: {
123
+ "x_cadso_work_task.Default view": { name: "x_cadso_work_task.Default view", sys_id: LIST_A, files: [{ name: "name", type: "txt" }] },
124
+ },
125
+ },
126
+ sys_script: {
127
+ records: { "My rule": { name: "My rule", sys_id: id(1), files: [{ name: "script", type: "js" }] } },
128
+ },
129
+ };
130
+ }
131
+ it("adds the columns file to every record of an enabled table, and nothing else", function () {
132
+ const tables = manifestTables();
133
+ (0, layoutElements_1.addLayoutElementFiles)(tables, ["sys_ui_list"]);
134
+ expect(tables.sys_ui_list.records["x_cadso_work_task.Default view"].files).toEqual([
135
+ { name: "name", type: "txt" },
136
+ { name: "columns", type: "txt" },
137
+ ]);
138
+ expect(tables.sys_script.records["My rule"].files).toEqual([{ name: "script", type: "js" }]);
139
+ });
140
+ it("is a no-op when the option is off, and idempotent when on", function () {
141
+ const off = manifestTables();
142
+ (0, layoutElements_1.addLayoutElementFiles)(off, []);
143
+ expect(off).toEqual(manifestTables());
144
+ const on = manifestTables();
145
+ (0, layoutElements_1.addLayoutElementFiles)(on, ["sys_ui_list"]);
146
+ (0, layoutElements_1.addLayoutElementFiles)(on, ["sys_ui_list"]);
147
+ expect(on.sys_ui_list.records["x_cadso_work_task.Default view"].files.length).toBe(2);
148
+ });
149
+ });
150
+ describe("splitLayoutElementRequests", function () {
151
+ it("takes the derived files out of the request and remembers which records asked for them", function () {
152
+ const missing = {
153
+ sys_ui_list: {
154
+ [LIST_A]: [{ name: "name", type: "txt" }, { name: "columns", type: "txt" }],
155
+ [LIST_B]: [{ name: "columns", type: "txt" }],
156
+ },
157
+ sys_script: { [id(1)]: [{ name: "script", type: "js" }] },
158
+ };
159
+ const split = (0, layoutElements_1.splitLayoutElementRequests)(missing);
160
+ expect(split.request).toEqual({
161
+ sys_ui_list: { [LIST_A]: [{ name: "name", type: "txt" }], [LIST_B]: [] },
162
+ sys_script: { [id(1)]: [{ name: "script", type: "js" }] },
163
+ });
164
+ expect(split.wanted).toEqual({ sys_ui_list: [LIST_A, LIST_B] });
165
+ // The caller's map is not mutated.
166
+ expect(missing.sys_ui_list[LIST_A].length).toBe(2);
167
+ });
168
+ it("leaves a same-named field of another table alone", function () {
169
+ const missing = { x_custom: { [id(1)]: [{ name: "columns", type: "txt" }] } };
170
+ const split = (0, layoutElements_1.splitLayoutElementRequests)(missing);
171
+ expect(split.request).toEqual(missing);
172
+ expect(split.wanted).toEqual({});
173
+ });
174
+ });
175
+ describe("readLayoutElements", function () {
176
+ it("reads child rows for every parent, pages through them, and writes empty for a parent with none", async function () {
177
+ const rows = [];
178
+ for (let i = 0; i < 5; i++) {
179
+ rows.push({ sys_id: id(i + 1), list_id: LIST_A, element: "f" + i, position: String(4 - i) });
180
+ }
181
+ const fake = fakeReader({ sys_ui_list_element: rows }, PARENT_FIELD);
182
+ const out = await (0, layoutElements_1.readLayoutElements)(fake.read, "sys_ui_list", [LIST_A, LIST_B], { pageSize: 2, chunkSize: 100 });
183
+ expect(out[LIST_A]).toBe("f4\nf3\nf2\nf1\nf0\n");
184
+ expect(out[LIST_B]).toBe("");
185
+ expect(fake.calls.map(function (c) { return c.offset; })).toEqual([0, 2, 4]);
186
+ expect(fake.calls[0].table).toBe("sys_ui_list_element");
187
+ expect(fake.calls[0].query).toBe("list_idIN" + LIST_A + "," + LIST_B + "^ORDERBYsys_id");
188
+ });
189
+ it("splits a long parent list into several queries", async function () {
190
+ const parents = [];
191
+ for (let i = 0; i < 5; i++)
192
+ parents.push(id(100 + i));
193
+ const fake = fakeReader({ sys_ui_element: [] }, PARENT_FIELD);
194
+ await (0, layoutElements_1.readLayoutElements)(fake.read, "sys_ui_section", parents, { pageSize: 1000, chunkSize: 2 });
195
+ expect(fake.calls.length).toBe(3);
196
+ });
197
+ it("refuses a parent id that is not a record id, before any query runs", async function () {
198
+ const fake = fakeReader({}, PARENT_FIELD);
199
+ await expect((0, layoutElements_1.readLayoutElements)(fake.read, "sys_ui_list", [LIST_A, "x^NQsys_id!=1"], { pageSize: 1000, chunkSize: 100 })).rejects.toThrow(/record id/);
200
+ expect(fake.calls.length).toBe(0);
201
+ });
202
+ it("fails instead of writing an empty file when a read fails", async function () {
203
+ const read = async function () { throw new Error("HTTP 403"); };
204
+ await expect((0, layoutElements_1.readLayoutElements)(read, "sys_ui_list", [LIST_A], { pageSize: 1000, chunkSize: 100 })).rejects.toThrow(/403/);
205
+ });
206
+ it("ignores a returned row whose parent was not asked for", async function () {
207
+ const read = async function () {
208
+ return [{ sys_id: id(1), list_id: LIST_B, element: "stray", position: "0" }];
209
+ };
210
+ const out = await (0, layoutElements_1.readLayoutElements)(read, "sys_ui_list", [LIST_A], { pageSize: 1000, chunkSize: 100 });
211
+ expect(out).toEqual({ [LIST_A]: "" });
212
+ });
213
+ });
214
+ describe("fillLayoutElementContent", function () {
215
+ it("keys the content by record id, so two layouts with colliding names get their own columns", async function () {
216
+ const tables = {
217
+ sys_ui_list: {
218
+ records: {
219
+ "csm_consumer.Default view (aaaaaaaa)": { name: "csm_consumer.Default view (aaaaaaaa)", sys_id: LIST_A, files: [{ name: "name", type: "txt", content: "csm_consumer" }] },
220
+ "csm_consumer.Default view (bbbbbbbb)": { name: "csm_consumer.Default view (bbbbbbbb)", sys_id: LIST_B, files: [{ name: "name", type: "txt", content: "csm_consumer" }] },
221
+ },
222
+ },
223
+ };
224
+ const fake = fakeReader({
225
+ sys_ui_list_element: [
226
+ { sys_id: id(1), list_id: LIST_A, element: "first_name", position: "0" },
227
+ { sys_id: id(2), list_id: LIST_B, element: "email", position: "0" },
228
+ { sys_id: id(3), list_id: LIST_B, element: "phone", position: "1" },
229
+ ],
230
+ }, PARENT_FIELD);
231
+ await (0, layoutElements_1.fillLayoutElementContent)(tables, { sys_ui_list: [LIST_A, LIST_B] }, fake.read);
232
+ const a = tables.sys_ui_list.records["csm_consumer.Default view (aaaaaaaa)"].files;
233
+ const b = tables.sys_ui_list.records["csm_consumer.Default view (bbbbbbbb)"].files;
234
+ expect(a[a.length - 1]).toEqual({ name: "columns", type: "txt", content: "first_name\n" });
235
+ expect(b[b.length - 1]).toEqual({ name: "columns", type: "txt", content: "email\nphone\n" });
236
+ });
237
+ it("does nothing when no record asked for layout elements", async function () {
238
+ const fake = fakeReader({}, PARENT_FIELD);
239
+ const tables = { sys_ui_list: { records: {} } };
240
+ await (0, layoutElements_1.fillLayoutElementContent)(tables, {}, fake.read);
241
+ expect(fake.calls.length).toBe(0);
242
+ });
243
+ });
244
+ describe("isLayoutElementsFile / isLayoutElementsFilePath", function () {
245
+ it("recognizes the three derived files by table and name", function () {
246
+ expect((0, layoutElements_1.isLayoutElementsFile)("sys_ui_list", "columns", "txt")).toBe(true);
247
+ expect((0, layoutElements_1.isLayoutElementsFile)("sys_ui_section", "fields", "txt")).toBe(true);
248
+ expect((0, layoutElements_1.isLayoutElementsFile)("sys_ui_form", "sections", "txt")).toBe(true);
249
+ expect((0, layoutElements_1.isLayoutElementsFile)("sys_ui_list", "name", "txt")).toBe(false);
250
+ expect((0, layoutElements_1.isLayoutElementsFile)("x_custom", "columns", "txt")).toBe(false);
251
+ });
252
+ it("recognizes a derived file from its path on disk", function () {
253
+ expect((0, layoutElements_1.isLayoutElementsFilePath)("/repo/src/Work/sys_ui_list/x_cadso_work_task.Default view/columns.txt")).toBe(true);
254
+ expect((0, layoutElements_1.isLayoutElementsFilePath)("/repo/src/Work/sys_ui_section/x_cadso_work_task.Default view.Details/fields.txt")).toBe(true);
255
+ expect((0, layoutElements_1.isLayoutElementsFilePath)("/repo/src/Work/sys_ui_form/x_cadso_work_task.Default view/sections.txt")).toBe(true);
256
+ expect((0, layoutElements_1.isLayoutElementsFilePath)("/repo/src/Work/sys_ui_list/x_cadso_work_task.Default view/name.txt")).toBe(false);
257
+ expect((0, layoutElements_1.isLayoutElementsFilePath)("/repo/src/Work/x_custom/rec/columns.txt")).toBe(false);
258
+ });
259
+ });