Files
weiguang-jewelry-ledger/新账系统/test/app-paths.test.js
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

62 lines
2.1 KiB
JavaScript
Raw Normal View History

'use strict';
// 目录重组后的路径护栏:默认数据目录、静态资源目录与飞书配置文件必须仍落在 新账系统/ 下的原位置,
// 否则用户机器上的真实账本会“消失”(启动时在新位置建空库)。
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const path = require('node:path');
const { Ledger } = require('../core/ledger');
const { createApp } = require('../server');
const { serveStatic } = require('../server/middleware/static');
const APP_DIR = path.resolve(__dirname, '..');
function withSpy(object, method, record, fn) {
const original = object[method];
object[method] = function (...args) {
record.push(args[0]);
return original.apply(this, args);
};
try {
return fn();
} finally {
object[method] = original;
}
}
test('默认数据目录仍为 新账系统/data/<mode>', () => {
for (const mode of ['trial', 'demo']) {
const app = createApp({ mode, ledger: new Ledger(':memory:'), feishuConfig: { enabled: false } });
try {
assert.equal(app.dataDir, path.join(APP_DIR, 'data', mode));
} finally {
app.ledger.close();
}
}
});
test('默认飞书配置文件仍为 新账系统/feishu.local.json', () => {
const saved = process.env.FEISHU_CONFIG;
delete process.env.FEISHU_CONFIG;
const checked = [];
const original = fs.existsSync;
fs.existsSync = file => {
checked.push(file);
return file === path.join(APP_DIR, 'feishu.local.json') ? false : original(file);
};
try {
const app = createApp({ mode: 'trial', ledger: new Ledger(':memory:') });
app.ledger.close();
} finally {
fs.existsSync = original;
if (saved !== undefined) process.env.FEISHU_CONFIG = saved;
}
assert.ok(checked.includes(path.join(APP_DIR, 'feishu.local.json')), checked.join('\n'));
});
test('静态资源仍从 新账系统/public 读取', () => {
const read = [];
const res = { writeHead() {}, setHeader() {}, end() {} };
withSpy(fs, 'readFileSync', read, () => serveStatic(res, '/', false));
assert.ok(read.includes(path.join(APP_DIR, 'public', 'index.html')), read.join('\n'));
});