Repository navigation
Expand file tree
/
Copy pathimport-runner-kept-options.test.ts
More file actions
165 lines (151 loc) · 8.3 KB
/
Copy pathimport-runner-kept-options.test.ts
File metadata and controls
165 lines (151 loc) · 8.3 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* #22183 — `createMissingOptions` reaches the engine write.
*
* The coercion keeps a select / multiselect cell that matches no option, and
* the engine's option check refuses a value outside the options unless the
* write names it in `ExecutionContext.keptOptionValues`. runImport owns the
* hand-over: every write call (and the dry run's `validateData`) carries
* exactly the values the rows it writes kept, and a call whose rows kept
* nothing carries the import's write context unchanged.
*
* The engine's half (the option arms admit a listed value, and only it) is
* pinned in @objectstack/objectql (`import-kept-option-values.test.ts`, which
* also drives this runner through the real engine); this file pins the wiring.
*/
import { describe, it, expect, vi } from 'vitest';
import { runImport, type ImportProtocolLike } from './import-runner';
import type { ExportFieldMeta } from './import-field-meta.js';
type CreateArgs = Parameters<ImportProtocolLike['createData']>[0];
type UpdateArgs = Parameters<ImportProtocolLike['updateData']>[0];
type CreateManyArgs = Parameters<NonNullable<ImportProtocolLike['createManyData']>>[0];
type InsertManyArgs = Parameters<NonNullable<ImportProtocolLike['insertManyData']>>[0];
type ValidateArgs = Parameters<NonNullable<ImportProtocolLike['validateData']>>[0];
const metaMap = new Map<string, ExportFieldMeta>([
['code', { name: 'code', type: 'text' }],
['priority', { name: 'priority', type: 'select', options: [{ label: 'High', value: 'high' }, { label: 'Low', value: 'low' }] }],
['tags', { name: 'tags', type: 'multiselect', options: [{ label: 'Important', value: 'important' }] }],
]);
const baseOpts = {
objectName: 'ticket',
metaMap,
writeMode: 'insert' as const,
matchFields: [] as string[],
dryRun: false,
runAutomations: false,
trimWhitespace: true,
createMissingOptions: true,
skipBlankMatchKey: false,
};
/** Records `{ kind, rows, context }` for every call that writes or previews. */
function makeProvider(shape: 'inline' | 'createMany' | 'insertMany' | 'update' | 'validate') {
const calls: Array<{ kind: string; rows: Array<Record<string, unknown>>; context: any }> = [];
let idc = 0;
const p: ImportProtocolLike = {
findData: vi.fn(async () => (shape === 'update' ? [{ id: 'existing', code: 'k' }] : [])),
createData: vi.fn(async (args: CreateArgs) => {
calls.push({ kind: 'createData', rows: [args.data as Record<string, unknown>], context: args.context });
return { id: `d${++idc}`, ...(args.data as object) };
}),
updateData: vi.fn(async (args: UpdateArgs) => {
calls.push({ kind: 'updateData', rows: [args.data as Record<string, unknown>], context: args.context });
return { id: args.id, ...(args.data as object) };
}),
};
if (shape === 'createMany') {
p.createManyData = vi.fn(async (args: CreateManyArgs) => {
calls.push({ kind: 'createManyData', rows: args.records as Array<Record<string, unknown>>, context: args.context });
return { records: args.records.map((r: any) => ({ id: `d${++idc}`, ...r })) };
});
}
if (shape === 'insertMany') {
p.insertManyData = vi.fn(async (args: InsertManyArgs) => {
calls.push({ kind: 'insertManyData', rows: args.records as Array<Record<string, unknown>>, context: args.context });
return { outcomes: args.records.map((r: any) => ({ ok: true, record: { id: `d${++idc}`, ...r } })) } as any;
});
// `bulkWrite` needs the all-or-nothing primitive beside the partial one.
p.createManyData = vi.fn(async (args: CreateManyArgs) => {
calls.push({ kind: 'createManyData', rows: args.records as Array<Record<string, unknown>>, context: args.context });
return { records: args.records.map((r: any) => ({ id: `d${++idc}`, ...r })) };
});
}
if (shape === 'validate') {
p.validateData = vi.fn(async (args: ValidateArgs) => {
const rows = (Array.isArray(args.data) ? args.data : [args.data]) as Array<Record<string, unknown>>;
calls.push({ kind: 'validateData', rows, context: args.context });
return { results: rows.map(() => ({ valid: true, errors: [] })) } as any;
});
}
return { p, calls };
}
const ROWS = [
{ code: 'a', priority: 'Bogus' }, // kept: priority
{ code: 'b', priority: 'High' }, // matched: nothing kept
{ code: 'c', tags: 'Important, Other' }, // kept: tags
];
describe('runImport — createMissingOptions hands the kept values to the write (#22183)', () => {
it('a per-row create carries exactly that row\'s kept values, and a row that kept nothing carries none', async () => {
const { p, calls } = makeProvider('inline');
const summary = await runImport({ ...baseOpts, p, rows: ROWS, context: { userId: 'u1' } });
expect(summary.created).toBe(3);
expect(calls.map((c) => c.kind)).toEqual(['createData', 'createData', 'createData']);
expect(calls[0].context.keptOptionValues).toEqual({ priority: ['Bogus'] });
expect(calls[1].context).not.toHaveProperty('keptOptionValues');
expect(calls[2].context.keptOptionValues).toEqual({ tags: ['Other'] });
// The rest of the write context is the import's own, untouched.
for (const c of calls) {
expect(c.context.userId).toBe('u1');
expect(c.context.skipAutomations).toBe(true);
}
expect(calls[0].rows[0]).toMatchObject({ priority: 'Bogus' });
expect(calls[2].rows[0]).toMatchObject({ tags: ['important', 'Other'] });
});
it('a batched create (createManyData) carries the union of its rows\' kept values', async () => {
const { p, calls } = makeProvider('createMany');
const summary = await runImport({ ...baseOpts, p, rows: [...ROWS, { code: 'd', priority: 'Bogus' }] });
expect(summary.created).toBe(4);
expect(calls.map((c) => c.kind)).toEqual(['createManyData']);
expect(calls[0].rows).toHaveLength(4);
expect(calls[0].context.keptOptionValues).toEqual({ priority: ['Bogus'], tags: ['Other'] });
});
it('a partial-success batch (insertManyData) carries the union of its rows\' kept values', async () => {
const { p, calls } = makeProvider('insertMany');
const summary = await runImport({ ...baseOpts, p, rows: ROWS });
expect(summary.created).toBe(3);
expect(calls.map((c) => c.kind)).toEqual(['insertManyData']);
expect(calls[0].context.keptOptionValues).toEqual({ priority: ['Bogus'], tags: ['Other'] });
});
it('an update carries the row\'s kept values', async () => {
const { p, calls } = makeProvider('update');
const summary = await runImport({
...baseOpts, p, writeMode: 'update', matchFields: ['code'], rows: [{ code: 'k', priority: 'Bogus' }],
});
expect(summary.updated).toBe(1);
expect(calls.map((c) => c.kind)).toEqual(['updateData']);
expect(calls[0].context.keptOptionValues).toEqual({ priority: ['Bogus'] });
});
it('the dry run asks validateData with the context the batch write would carry: the union of its rows\' kept values', async () => {
// [#26684] The dry run previews the create rows as the batch the commit
// writes them in, so its one call carries that batch call's context.
const { p, calls } = makeProvider('validate');
const summary = await runImport({ ...baseOpts, p, dryRun: true, rows: ROWS });
expect(summary.created).toBe(3);
expect(calls.map((c) => c.kind)).toEqual(['validateData']);
expect(calls[0].rows).toHaveLength(3);
expect(calls[0].context.keptOptionValues).toEqual({ priority: ['Bogus'], tags: ['Other'] });
});
it('a dry run whose rows kept nothing carries the import\'s write context unchanged', async () => {
const { p, calls } = makeProvider('validate');
await runImport({ ...baseOpts, p, dryRun: true, rows: [{ code: 'b', priority: 'High' }] });
expect(calls).toHaveLength(1);
expect(calls[0].context).not.toHaveProperty('keptOptionValues');
});
it('with the option off, an unmatched cell fails at coercion and no write carries the key', async () => {
const { p, calls } = makeProvider('inline');
const summary = await runImport({ ...baseOpts, p, createMissingOptions: false, rows: ROWS });
expect(summary.created).toBe(1);
expect(summary.results.filter((r) => !r.ok).map((r) => r.code)).toEqual(['invalid_option', 'invalid_option']);
expect(calls).toHaveLength(1);
expect(calls[0].context).not.toHaveProperty('keptOptionValues');
});
});