1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
const assert = require('node:assert/strict');
const { readFileSync } = require('node:fs');
const vm = require('node:vm');
const source = readFileSync('logs.js', 'utf8');
const lookup = source.slice(source.indexOf('let deviceMapCache = {}'), source.indexOf('function updateDeviceCard'));
async function main() {
let requests = 0;
let mode = 'success';
const updates = [];
const context = vm.createContext({
console: { warn() {} },
updateDeviceReferences: (...args) => updates.push(args),
fetch: async (path) => {
requests++;
assert.match(path, /^\/api\/devices\/[A-Za-z0-9_-][A-Za-z0-9_.-]*$/);
return {
ok: mode !== 'http-error', status: mode === 'http-error' ? 404 : 200,
json: async () => {
if (mode === 'invalid-json') throw new Error('not JSON');
if (mode === 'missing-name') return { status: 'error' };
if (mode === 'http-error') return { error: 'Not found' };
return {
name: null, model: { name: 'Model fallback' }, serial: 'SERIAL',
assigned_to: { name: 'Synthetic user' }, image: '/image.png',
category: { tag_color: '#123456' }, notes: 'Synthetic notes'
};
}
};
}
});
vm.runInContext(lookup, context);
const first = context.fetchDeviceDetails('TEST_1-2.3');
assert.equal(context.fetchDeviceDetails('TEST_1-2.3'), first, 'dedupe pending requests');
const device = await first;
assert.deepEqual(JSON.parse(JSON.stringify(device)), {
name: 'Model fallback', serial: 'SERIAL', assigned_to: 'Synthetic user',
image: '/image.png', category_color: '#123456', notes: 'Synthetic notes'
});
assert.equal(await context.fetchDeviceDetails('TEST_1-2.3'), device);
assert.equal(requests, 1);
assert.equal(updates.length, 1);
for (const id of ['', '../secret', '.hidden', 'a/b', 'a?x=y']) {
assert.equal(await context.fetchDeviceDetails(id), null);
}
assert.equal(requests, 1, 'invalid tags never request the proxy');
for (mode of ['http-error', 'invalid-json', 'missing-name']) {
assert.equal(await context.fetchDeviceDetails('RETRY'), null);
}
mode = 'success';
assert.ok(await context.fetchDeviceDetails('RETRY'), 'failed lookups can retry');
console.log('PASS: raw Snipe-IT mapping, model-name fallback, cache/dedupe, invalid IDs, errors/retry');
}
main().catch((error) => { console.error(error); process.exitCode = 1; });