Files
memind/wechat-schedule-llm-config.test.mjs
T
2026-07-07 12:10:21 +08:00

58 lines
2.0 KiB
JavaScript

import assert from 'node:assert/strict';
import test from 'node:test';
import {
createWechatScheduleLlmConfigService,
wechatScheduleLlmConfigInternals,
} from './wechat-schedule-llm-config.mjs';
function createPool(seedRow = null) {
const state = { row: seedRow };
return {
async query(sql, params) {
if (sql.includes('CREATE TABLE')) return [[], []];
if (sql.includes('SELECT config_json')) {
return [state.row ? [state.row] : [], []];
}
if (sql.includes('INSERT INTO h5_wechat_admin_config')) {
state.row = {
config_json: params[1],
updated_by: params[2],
updated_at: params[3],
};
return [[], []];
}
throw new Error(`Unexpected query: ${sql}`);
},
};
}
test('wechat schedule llm config service falls back to env defaults', async () => {
const service = createWechatScheduleLlmConfigService(createPool(), {
env: { H5_WECHAT_SCHEDULE_LLM_ENABLED: '1' },
});
const result = await service.getConfig();
assert.equal(result.scheduleLlmEnabled, true);
assert.equal(result.updatedAt, null);
});
test('wechat schedule llm config service persists admin toggle', async () => {
const service = createWechatScheduleLlmConfigService(createPool(), {
env: { H5_WECHAT_SCHEDULE_LLM_ENABLED: '0' },
});
const updated = await service.updateConfig({ scheduleLlmEnabled: true }, { updatedBy: 'admin-1' });
assert.equal(updated.scheduleLlmEnabled, true);
assert.equal(updated.updatedBy, 'admin-1');
assert.equal(await service.isScheduleLlmEnabled(), true);
});
test('wechat schedule llm config internals normalize booleans consistently', () => {
assert.equal(wechatScheduleLlmConfigInternals.normalizeBoolean('1', false), true);
assert.equal(wechatScheduleLlmConfigInternals.normalizeBoolean('off', true), false);
assert.equal(
wechatScheduleLlmConfigInternals.defaultsFromEnv({
H5_WECHAT_SCHEDULE_LLM_ENABLED: 'true',
}).scheduleLlmEnabled,
true,
);
});