minicpm5-2b-webgpu / tests /core.test.js
ProCreations's picture
Dedicated WebGPU runtime, DSpark acceleration, and automatic tool repair
c51a3e7 verified
Raw History Blame Contribute Delete
2.62 kB
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='<html><style>body{color:red}</style><script>if(1<2) alert("&");</script></html>';const [c]=parseXMLCalls(`<function name="create_html"><param name="title">A &amp; B</param><param name="html"><![CDATA[${html}]]></param></function>`);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('<function name="create_spreadsheet"><param name="title">Budget</param><param name="sheets">[{"name":"Budget","columns":["A"],"rows":[[123],["=SUM(A2:A2)"]]}]</param></function>')[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('<function name="create_html"><param name="html">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('<meta http-equiv="Content-Security-Policy" content="default-src *"><script>fetch("https://example.com")</script>');assert.ok(page.indexOf(PREVIEW_CSP)<page.indexOf('default-src *'));assert.ok(PREVIEW_CSP.includes("connect-src 'none'"));});
test('malformed closing tags return repairable errors to the model',()=>{assert.throws(()=>validateArtifact('create_html',{title:'Counter',html:'<span>+</span id="count">0'}),/closing tag/);assert.doesNotThrow(()=>validateArtifact('create_html',{title:'Counter',html:'<span>+</span><span id="count">0</span>'}));});