@fugood/bricks-ctor 2.25.0-beta.42 → 2.25.0-beta.43
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.
- package/compile/__tests__/config-diff.test.js +72 -0
- package/compile/__tests__/index.test.js +61 -1
- package/compile/config-diff.ts +108 -0
- package/compile/index.ts +23 -1
- package/package.json +7 -3
- package/skills/bricks-ctor/SKILL.md +2 -0
- package/skills/bricks-ctor/references/architecture-patterns.md +6 -0
- package/skills/bricks-ctor/references/source-editing-tools.md +81 -0
- package/tools/mcp-env.ts +13 -0
- package/tools/mcp-server.ts +8 -0
- package/tools/mcp-tools/__tests__/data-calc-editing.test.js +516 -0
- package/tools/mcp-tools/__tests__/entry-editing.test.js +836 -0
- package/tools/mcp-tools/__tests__/mcp-env.test.js +21 -0
- package/tools/mcp-tools/_verify.ts +45 -0
- package/tools/mcp-tools/compile.ts +2 -0
- package/tools/mcp-tools/data-calc-editing.ts +1425 -0
- package/tools/mcp-tools/entry-editing.ts +2398 -0
- package/tools/postinstall.ts +80 -3
- package/utils/data.ts +3 -5
|
@@ -0,0 +1,516 @@
|
|
|
1
|
+
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
2
|
+
import os from 'node:os'
|
|
3
|
+
import path from 'node:path'
|
|
4
|
+
|
|
5
|
+
jest.mock('oxfmt', () => ({
|
|
6
|
+
format: async (_file, source) => ({ code: source, errors: [] }),
|
|
7
|
+
}))
|
|
8
|
+
|
|
9
|
+
import { register, __test__ } from '../data-calc-editing'
|
|
10
|
+
|
|
11
|
+
const writeFixtureProject = async () => {
|
|
12
|
+
const projectDir = await mkdtemp(path.join(os.tmpdir(), 'bricks-data-calc-editing-'))
|
|
13
|
+
const subspaceDir = path.join(projectDir, 'subspaces/subspace-0')
|
|
14
|
+
await mkdir(subspaceDir, { recursive: true })
|
|
15
|
+
await writeFile(
|
|
16
|
+
path.join(subspaceDir, 'data.ts'),
|
|
17
|
+
`import type { Data } from 'bricks-ctor'
|
|
18
|
+
import { makeId } from 'bricks-ctor'
|
|
19
|
+
|
|
20
|
+
export const dPrice: Data = {
|
|
21
|
+
__typename: 'Data',
|
|
22
|
+
id: makeId('data'),
|
|
23
|
+
alias: 'price',
|
|
24
|
+
title: 'Price',
|
|
25
|
+
type: 'number',
|
|
26
|
+
value: 5,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const dQuantity: Data = {
|
|
30
|
+
__typename: 'Data',
|
|
31
|
+
id: makeId('data'),
|
|
32
|
+
alias: 'quantity',
|
|
33
|
+
title: 'Quantity',
|
|
34
|
+
type: 'number',
|
|
35
|
+
value: 2,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export const dTotal: Data = {
|
|
39
|
+
__typename: 'Data',
|
|
40
|
+
id: makeId('data'),
|
|
41
|
+
alias: 'total',
|
|
42
|
+
title: 'Total',
|
|
43
|
+
type: 'number',
|
|
44
|
+
value: 0,
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export const dError: Data = {
|
|
48
|
+
__typename: 'Data',
|
|
49
|
+
id: makeId('data'),
|
|
50
|
+
alias: 'error',
|
|
51
|
+
title: 'Error',
|
|
52
|
+
type: 'string',
|
|
53
|
+
value: '',
|
|
54
|
+
}
|
|
55
|
+
`,
|
|
56
|
+
)
|
|
57
|
+
await writeFile(
|
|
58
|
+
path.join(subspaceDir, 'index.ts'),
|
|
59
|
+
`import type { Subspace } from 'bricks-ctor'
|
|
60
|
+
|
|
61
|
+
export default {
|
|
62
|
+
__typename: 'Subspace',
|
|
63
|
+
id: 'SUBSPACE_TEST',
|
|
64
|
+
title: 'Main',
|
|
65
|
+
layout: { width: 96, height: 54 },
|
|
66
|
+
canvases: [],
|
|
67
|
+
rootCanvas: undefined,
|
|
68
|
+
animations: [],
|
|
69
|
+
bricks: [],
|
|
70
|
+
generators: [],
|
|
71
|
+
data: Object.values(data),
|
|
72
|
+
dataRouting: [],
|
|
73
|
+
dataCalculation: [],
|
|
74
|
+
} as Subspace
|
|
75
|
+
`,
|
|
76
|
+
)
|
|
77
|
+
return projectDir
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const readProjectFile = (projectDir, relPath) => readFile(path.join(projectDir, relPath), 'utf8')
|
|
81
|
+
|
|
82
|
+
const readAudit = async (projectDir) =>
|
|
83
|
+
(await readFile(path.join(projectDir, '.bricks/edits.jsonl'), 'utf8'))
|
|
84
|
+
.trim()
|
|
85
|
+
.split('\n')
|
|
86
|
+
.map((line) => JSON.parse(line))
|
|
87
|
+
|
|
88
|
+
describe('ctor MCP data-calc editing tools', () => {
|
|
89
|
+
let projectDir
|
|
90
|
+
|
|
91
|
+
afterEach(async () => {
|
|
92
|
+
if (projectDir) await rm(projectDir, { recursive: true, force: true })
|
|
93
|
+
projectDir = null
|
|
94
|
+
})
|
|
95
|
+
|
|
96
|
+
test('registers the four data-calc MCP tools with descriptions', () => {
|
|
97
|
+
const calls = []
|
|
98
|
+
register(
|
|
99
|
+
{
|
|
100
|
+
tool(...args) {
|
|
101
|
+
calls.push(args)
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
'/tmp/project',
|
|
105
|
+
)
|
|
106
|
+
|
|
107
|
+
expect(calls.map(([name]) => name)).toEqual([
|
|
108
|
+
'new_data_calc',
|
|
109
|
+
'edit_data_calc',
|
|
110
|
+
'edit_data_calc_io',
|
|
111
|
+
'remove_data_calc',
|
|
112
|
+
])
|
|
113
|
+
expect(
|
|
114
|
+
calls.every(([, description]) => typeof description === 'string' && description.length > 20),
|
|
115
|
+
).toBe(true)
|
|
116
|
+
expect(calls[0][2].code.description).toContain('export function main')
|
|
117
|
+
expect(calls[2][2].data.description).toContain('{ ref')
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
test('new_data_calc creates script, sandbox, index, and minimal subspace wiring', async () => {
|
|
121
|
+
projectDir = await writeFixtureProject()
|
|
122
|
+
|
|
123
|
+
const result = await __test__.newDataCalc(projectDir, {
|
|
124
|
+
alias: 'totalCalc',
|
|
125
|
+
title: 'Total Calc',
|
|
126
|
+
code: 'return { total: inputs.price * inputs.quantity }',
|
|
127
|
+
inputs: [
|
|
128
|
+
{ key: 'price', data: 'price' },
|
|
129
|
+
{ key: 'quantity', data: 'quantity', trigger: false },
|
|
130
|
+
],
|
|
131
|
+
outputs: [{ key: 'total', data: 'total' }],
|
|
132
|
+
output: 'total',
|
|
133
|
+
error: 'error',
|
|
134
|
+
verify: false,
|
|
135
|
+
})
|
|
136
|
+
|
|
137
|
+
expect(result.outcome).toBe('ok')
|
|
138
|
+
expect(result.calc).toBe('total-calc')
|
|
139
|
+
const calc = await readProjectFile(
|
|
140
|
+
projectDir,
|
|
141
|
+
'subspaces/subspace-0/data-calc/data-calculation-total-calc.ts',
|
|
142
|
+
)
|
|
143
|
+
expect(calc).toMatch(/id:\s*makeId\(["']property_bank_calc["'], ["']totalCalc["']\)/)
|
|
144
|
+
expect(calc).toMatch(
|
|
145
|
+
/code:\s*await readFile\(new URL\(["']\.\/data-calculation-total-calc\.sandbox\.js["']/,
|
|
146
|
+
)
|
|
147
|
+
expect(calc).toContain('data: () => data.dPrice')
|
|
148
|
+
expect(calc).toContain('trigger: false')
|
|
149
|
+
expect(calc).toContain('output: () => data.dTotal')
|
|
150
|
+
expect(calc).toContain('error: () => data.dError')
|
|
151
|
+
const sandbox = await readProjectFile(
|
|
152
|
+
projectDir,
|
|
153
|
+
'subspaces/subspace-0/data-calc/data-calculation-total-calc.sandbox.js',
|
|
154
|
+
)
|
|
155
|
+
expect(sandbox).toContain('export function main()')
|
|
156
|
+
expect(sandbox).toContain('return { total: inputs.price * inputs.quantity }')
|
|
157
|
+
const index = await readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/index.ts')
|
|
158
|
+
expect(index).toContain(
|
|
159
|
+
"import { dataCalculation as dataCalculation0 } from './data-calculation-total-calc'",
|
|
160
|
+
)
|
|
161
|
+
const subspaceIndex = await readProjectFile(projectDir, 'subspaces/subspace-0/index.ts')
|
|
162
|
+
expect(subspaceIndex).toMatch(/import \{ dataCalculation \} from ["']\.\/data-calc["']/)
|
|
163
|
+
expect(subspaceIndex).toMatch(/dataCalculation\s*\n\s*} as Subspace/)
|
|
164
|
+
const audit = await readAudit(projectDir)
|
|
165
|
+
expect(audit.at(-1).summary).toContain('created data calc total-calc')
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
test('records audit provenance from env', async () => {
|
|
169
|
+
projectDir = await writeFixtureProject()
|
|
170
|
+
const previousSessionId = process.env.BRICKS_CTOR_SESSION_ID
|
|
171
|
+
const previousAgentId = process.env.BRICKS_CTOR_AGENT_ID
|
|
172
|
+
process.env.BRICKS_CTOR_SESSION_ID = 'session-1'
|
|
173
|
+
process.env.BRICKS_CTOR_AGENT_ID = 'agent-1'
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
const result = await __test__.newDataCalc(projectDir, {
|
|
177
|
+
alias: 'auditCalc',
|
|
178
|
+
verify: false,
|
|
179
|
+
})
|
|
180
|
+
|
|
181
|
+
expect(result.outcome).toBe('ok')
|
|
182
|
+
const audit = await readAudit(projectDir)
|
|
183
|
+
expect(audit.at(-1).provenance).toEqual({
|
|
184
|
+
session: 'session-1',
|
|
185
|
+
agent: 'agent-1',
|
|
186
|
+
})
|
|
187
|
+
} finally {
|
|
188
|
+
if (previousSessionId === undefined) delete process.env.BRICKS_CTOR_SESSION_ID
|
|
189
|
+
else process.env.BRICKS_CTOR_SESSION_ID = previousSessionId
|
|
190
|
+
if (previousAgentId === undefined) delete process.env.BRICKS_CTOR_AGENT_ID
|
|
191
|
+
else process.env.BRICKS_CTOR_AGENT_ID = previousAgentId
|
|
192
|
+
}
|
|
193
|
+
})
|
|
194
|
+
|
|
195
|
+
test('edit_data_calc updates scalars, refs, and migrates inline code to sandbox form', async () => {
|
|
196
|
+
projectDir = await writeFixtureProject()
|
|
197
|
+
const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
|
|
198
|
+
await mkdir(dataCalcDir, { recursive: true })
|
|
199
|
+
await writeFile(
|
|
200
|
+
path.join(dataCalcDir, 'data-calculation-inline.ts'),
|
|
201
|
+
`import type { DataCalculationScript } from 'bricks-ctor'
|
|
202
|
+
import { makeId } from 'bricks-ctor'
|
|
203
|
+
import * as data from '../data'
|
|
204
|
+
|
|
205
|
+
export const dataCalculation: DataCalculationScript = {
|
|
206
|
+
__typename: 'DataCalculationScript',
|
|
207
|
+
id: makeId('property_bank_calc'),
|
|
208
|
+
alias: 'inline',
|
|
209
|
+
code: 'return inputs.price',
|
|
210
|
+
enableAsync: false,
|
|
211
|
+
inputs: [],
|
|
212
|
+
output: null,
|
|
213
|
+
outputs: [],
|
|
214
|
+
error: null,
|
|
215
|
+
}
|
|
216
|
+
`,
|
|
217
|
+
)
|
|
218
|
+
|
|
219
|
+
const result = await __test__.editDataCalc(projectDir, {
|
|
220
|
+
subspace: 0,
|
|
221
|
+
calc: 'inline',
|
|
222
|
+
set: {
|
|
223
|
+
title: 'Edited Inline',
|
|
224
|
+
enableAsync: true,
|
|
225
|
+
output: 'total',
|
|
226
|
+
error: 'error',
|
|
227
|
+
},
|
|
228
|
+
code: 'return { total: inputs.price }',
|
|
229
|
+
verify: false,
|
|
230
|
+
})
|
|
231
|
+
|
|
232
|
+
expect(result.outcome).toBe('ok')
|
|
233
|
+
const calc = await readProjectFile(
|
|
234
|
+
projectDir,
|
|
235
|
+
'subspaces/subspace-0/data-calc/data-calculation-inline.ts',
|
|
236
|
+
)
|
|
237
|
+
expect(calc).toMatch(/import \{ readFile \} from ["']node:fs\/promises["']/)
|
|
238
|
+
expect(calc).toMatch(/title:\s*["']Edited Inline["']/)
|
|
239
|
+
expect(calc).toContain('enableAsync: true')
|
|
240
|
+
expect(calc).toMatch(
|
|
241
|
+
/code:\s*await readFile\(new URL\(["']\.\/data-calculation-inline\.sandbox\.js["']/,
|
|
242
|
+
)
|
|
243
|
+
expect(calc).toContain('output: () => data.dTotal')
|
|
244
|
+
expect(calc).toContain('error: () => data.dError')
|
|
245
|
+
const sandbox = await readProjectFile(
|
|
246
|
+
projectDir,
|
|
247
|
+
'subspaces/subspace-0/data-calc/data-calculation-inline.sandbox.js',
|
|
248
|
+
)
|
|
249
|
+
expect(sandbox).toContain('return { total: inputs.price }')
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
test('edit_data_calc_io edits inputs and outputs with input-key uniqueness', async () => {
|
|
253
|
+
projectDir = await writeFixtureProject()
|
|
254
|
+
await __test__.newDataCalc(projectDir, {
|
|
255
|
+
alias: 'ioCalc',
|
|
256
|
+
inputs: [{ key: 'price', data: 'price' }],
|
|
257
|
+
outputs: [{ key: 'total', data: 'total' }],
|
|
258
|
+
verify: false,
|
|
259
|
+
})
|
|
260
|
+
|
|
261
|
+
const duplicate = await __test__.editDataCalcIo(projectDir, {
|
|
262
|
+
subspace: 0,
|
|
263
|
+
calc: 'io-calc',
|
|
264
|
+
field: 'inputs',
|
|
265
|
+
op: 'add',
|
|
266
|
+
key: 'price',
|
|
267
|
+
data: 'quantity',
|
|
268
|
+
verify: false,
|
|
269
|
+
})
|
|
270
|
+
expect(duplicate.outcome).toBe('error')
|
|
271
|
+
expect(duplicate.error.code).toBe('duplicate_key')
|
|
272
|
+
|
|
273
|
+
const addInput = await __test__.editDataCalcIo(projectDir, {
|
|
274
|
+
subspace: 0,
|
|
275
|
+
calc: 'io-calc',
|
|
276
|
+
field: 'inputs',
|
|
277
|
+
op: 'add',
|
|
278
|
+
key: 'quantity',
|
|
279
|
+
data: 'quantity',
|
|
280
|
+
trigger: false,
|
|
281
|
+
verify: false,
|
|
282
|
+
})
|
|
283
|
+
expect(addInput.outcome).toBe('ok')
|
|
284
|
+
|
|
285
|
+
const fanOut = await __test__.editDataCalcIo(projectDir, {
|
|
286
|
+
subspace: 0,
|
|
287
|
+
calc: 'io-calc',
|
|
288
|
+
field: 'outputs',
|
|
289
|
+
op: 'add',
|
|
290
|
+
key: 'total',
|
|
291
|
+
data: 'total',
|
|
292
|
+
verify: false,
|
|
293
|
+
})
|
|
294
|
+
expect(fanOut.outcome).toBe('ok')
|
|
295
|
+
|
|
296
|
+
const calc = await readProjectFile(
|
|
297
|
+
projectDir,
|
|
298
|
+
'subspaces/subspace-0/data-calc/data-calculation-io-calc.ts',
|
|
299
|
+
)
|
|
300
|
+
expect(calc).toMatch(/key:\s*["']quantity["']/)
|
|
301
|
+
expect(calc).toContain('trigger: false')
|
|
302
|
+
expect((calc.match(/key:\s*["']total["']/g) || []).length).toBe(2)
|
|
303
|
+
})
|
|
304
|
+
|
|
305
|
+
test('new_data_calc wraps top-level await bodies in an async main', async () => {
|
|
306
|
+
projectDir = await writeFixtureProject()
|
|
307
|
+
|
|
308
|
+
const result = await __test__.newDataCalc(projectDir, {
|
|
309
|
+
alias: 'asyncCalc',
|
|
310
|
+
enableAsync: true,
|
|
311
|
+
code: 'const value = await Promise.resolve(inputs.price)\nreturn value',
|
|
312
|
+
inputs: [{ key: 'price', data: 'price' }],
|
|
313
|
+
verify: false,
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
expect(result.outcome).toBe('ok')
|
|
317
|
+
const sandbox = await readProjectFile(
|
|
318
|
+
projectDir,
|
|
319
|
+
'subspaces/subspace-0/data-calc/data-calculation-async-calc.sandbox.js',
|
|
320
|
+
)
|
|
321
|
+
expect(sandbox).toContain('export async function main()')
|
|
322
|
+
})
|
|
323
|
+
|
|
324
|
+
test('new_data_calc rejects sandbox code that does not parse', async () => {
|
|
325
|
+
projectDir = await writeFixtureProject()
|
|
326
|
+
|
|
327
|
+
const result = await __test__.newDataCalc(projectDir, {
|
|
328
|
+
alias: 'broken',
|
|
329
|
+
code: 'return {',
|
|
330
|
+
verify: false,
|
|
331
|
+
})
|
|
332
|
+
|
|
333
|
+
expect(result.outcome).toBe('error')
|
|
334
|
+
expect(result.error.code).toBe('invalid_code')
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
test('edit_data_calc set routes inputs/outputs through the IO builder and guards required fields', async () => {
|
|
338
|
+
projectDir = await writeFixtureProject()
|
|
339
|
+
await __test__.newDataCalc(projectDir, { alias: 'setCalc', verify: false })
|
|
340
|
+
|
|
341
|
+
const ioViaSet = await __test__.editDataCalc(projectDir, {
|
|
342
|
+
subspace: 0,
|
|
343
|
+
calc: 'set-calc',
|
|
344
|
+
set: { inputs: [{ key: 'price', data: 'price' }] },
|
|
345
|
+
verify: false,
|
|
346
|
+
})
|
|
347
|
+
expect(ioViaSet.outcome).toBe('ok')
|
|
348
|
+
const calc = await readProjectFile(
|
|
349
|
+
projectDir,
|
|
350
|
+
'subspaces/subspace-0/data-calc/data-calculation-set-calc.ts',
|
|
351
|
+
)
|
|
352
|
+
expect(calc).toContain('data: () => data.dPrice')
|
|
353
|
+
|
|
354
|
+
const codeViaSet = await __test__.editDataCalc(projectDir, {
|
|
355
|
+
subspace: 0,
|
|
356
|
+
calc: 'set-calc',
|
|
357
|
+
set: { code: 'return 1' },
|
|
358
|
+
verify: false,
|
|
359
|
+
})
|
|
360
|
+
expect(codeViaSet.outcome).toBe('error')
|
|
361
|
+
expect(codeViaSet.error.code).toBe('invalid_field')
|
|
362
|
+
|
|
363
|
+
const badUnset = await __test__.editDataCalc(projectDir, {
|
|
364
|
+
subspace: 0,
|
|
365
|
+
calc: 'set-calc',
|
|
366
|
+
unset: ['inputs'],
|
|
367
|
+
verify: false,
|
|
368
|
+
})
|
|
369
|
+
expect(badUnset.outcome).toBe('error')
|
|
370
|
+
expect(badUnset.error.code).toBe('invalid_field')
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
test('subspace+calc addressing tolerates non-Script calc files while scanning', async () => {
|
|
374
|
+
projectDir = await writeFixtureProject()
|
|
375
|
+
const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
|
|
376
|
+
await mkdir(dataCalcDir, { recursive: true })
|
|
377
|
+
await writeFile(
|
|
378
|
+
path.join(dataCalcDir, 'data-calculation-0.ts'),
|
|
379
|
+
`import type { DataCalculationMap } from 'bricks-ctor'
|
|
380
|
+
|
|
381
|
+
export const dataCalculation: DataCalculationMap = {
|
|
382
|
+
__typename: 'DataCalculationMap',
|
|
383
|
+
id: 'PROPERTY_BANK_COMMAND_MAP_MAP',
|
|
384
|
+
nodes: [],
|
|
385
|
+
editorInfo: [],
|
|
386
|
+
}
|
|
387
|
+
`,
|
|
388
|
+
)
|
|
389
|
+
await __test__.newDataCalc(projectDir, { alias: 'realCalc', verify: false })
|
|
390
|
+
|
|
391
|
+
const result = await __test__.editDataCalc(projectDir, {
|
|
392
|
+
subspace: 0,
|
|
393
|
+
calc: 'realCalc',
|
|
394
|
+
set: { title: 'Real' },
|
|
395
|
+
verify: false,
|
|
396
|
+
})
|
|
397
|
+
|
|
398
|
+
expect(result.outcome).toBe('ok')
|
|
399
|
+
expect(result.file).toBe('subspaces/subspace-0/data-calc/data-calculation-real-calc.ts')
|
|
400
|
+
})
|
|
401
|
+
|
|
402
|
+
test('new_data_calc warns instead of overwriting a non-empty inline dataCalculation', async () => {
|
|
403
|
+
projectDir = await writeFixtureProject()
|
|
404
|
+
await writeFile(
|
|
405
|
+
path.join(projectDir, 'subspaces/subspace-0/index.ts'),
|
|
406
|
+
`import type { Subspace } from 'bricks-ctor'
|
|
407
|
+
|
|
408
|
+
export default {
|
|
409
|
+
__typename: 'Subspace',
|
|
410
|
+
id: 'SUBSPACE_TEST',
|
|
411
|
+
title: 'Main',
|
|
412
|
+
dataCalculation: [{ __typename: 'DataCalculationScript' }],
|
|
413
|
+
} as Subspace
|
|
414
|
+
`,
|
|
415
|
+
)
|
|
416
|
+
|
|
417
|
+
const result = await __test__.newDataCalc(projectDir, { alias: 'warned', verify: false })
|
|
418
|
+
|
|
419
|
+
expect(result.outcome).toBe('ok')
|
|
420
|
+
expect(result.warnings?.[0]).toContain('non-empty dataCalculation')
|
|
421
|
+
const index = await readProjectFile(projectDir, 'subspaces/subspace-0/index.ts')
|
|
422
|
+
expect(index).not.toMatch(/import \{ dataCalculation \}/)
|
|
423
|
+
expect(index).toContain("dataCalculation: [{ __typename: 'DataCalculationScript' }]")
|
|
424
|
+
})
|
|
425
|
+
|
|
426
|
+
test('remove_data_calc ignores non-sibling sandbox filenames in code', async () => {
|
|
427
|
+
projectDir = await writeFixtureProject()
|
|
428
|
+
const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
|
|
429
|
+
await mkdir(dataCalcDir, { recursive: true })
|
|
430
|
+
await writeFile(path.join(projectDir, 'evil.sandbox.js'), 'export function main() {}\n')
|
|
431
|
+
await writeFile(
|
|
432
|
+
path.join(dataCalcDir, 'data-calculation-escape.ts'),
|
|
433
|
+
`import type { DataCalculationScript } from 'bricks-ctor'
|
|
434
|
+
import { makeId } from 'bricks-ctor'
|
|
435
|
+
import { readFile } from 'node:fs/promises'
|
|
436
|
+
|
|
437
|
+
export const dataCalculation: DataCalculationScript = {
|
|
438
|
+
__typename: 'DataCalculationScript',
|
|
439
|
+
id: makeId('property_bank_calc'),
|
|
440
|
+
alias: 'escape',
|
|
441
|
+
code: await readFile(new URL('../../../evil.sandbox.js', import.meta.url), 'utf8'),
|
|
442
|
+
enableAsync: false,
|
|
443
|
+
inputs: [],
|
|
444
|
+
output: null,
|
|
445
|
+
outputs: [],
|
|
446
|
+
error: null,
|
|
447
|
+
}
|
|
448
|
+
`,
|
|
449
|
+
)
|
|
450
|
+
|
|
451
|
+
const result = await __test__.removeDataCalc(projectDir, {
|
|
452
|
+
subspace: 0,
|
|
453
|
+
calc: 'escape',
|
|
454
|
+
verify: false,
|
|
455
|
+
})
|
|
456
|
+
|
|
457
|
+
expect(result.outcome).toBe('ok')
|
|
458
|
+
await expect(readProjectFile(projectDir, 'evil.sandbox.js')).resolves.toContain('main')
|
|
459
|
+
})
|
|
460
|
+
|
|
461
|
+
test('remove_data_calc deletes calc files and regenerates index', async () => {
|
|
462
|
+
projectDir = await writeFixtureProject()
|
|
463
|
+
await __test__.newDataCalc(projectDir, {
|
|
464
|
+
alias: 'removeMe',
|
|
465
|
+
code: 'return inputs.price',
|
|
466
|
+
verify: false,
|
|
467
|
+
})
|
|
468
|
+
|
|
469
|
+
const result = await __test__.removeDataCalc(projectDir, {
|
|
470
|
+
subspace: 0,
|
|
471
|
+
calc: 'remove-me',
|
|
472
|
+
verify: false,
|
|
473
|
+
})
|
|
474
|
+
|
|
475
|
+
expect(result.outcome).toBe('ok')
|
|
476
|
+
await expect(
|
|
477
|
+
readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/data-calculation-remove-me.ts'),
|
|
478
|
+
).rejects.toThrow()
|
|
479
|
+
await expect(
|
|
480
|
+
readProjectFile(
|
|
481
|
+
projectDir,
|
|
482
|
+
'subspaces/subspace-0/data-calc/data-calculation-remove-me.sandbox.js',
|
|
483
|
+
),
|
|
484
|
+
).rejects.toThrow()
|
|
485
|
+
const index = await readProjectFile(projectDir, 'subspaces/subspace-0/data-calc/index.ts')
|
|
486
|
+
expect(index).not.toContain('remove-me')
|
|
487
|
+
})
|
|
488
|
+
|
|
489
|
+
test('DataCalculationMap returns fallback recommendation', async () => {
|
|
490
|
+
projectDir = await writeFixtureProject()
|
|
491
|
+
const dataCalcDir = path.join(projectDir, 'subspaces/subspace-0/data-calc')
|
|
492
|
+
await mkdir(dataCalcDir, { recursive: true })
|
|
493
|
+
await writeFile(
|
|
494
|
+
path.join(dataCalcDir, 'data-calculation-map.ts'),
|
|
495
|
+
`import type { DataCalculationMap } from 'bricks-ctor'
|
|
496
|
+
|
|
497
|
+
export const dataCalculation: DataCalculationMap = {
|
|
498
|
+
__typename: 'DataCalculationMap',
|
|
499
|
+
id: 'PROPERTY_BANK_COMMAND_MAP_MAP',
|
|
500
|
+
nodes: [],
|
|
501
|
+
editorInfo: [],
|
|
502
|
+
}
|
|
503
|
+
`,
|
|
504
|
+
)
|
|
505
|
+
|
|
506
|
+
const result = await __test__.editDataCalc(projectDir, {
|
|
507
|
+
file: 'subspaces/subspace-0/data-calc/data-calculation-map.ts',
|
|
508
|
+
set: { title: 'Map' },
|
|
509
|
+
verify: false,
|
|
510
|
+
})
|
|
511
|
+
|
|
512
|
+
expect(result.outcome).toBe('fallback_recommended')
|
|
513
|
+
expect(result.isError).toBe(false)
|
|
514
|
+
expect(result.error.message).toContain('DataCalculationMap is out of scope')
|
|
515
|
+
})
|
|
516
|
+
})
|