89 lines
4.7 KiB
JavaScript
89 lines
4.7 KiB
JavaScript
import assert from 'node:assert/strict';
|
|
import { readFile } from 'node:fs/promises';
|
|
import test from 'node:test';
|
|
|
|
async function read(relativePath) {
|
|
return readFile(new URL(`../${relativePath}`, import.meta.url), 'utf8');
|
|
}
|
|
|
|
test('ERP session keepalive is scheduled through alarms with a conservative default', async () => {
|
|
const [manifestText, background, mappingText] = await Promise.all([
|
|
read('chrome-extension/ltjt-order-assistant/manifest.json'),
|
|
read('chrome-extension/ltjt-order-assistant/background.js'),
|
|
read('mappings/lifecycle.mapping.json')
|
|
]);
|
|
const manifest = JSON.parse(manifestText);
|
|
const mapping = JSON.parse(mappingText);
|
|
assert.ok(manifest.permissions.includes('alarms'));
|
|
assert.match(background, /ERP_KEEPALIVE_ALARM_NAME = 'ltjt-erp-session-keepalive'/);
|
|
assert.match(background, /ERP_KEEPALIVE_PERIOD_MINUTES = 5/);
|
|
assert.match(background, /delayInMinutes: ERP_KEEPALIVE_INITIAL_DELAY_MINUTES/);
|
|
assert.match(background, /chrome\.alarms\.onAlarm\.addListener/);
|
|
assert.match(background, /ERP_LINK_RECOVERY_COOLDOWN_MS = 2 \* 60_000/);
|
|
assert.equal(mapping.erp_session_keepalive.enabled_by_default, true);
|
|
assert.equal(mapping.erp_session_keepalive.period_minutes, 5);
|
|
assert.equal(mapping.erp_session_keepalive.initial_delay_minutes, 1);
|
|
assert.equal(mapping.erp_session_keepalive.exception_recovery.enabled_by_default, true);
|
|
assert.equal(mapping.erp_session_keepalive.exception_recovery.cooldown_minutes, 2);
|
|
});
|
|
|
|
test('ERP keepalive is an existing-tab read-only request and never a page reload or task write', async () => {
|
|
const background = await read('chrome-extension/ltjt-order-assistant/background.js');
|
|
const probeBody = background.slice(
|
|
background.indexOf('async function executeErpKeepaliveProbe'),
|
|
background.indexOf('async function performErpLinkRecovery')
|
|
);
|
|
const keepaliveBody = background.slice(
|
|
background.indexOf('async function runErpSessionKeepalive'),
|
|
background.indexOf('async function ensureErpSessionKeepaliveAlarm')
|
|
);
|
|
assert.match(keepaliveBody, /findExistingErpTabForKeepalive/);
|
|
assert.match(keepaliveBody, /active_erp_execution/);
|
|
assert.match(keepaliveBody, /executeErpKeepaliveProbe/);
|
|
assert.match(keepaliveBody, /requestErpLinkRecovery/);
|
|
assert.match(probeBody, /fetch\(keepaliveUrl, \{/);
|
|
assert.match(probeBody, /method: 'GET'/);
|
|
assert.match(probeBody, /credentials: 'include'/);
|
|
assert.match(probeBody, /cache: 'no-store'/);
|
|
assert.match(probeBody, /response\.text\(\)/);
|
|
assert.doesNotMatch(keepaliveBody, /chrome\.tabs\.create/);
|
|
assert.doesNotMatch(keepaliveBody, /chrome\.tabs\.reload/);
|
|
assert.doesNotMatch(keepaliveBody, /SubmitInfoForm|captureAjaxSubmit|DoInfo/);
|
|
});
|
|
|
|
test('ERP exception recovery refreshes the existing ERP tab once, then performs a read-only probe', async () => {
|
|
const background = await read('chrome-extension/ltjt-order-assistant/background.js');
|
|
const recoveryBody = background.slice(
|
|
background.indexOf('async function performErpLinkRecovery'),
|
|
background.indexOf('function requestErpLinkRecovery')
|
|
);
|
|
assert.match(recoveryBody, /findExistingErpTabForKeepalive\(\{ allowLoading: true \}\)/);
|
|
assert.match(recoveryBody, /hasActiveErpExecution/);
|
|
assert.match(recoveryBody, /chrome\.tabs\.reload\(tab\.id, \{ bypassCache: true \}\)/);
|
|
assert.match(recoveryBody, /waitForTabLoad\(tab\.id, ERP_LINK_RECOVERY_TIMEOUT_MS\)/);
|
|
assert.match(recoveryBody, /executeErpKeepaliveProbe\(tab\.id\)/);
|
|
assert.match(recoveryBody, /recovery_count: state\.recovery_count \+ 1/);
|
|
assert.doesNotMatch(recoveryBody, /chrome\.tabs\.create/);
|
|
assert.doesNotMatch(recoveryBody, /captureAjaxSubmit|SubmitInfoForm|DoInfo/);
|
|
});
|
|
|
|
test('keepalive setting and last probe state are visible without changing the business operation contract', async () => {
|
|
const [background, bridge, popup, popupHtml] = await Promise.all([
|
|
read('chrome-extension/ltjt-order-assistant/background.js'),
|
|
read('chrome-extension/ltjt-order-assistant/business-bridge.js'),
|
|
read('chrome-extension/ltjt-order-assistant/popup.js'),
|
|
read('chrome-extension/ltjt-order-assistant/popup.html')
|
|
]);
|
|
assert.match(background, /erp_keepalive: keepalive/);
|
|
assert.match(background, /last_status: status/);
|
|
assert.match(background, /erp_link_recovery/);
|
|
assert.match(background, /recovery_status/);
|
|
assert.match(bridge, /erpKeepaliveEnabled/);
|
|
assert.match(bridge, /erp_keepalive_enabled/);
|
|
assert.match(popupHtml, /id="erpKeepaliveToggle"/);
|
|
assert.match(popup, /readKeepaliveEnabled/);
|
|
assert.match(popup, /keepaliveStatusText/);
|
|
assert.match(popup, /正在刷新现有 ERP 页面并进行只读复测/);
|
|
assert.match(popup, /erpKeepaliveEnabled: event\.currentTarget\.checked/);
|
|
});
|