import test from 'node:test';
import assert from 'node:assert/strict';
import {kvBytes,validateSettings,parseXMLCalls,validateArtifact,sandboxHTML,PREVIEW_CSP} from '../src/core.js';
test('KV allocations use all 42 layers, two KV heads and block overhead',()=>{assert.equal(kvBytes(16384,'f16','f16'),704643072);assert.equal(kvBytes(16384,'q8_0','q8_0'),396361728);assert.equal(kvBytes(131072,'q4_0','q4_0'),1761607680);});
test('untrusted saved settings are bounded by supported model options',()=>{const s=validateSettings({context:999999,cacheK:'bogus',temperature:9,maxTokens:999999});assert.equal(s.context,16384);assert.equal(s.cacheK,'f16');assert.equal(s.temperature,2);assert.equal(s.maxTokens,15872);});
test('MiniCPM XML CDATA preserves source code exactly',()=>{const html='';const [c]=parseXMLCalls(`A & B`);const a=JSON.parse(c.function.arguments);assert.equal(a.html,html);assert.equal(a.title,'A & B');});
test('spreadsheet XML parses arrays, including formulas and numeric cells',()=>{const a=JSON.parse(parseXMLCalls('Budget[{"name":"Budget","columns":["A"],"rows":[[123],["=SUM(A2:A2)"]]}]')[0].function.arguments);assert.equal(a.sheets[0].rows[0][0],123);assert.doesNotThrow(()=>validateArtifact('create_spreadsheet',a));});
test('incomplete or unknown tool calls fail rather than producing bogus files',()=>{assert.equal(parseXMLCalls('unfinished').length,0);assert.throws(()=>validateArtifact('shell',{title:'x'}));assert.throws(()=>validateArtifact('create_html',{title:'x',html:''}));assert.throws(()=>validateArtifact('create_spreadsheet',{title:'x',sheets:[{columns:['a'],rows:[{a:1}]}]}));});
test('preview network restrictions precede model-controlled content',()=>{const page=sandboxHTML('');assert.ok(page.indexOf(PREVIEW_CSP){assert.throws(()=>validateArtifact('create_html',{title:'Counter',html:'+0'}),/closing tag/);assert.doesNotThrow(()=>validateArtifact('create_html',{title:'Counter',html:'+0'}));});