'use strict'; const assert = require('node:assert/strict'); const fs = require('node:fs'); const vm = require('node:vm'); const ampacity = require('../src/buck-wire-ampacity.js'); const { limit, table, info } = ampacity; let passed = 0; function test(name, run) { run(); passed++; console.log('✓ ' + name); } function close(actual, expected) { assert.ok(Number.isFinite(actual)); assert.ok(Math.abs(actual - expected) <= Math.abs(expected) * 1e-12 + 1e-12, `实际 ${actual},预期 ${expected}`); } function rejects(run) { assert.throws(run, error => error instanceof Error && /[\u4e00-\u9fff]/.test(error.message)); } const gauges = [6, 8, 10, 12, 14, 16, 18, 20, 22, 24]; const single = [95, 75, 52, 34, 24, 15, 9.5, 6, 5, 3.5]; const grouped = [55, 40, 30, 20, 15, 10, 7, 5, 3, 2]; test('CommonJS和无依赖浏览器全局导出一致,静态表及信息只读', () => { assert.equal(globalThis.BuckWireAmpacity, ampacity); const browser = {}; vm.runInNewContext(fs.readFileSync(require.resolve('../src/buck-wire-ampacity.js'), 'utf8'), browser); assert.equal(typeof browser.BuckWireAmpacity.limit, 'function'); assert.equal(browser.BuckWireAmpacity.limit(12).currentLimit, limit(12).currentLimit); for (const value of [ampacity, table, table.single, table.grouped, info, info.installations, info.supportedAwg, info.temperatureBands, ...info.temperatureBands]) assert.ok(Object.isFrozen(value)); assert.throws(() => { table.single[12] = 999; }, TypeError); }); test('两列完整表逐行与已核实PVC铜线典型值一致,不进行插值', () => { assert.deepEqual(info.supportedAwg, gauges); assert.equal(Object.keys(table.single).length, 10); assert.equal(Object.keys(table.grouped).length, 10); for (const [index, awg] of gauges.entries()) { assert.equal(table.single[awg], single[index]); assert.equal(table.grouped[awg], grouped[index]); for (const [installation, expected] of [['single', single[index]], ['grouped', grouped[index]]]) { const result = limit(awg, { installation, ambientC: 30, designFactor: 1 }); assert.equal(result.awg, awg); assert.equal(result.installation, installation); assert.equal(result.baseCurrent, expected); assert.equal(result.temperatureFactor, 1); assert.equal(result.currentLimit, expected); assert.equal(result.designFactor, 1); } } }); test('默认grouped/30°C/0.8,0.8是工具自定余量而非源标准', () => { const result = limit(12); assert.equal(result.installation, 'grouped'); assert.equal(result.ambientC, 30); assert.equal(result.designFactor, 0.8); assert.equal(result.baseCurrent, 20); assert.equal(result.temperatureFactor, 1); assert.equal(result.currentLimit, 16); assert.equal(info.defaultInstallation, 'grouped'); assert.equal(info.defaultDesignFactor, 0.8); assert.ok(result.scope.includes('本工具自定')); assert.ok(result.scope.includes('不是原表')); assert.ok(info.designFactorScope.includes('不是原表')); }); test('环境分段边界严格≤30/≤40/≤45/≤50,低温不冷增额', () => { const cases = [[-40, 1], [-10, 1], [0, 1], [29.999999, 1], [30, 1], [30.000001, 0.82], [40, 0.82], [40.000001, 0.71], [45, 0.71], [45.000001, 0.58], [50, 0.58]]; for (const [ambientC, expected] of cases) { const result = limit(10, { installation: 'single', ambientC, designFactor: 1 }); assert.equal(result.temperatureFactor, expected); close(result.currentLimit, 52 * expected); assert.ok(result.currentLimit <= result.baseCurrent); } assert.equal(limit(10, { ambientC: -40 }).currentLimit, limit(10, { ambientC: 30 }).currentLimit); }); test('设计余量与环境因子相乘降额,单芯与≤3芯列不互相混用', () => { for (const awg of gauges) for (const installation of ['single', 'grouped']) { const result = limit(awg, { installation, ambientC: 42, designFactor: 0.6 }); close(result.currentLimit, table[installation][awg] * 0.71 * 0.6); assert.ok(result.currentLimit <= result.baseCurrent); } close(limit(18, { installation: 'grouped', ambientC: 50 }).currentLimit, 7 * 0.58 * 0.8); close(limit(18, { installation: 'single', ambientC: 50 }).currentLimit, 9.5 * 0.58 * 0.8); }); test('使用比例允许超过100%,仍按所选表值及环境系数相乘', () => { for (const awg of gauges) for (const installation of ['single', 'grouped']) { for (const designFactor of [1.00001, 1.5, 2, 10]) { const result = limit(awg, { installation, ambientC: 42, designFactor }); close(result.currentLimit, table[installation][awg] * 0.71 * designFactor); assert.equal(result.designFactor, designFactor); } } assert.equal(limit(18, { designFactor: 2 }).currentLimit, 14); }); test('不根据材质加额,single列使用/厂家敷设温升/非官方标准限制明示', () => { const ordinary = limit(12); for (const material of ['PVC', 'silicone', 'PTFE']) { assert.deepEqual(limit(12, { material }), ordinary); } assert.equal(info.sourceIsOfficialStandard, false); assert.equal(info.source, 'https://www.engineeringtoolbox.com/wire-gauges-d_419.html'); assert.equal(ordinary.source, info.source); for (const text of ['PVC', 'single', '至多 3 芯', '不是官方标准', '不自动加额', '厂家', '敷设', '温升', '法规']) { assert.ok(ordinary.scope.includes(text), text); } }); test('严格拒绝奇数/非数AWG、不支持敷设、环境越界与非法余量,错误为中文', () => { for (const awg of [5, 7, 11, 23, 25, 12.5, '12', null, undefined, NaN, Infinity]) rejects(() => limit(awg)); for (const options of [null, [], 'grouped', 1, false]) rejects(() => limit(12, options)); for (const installation of ['', 'multicore', 'SINGLE', 'single ', 0, 1, null, {}, []]) { rejects(() => limit(12, { installation })); } for (const ambientC of [-40.00001, 50.00001, 51, NaN, Infinity, -Infinity, '30', null]) { rejects(() => limit(12, { ambientC })); } for (const designFactor of [0, -1, NaN, Infinity, '0.8', null, Number.MIN_VALUE, Number.MAX_VALUE]) { rejects(() => limit(12, { designFactor })); } assert.deepEqual(limit(12, {}), limit(12, { installation: undefined, ambientC: undefined, designFactor: undefined })); }); console.log(`\n${passed} 项经验载流表测试全部通过。`);