@fugood/bricks-ctor 2.25.0-beta.42 → 2.25.0-beta.45

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,81 @@
1
+ # MCP Source-Editing Tools
2
+
3
+ The project-local `bricks-ctor` MCP server exposes tools that edit `subspaces/**` via
4
+ surgical AST edits, keeping files in the standard generated style. Prefer them over
5
+ hand-editing entry files; they validate references, manage imports, compile-verify, and
6
+ record every operation in `.bricks/edits.jsonl`.
7
+
8
+ ## Entry tools (bricks.ts / generators.ts / canvases.ts / data.ts / animations.ts)
9
+
10
+ | Tool | Purpose |
11
+ |------|---------|
12
+ | `new_entry` | Create a standard entry skeleton (`file`, `type`, `templateKey`, `alias`, optional initial `set`/`events`) |
13
+ | `edit_entry` | Set/unset dotted paths on an entry: `title`, `property.url`, `outlets.response`, `value`, `switches[0].property.text`, … |
14
+ | `edit_events` | Add/remove/replace/move/clear EventAction items in `events.<eventKey>` |
15
+ | `edit_canvas_items` | Add/replace/remove/move brick items on a Canvas `items` array (frames need numeric x/y/width/height) |
16
+ | `edit_switches` | Add/replace/remove/move switches (id, title, conds, override, disabled, break) |
17
+ | `remove_entry` | Delete an entry; cascades same-subspace references by default, `strict: true` refuses and lists sites |
18
+
19
+ Addressing: `{ file, entry }` (export const name) is primary; `id` works as a global
20
+ fallback (omit `file`). `edit_entry`/`edit_events` accept a `switch` parameter (switch id
21
+ or index) to edit the facets inside one switch — `edit_switches` owns only the array and
22
+ the conds/override shell.
23
+
24
+ ### Event actions
25
+
26
+ ```jsonc
27
+ {
28
+ "handler": "system", // or { "ref": "brickOrGeneratorRef" } or { "subspace": "SUBSPACE_id" }
29
+ "name": "CHANGE_CANVAS",
30
+ "params": { "canvasId": { "ref": "mainCanvas" } }, // template params, OR:
31
+ "dataParams": { "someData": 5 }, // PROPERTY_BANK-style params
32
+ "waitAsync": false
33
+ }
34
+ ```
35
+
36
+ System actions derive their `as SystemAction...` cast automatically; entity-action casts
37
+ are added only when passed via `cast`. The compiled source form
38
+ `{ handler, action: { name, params: [...], dataParams: [...] }, waitAsync }` is also
39
+ accepted, and `{ "expr": "<raw EventAction>" }` is the escape hatch for shapes the
40
+ structured form cannot express. Handler refs must resolve to a brick or generator.
41
+ `PROPERTY_BANK_EXPRESSION` expressions are validated against the runtime fold rules at
42
+ edit time: only expression statements, simple `const`/`let` declarations, and a final
43
+ return inside a zero-arg IIFE evaluate — no `if`/`for`/`while` (use ternaries, or a
44
+ DataCalculationScript for branching logic).
45
+
46
+ ## Data-calc tools (DataCalculationScript only)
47
+
48
+ | Tool | Purpose |
49
+ |------|---------|
50
+ | `new_data_calc` | Create `data-calc/data-calculation-{slug}.ts` + `.sandbox.js`, regenerate `data-calc/index.ts`, wire a minimal subspace root |
51
+ | `edit_data_calc` | Set/unset scalar fields, `output`/`error` refs, replace whole `inputs`/`outputs`, or rewrite the sandbox `code` |
52
+ | `edit_data_calc_io` | Add/remove/replace/clear single `inputs`/`outputs` items (input keys unique; output keys may repeat for fan-out) |
53
+ | `remove_data_calc` | Delete the calc `.ts` + its sandbox file and regenerate the index |
54
+
55
+ Addressing: `{ file }` or `{ subspace, calc }` where `calc` is alias, id, or filename
56
+ slug (subspace defaults to `subspace-0`). Code is canonicalized to the sandbox file form
57
+ (`export function main() { ... }` — wrapped automatically, `async` added when the body
58
+ uses top-level `await`). `DataCalculationMap` (visual node graph) is out of scope and
59
+ returns `fallback_recommended`.
60
+
61
+ ## Value grammar (everywhere a value appears)
62
+
63
+ | You pass | Emits |
64
+ |----------|-------|
65
+ | JSON scalar/array/object | literal |
66
+ | `{ "link": "dataRefOrAlias" }` | `linkData(() => data.dX)` (property data-links) |
67
+ | `{ "ref": "idOrAliasOrVarName", "subspace"?: 1 }` | `() => namespace.varName` getter |
68
+ | `{ "expr": "raw TypeScript" }` | spliced verbatim |
69
+
70
+ References resolve by var name, id, or alias within the target subspace and fail loudly
71
+ when missing or ambiguous — this doubles as reference validation.
72
+
73
+ ## Verification and audit
74
+
75
+ - Every call compile-verifies by default and returns `verify.configChange` — the minimal
76
+ compiled-config delta (path-keyed set/unset ops). Skip with per-call `verify: false`
77
+ or `BRICKS_CTOR_MCP_EDIT_VERIFY=0` when batching, then finish with the `compile` tool.
78
+ - All operations append to `.bricks/edits.jsonl` (gitignored) with inputs, outcomes, and
79
+ touched sites.
80
+ - Non-standard files or entries (hand-written shapes the AST editor cannot safely
81
+ rewrite) return `fallback_recommended` — use plain file edits for those cases only.
@@ -71,6 +71,8 @@ Useful flags:
71
71
 
72
72
  For ad-hoc CDP inspection against this local preview, connect any CDP client to `localhost:19852` — Chrome DevTools front-end works directly. For an agent-friendly CLI over CDP (screenshot, brick tree/query, input emulation, storage reads, runtime eval, network capture), the `bricks-cli` skill documents the `bricks devtools` command surface — read that skill if it is installed in this workspace. If it is not installed, run `bricks --help` and `bricks devtools --help`; the CLI's own help output is authoritative.
73
73
 
74
+ To inspect Data / Property Bank or storage state, prefer the dedicated `bricks devtools storage` subcommands — `storage data-bank get <S_xxxx>` (saved Data values), `storage system persist|memory`, `storage system get <key>` — over hand-written `runtime eval`. Reach for `runtime eval` only for *live* store internals that aren't persisted to a data bank (e.g. a current transient value: `runtime eval "system.data.property('S_xxxx', '<alias>')"`); don't reverse-engineer the `system.*` globals.
75
+
74
76
  ### Project Automations
75
77
 
76
78
  E2E tests authored in TypeScript inside the project (`AutomationTest` / `TestCase`). Test cases include:
@@ -0,0 +1,41 @@
1
+ import { appendFile, mkdir, readFile, writeFile } from 'node:fs/promises'
2
+ import path from 'node:path'
3
+
4
+ const auditLogIgnoreEntry = '.bricks/edits.jsonl'
5
+
6
+ // Ensure the project's .gitignore excludes the audit log. No-op when it is already
7
+ // ignored directly or via a broader `.bricks` rule.
8
+ const ensureAuditLogIgnored = async (projectDir: string) => {
9
+ const gitignorePath = path.join(projectDir, '.gitignore')
10
+ const content = await readFile(gitignorePath, 'utf8').catch((err: any) => {
11
+ if (err?.code === 'ENOENT') return ''
12
+ throw err
13
+ })
14
+ const ignored = content
15
+ .split(/\r?\n/)
16
+ .map((line) => line.trim())
17
+ .some((line) => line === auditLogIgnoreEntry || line === '.bricks/' || line === '.bricks')
18
+ if (ignored) return
19
+
20
+ const prefix = content && !content.endsWith('\n') ? '\n' : ''
21
+ await writeFile(
22
+ gitignorePath,
23
+ `${content}${prefix}\n# MCP entry-editing audit log\n${auditLogIgnoreEntry}\n`,
24
+ )
25
+ }
26
+
27
+ // Append one JSON record to `.bricks/edits.jsonl`, creating the directory and the
28
+ // gitignore entry as needed. Shared by the source-editing tools and `compile()` so
29
+ // every project mutation lands in the same audit log.
30
+ export const appendEditRecord = async (projectDir: string, record: Record<string, unknown>) => {
31
+ const bricksDir = path.join(projectDir, '.bricks')
32
+ await mkdir(bricksDir, { recursive: true })
33
+ await ensureAuditLogIgnored(projectDir)
34
+ await appendFile(path.join(bricksDir, 'edits.jsonl'), `${JSON.stringify(record)}\n`)
35
+ }
36
+
37
+ // Provenance stamped on every audit record: which agent/session produced the change.
38
+ export const editProvenance = () => ({
39
+ session: process.env.BRICKS_CTOR_SESSION_ID || process.env.CODEX_SESSION_ID,
40
+ agent: process.env.BRICKS_CTOR_AGENT_ID || process.env.USER,
41
+ })
@@ -0,0 +1,13 @@
1
+ const truthyEnvValues = new Set(['1', 'true', 'yes', 'on'])
2
+
3
+ export function isTruthyEnv(value: string | undefined) {
4
+ return truthyEnvValues.has(
5
+ String(value || '')
6
+ .trim()
7
+ .toLowerCase(),
8
+ )
9
+ }
10
+
11
+ export function shouldRegisterEditingTools(env: Record<string, string | undefined> = process.env) {
12
+ return isTruthyEnv(env.BRICKS_CTOR_ENABLE_EDITING_TOOLS)
13
+ }
@@ -6,6 +6,9 @@ import { register as registerLottie } from './mcp-tools/lottie'
6
6
  import { register as registerIcons } from './mcp-tools/icons'
7
7
  import { register as registerHuggingface } from './mcp-tools/huggingface'
8
8
  import { register as registerMedia } from './mcp-tools/media'
9
+ import { register as registerEntryEditing } from './mcp-tools/entry-editing'
10
+ import { register as registerDataCalcEditing } from './mcp-tools/data-calc-editing'
11
+ import { shouldRegisterEditingTools } from './mcp-env'
9
12
 
10
13
  const server = new McpServer({
11
14
  name: 'bricks-ctor',
@@ -24,5 +27,10 @@ registerIcons(server)
24
27
  registerHuggingface(server)
25
28
  registerMedia(server, projectDir)
26
29
 
30
+ if (shouldRegisterEditingTools()) {
31
+ registerEntryEditing(server, projectDir)
32
+ registerDataCalcEditing(server, projectDir)
33
+ }
34
+
27
35
  const transport = new StdioServerTransport()
28
36
  await server.connect(transport)
@@ -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
+ })