Files
CloudLab/cloudlab-web/tests/compare-engine.test.js
CloudLab Deploy 4c312cac39
CloudLab CI/CD / 门禁 doctor (push) Failing after 38s
CloudLab CI/CD / 单元测试 (push) Skipped
CloudLab CI/CD / 归档完整性 (push) Skipped
CloudLab CI/CD / 构建站点与归档 (push) Skipped
CloudLab CI/CD / 部署到 lab.hdtime.space(rsync) (push) Skipped
CloudLab CI/CD / 构建镜像并推送 Gitea 容器仓库 (push) Skipped
CloudLab CI/CD / 容器部署到 lab.hdtime.space (push) Skipped
CloudLab CI/CD / 发布版本(Tag) (push) Skipped
feat(platform): add CloudLab execution platform and IDE web source
Freeze the real-execution control plane, runner, manifests, validation cases and tests under cloudlab-platform; preserve the IDE/static web source under cloudlab-web. Generated material library and runtime state are intentionally excluded.
2026-11-18 00:14:35 +08:00

168 lines
5.5 KiB
JavaScript

'use strict';
const assert = require('node:assert/strict');
const M = require('../app/compare-engine.js');
let passed = 0;
function test(name, fn) {
try { fn(); passed += 1; console.log('ok ' + name); }
catch (e) { console.error('FAIL ' + name); console.error(e.stack || e); process.exitCode = 1; }
}
function runToEnd(cell, dt) {
M.start(cell);
let guard = 0;
while (cell.running && guard < 100000) { M.tick(cell, dt || 0.1); guard += 1; }
return M.metrics(cell);
}
function near(a, b, eps) { assert.ok(Math.abs(a - b) <= eps, a + ' != ' + b + ' ± ' + eps); }
test('preset matrix is 3x3 with A1..C3', () => {
const cells = M.createMatrix();
assert.equal(cells.length, 9);
assert.deepEqual(cells.map(c => c.id), ['A1','A2','A3','B1','B2','B3','C1','C2','C3']);
assert.equal(cells[0].contents.waterMl, 200);
assert.equal(cells[1].contents.hcl5Ml, 100);
assert.equal(cells[2].contents.na2co3G, 2);
assert.equal(cells[2].contents.hcl5Ml, 100);
assert.equal(cells[5].contents.na2co3G, 4);
assert.equal(cells[8].initialTempC, 45);
});
test('stoichiometry and liquid volume', () => {
const cells = M.createMatrix();
const t = M.totals(cells[2]);
near(t.volumeMl, 100, 1e-9);
near(t.nC, 2 / 105.9888, 1e-12);
near(t.nH, 0.140426, 1e-6);
near(t.maxExtent, t.nC, 1e-12);
near(t.nNa, 2 * t.nC, 1e-12);
assert.ok(t.nH > t.maxExtent * 2);
});
test('water blank pH 7 and no reaction', () => {
const cells = M.createMatrix();
const m = M.metrics(cells[0]);
near(m.pH, 7, 1e-9);
assert.equal(m.maxExtent, 0);
const r = M.start(cells[0]);
assert.equal(r.ok, false);
assert.equal(r.reason, 'no_reaction');
assert.equal(cells[0].status, 'no_reaction');
});
test('base-only beaker is alkaline and does not react', () => {
const cells = M.createMatrix();
const m = M.metrics(cells[7]);
assert.ok(m.pH > 11.5, 'pH=' + m.pH);
assert.equal(m.maxExtent, 0);
assert.equal(m.statusLabel, '就绪');
});
test('A3 5% HCl + 2 g Na2CO3 reaches 461.7 mL CO2 at 25 C', () => {
const cells = M.createMatrix();
const m = runToEnd(cells[2], 0.1);
assert.equal(m.status, 'done');
near(m.progress, 1, 1e-9);
near(m.co2Ml, 461.7, 1.0);
near(m.co2Ml, 2 / 105.9888 * 8.314462618 * 298.15 / 101325 * 1000 * 1000, 1.0);
assert.ok(m.pH < 0.1, 'pH=' + m.pH);
assert.ok(m.simTime > 10 && m.simTime < 80, 't=' + m.simTime);
});
test('doubling acid keeps CO2 but lowers final pH', () => {
const cells = M.createMatrix();
const a3 = runToEnd(cells[2], 0.1);
const b1 = runToEnd(cells[3], 0.1);
near(b1.co2Ml, a3.co2Ml, 1.0);
assert.ok(b1.pH < a3.pH, 'B1 pH ' + b1.pH + ' should be < A3 pH ' + a3.pH);
near(b1.extentMol, a3.extentMol, 1e-9);
});
test('doubling Na2CO3 doubles CO2', () => {
const cells = M.createMatrix();
const b3 = runToEnd(cells[5], 0.1);
near(b3.co2Ml, 923.3, 1.5);
assert.ok(b3.simTime > 60, 'B3 should be slower, t=' + b3.simTime);
});
test('bicarbonate gives 582.5 mL CO2 with excess acid', () => {
const cells = M.createMatrix();
const c1 = runToEnd(cells[6], 0.1);
near(c1.co2Ml, 582.5, 1.2);
assert.ok(c1.pH < 0.2, 'pH=' + c1.pH);
});
test('higher temperature speeds up but changes gas volume by Charles law', () => {
const cells = M.createMatrix();
const a3 = runToEnd(cells[2], 0.1);
const c3 = runToEnd(cells[8], 0.1);
assert.ok(c3.simTime < a3.simTime, '45 C should finish faster');
near(c3.co2Ml / a3.co2Ml, 318.15 / 298.15, 0.002);
});
test('pause freezes extent, resume continues', () => {
const cells = M.createMatrix();
const c = cells[2];
assert.equal(M.start(c).ok, true);
M.tick(c, 0.5);
const before = c.extentMol;
assert.ok(before > 0);
M.pause(c);
assert.equal(c.status, 'paused');
M.tick(c, 2);
near(c.extentMol, before, 1e-12);
M.pause(c);
assert.equal(c.status, 'running');
M.tick(c, 0.5);
assert.ok(c.extentMol > before);
});
test('add reagent and clean resets state', () => {
const cells = M.createMatrix();
const c = cells[0];
const r = M.addReagent(c, 'hcl5Ml', 50);
assert.equal(r.ok, true);
assert.equal(c.contents.hcl5Ml, 50);
assert.equal(c.contents.waterMl, 200);
assert.equal(c.simTime, 0);
const r2 = M.addReagent(c, 'na2co3G', 1);
assert.equal(r2.ok, true);
assert.equal(c.contents.na2co3G, 1);
const m = M.metrics(c);
assert.ok(m.maxExtent > 0);
M.clean(c);
assert.equal(c.contents.hcl5Ml, 0);
assert.equal(c.contents.na2co3G, 0);
assert.equal(c.status, 'idle');
});
test('liquid overflow is rejected', () => {
const cells = M.createMatrix();
const c = cells[0];
const r = M.addReagent(c, 'waterMl', 100);
assert.equal(r.ok, false);
assert.equal(r.reason, 'overflow');
assert.equal(c.contents.waterMl, 200);
});
test('serialize/restore keeps progress and pauses running state', () => {
const cells = M.createMatrix();
M.start(cells[2]);
M.tick(cells[2], 1);
const saved = JSON.parse(JSON.stringify(M.serialize(cells)));
const restored = M.restore(saved);
assert.equal(restored.length, 9);
assert.equal(restored[2].status, 'paused');
assert.equal(restored[2].running, false);
near(restored[2].extentMol, cells[2].extentMol, 1e-12);
assert.ok(restored[2].history.length >= 1);
});
test('model card states the teaching-model boundary', () => {
assert.equal(M.MODEL_CARD.tier, 'model');
assert.ok(M.MODEL_CARD.assumptions.some(s => /CO₂/.test(s)));
assert.ok(M.MODEL_CARD.disclaimer.includes('不是实验测量'));
assert.ok(M.MODEL_CARD.limitations.some(s => /真实配液|危险化学品/.test(s)));
});
console.log('\n' + passed + ' compare-engine tests passed');