cangku/backend/test/similarProductsSettings.test.js

79 lines
2.5 KiB
JavaScript

'use strict';
const test = require('node:test');
const assert = require('node:assert/strict');
const {
DEFAULT_ENDPOINT_URL,
DEFAULT_TIMEOUT_MS,
normalizeEndpointUrl,
normalizeStoredConfig,
shouldRetrySimilarProductsProbe,
stripSecretFields,
} = require('../src/services/similarProductsSettings');
test('company image search defaults to a five minute request timeout', () => {
assert.equal(DEFAULT_TIMEOUT_MS, 300000);
});
test('normalizeEndpointUrl keeps a complete similar-products endpoint unchanged', () => {
assert.equal(normalizeEndpointUrl(DEFAULT_ENDPOINT_URL), DEFAULT_ENDPOINT_URL);
});
test('deployment probe retries transient gateway and routing statuses only', () => {
assert.equal(shouldRetrySimilarProductsProbe(404), true);
assert.equal(shouldRetrySimilarProductsProbe(504), true);
assert.equal(shouldRetrySimilarProductsProbe(400), false);
assert.equal(shouldRetrySimilarProductsProbe(401), false);
});
test('legacy company image-search secrets are removed from general configuration', () => {
assert.deepEqual(stripSecretFields({
endpointUrl: DEFAULT_ENDPOINT_URL,
timeoutMs: DEFAULT_TIMEOUT_MS,
apiKey: 'legacy-secret',
api_key: 'legacy-secret-2',
token: 'legacy-secret-3',
}), {
endpointUrl: DEFAULT_ENDPOINT_URL,
timeoutMs: DEFAULT_TIMEOUT_MS,
});
});
test('normalizeStoredConfig gives database values precedence over environment fallback', () => {
const config = normalizeStoredConfig({
endpointUrl: DEFAULT_ENDPOINT_URL,
apiKey: 'database-api-key',
timeoutMs: 90000,
}, {
endpointUrl: 'http://192.168.40.206:7503/open-api/image-search/similar-products',
token: 'environment-api-key',
timeoutMs: DEFAULT_TIMEOUT_MS,
source: 'environment',
});
assert.deepEqual(config, {
endpointUrl: DEFAULT_ENDPOINT_URL,
baseUrl: DEFAULT_ENDPOINT_URL,
token: 'database-api-key',
timeoutMs: 90000,
source: 'database',
});
});
test('normalizeStoredConfig only falls back for fields omitted from database settings', () => {
const config = normalizeStoredConfig({
endpointUrl: DEFAULT_ENDPOINT_URL,
}, {
endpointUrl: 'http://old-service.local/open-api/image-search/similar-products',
token: 'environment-api-key',
timeoutMs: DEFAULT_TIMEOUT_MS,
source: 'environment',
});
assert.equal(config.endpointUrl, DEFAULT_ENDPOINT_URL);
assert.equal(config.token, 'environment-api-key');
assert.equal(config.timeoutMs, DEFAULT_TIMEOUT_MS);
assert.equal(config.source, 'database');
});