t56-live-fixture.mjs 2.1 KB

12345678910111213141516171819202122232425262728293031
  1. import assert from 'node:assert/strict'
  2. // This replaces only HTTP transport. The real detail API, adapter and row view still run.
  3. const scenario = process.env.T56_LIVE_SCENARIO || 'normal'
  4. const value = scenario === 'zero' ? 0 : scenario === 'missing' ? null : 17.3
  5. const time = scenario === 'invalid-time' ? 'not-a-timestamp' : '2026-10-23T17:04:29'
  6. const source = { id: 'captured-node-2', deviceId: 21119740, nodeId: 2, hum: value, tem: 22.2, createTime: time }
  7. if (scenario === 'wrong-node') source.nodeId = 3
  8. const metrics = [
  9. { metricCode: 'ENV_COD', metricName: 'COD', value, unit: scenario === 'wrong-unit' ? 'MPa' : 'mg/L',
  10. sampledAt: time === 'not-a-timestamp' ? time : `${time}+08:00`, timeSource: 'CREATED_AT', freshness: 'STALE' },
  11. { metricCode: 'ENV_NODE_2_TEMPERATURE', metricName: 'COD节点温度', value: 22.2, unit: '℃',
  12. sampledAt: time === 'not-a-timestamp' ? time : `${time}+08:00`, timeSource: 'CREATED_AT', freshness: 'STALE' },
  13. ]
  14. // phantom-temperature:源记录在,但后端漏发该节点的温度指标行。
  15. if (scenario === 'phantom-temperature') metrics.pop()
  16. const reply = (data) => new Response(JSON.stringify({ code: 200, data }), { headers: { 'Content-Type': 'application/json' } })
  17. globalThis.fetch = async (input) => {
  18. const url = new URL(input)
  19. assert.equal(url.hostname, 'fixture.invalid', 'The offline guard must never contact a live host')
  20. if (url.pathname === '/api/env/visualization/latest') {
  21. assert.equal(url.searchParams.get('deviceId'), '21119740')
  22. // no-source:源侧无记录;其余场景仅节点 2 有捕获记录。
  23. return reply(url.searchParams.get('nodeId') === '2' && scenario !== 'no-source' ? source : null)
  24. }
  25. assert.equal(url.pathname, '/api/cockpit/v1/equipments/21119740/detail-aggregate')
  26. return reply({ deviceCode: '21119740', aggregateState: 'READY',
  27. archive: { state: 'SUCCESS', data: { deviceCode: '21119740', deviceName: '环境设备', deviceType: '15', deviceTypeName: '仁科环境监测器' } },
  28. metrics: { state: 'STALE', data: { deviceCode: '21119740', state: 'STALE', stateReason: 'SAMPLE_OUTDATED', metrics } },
  29. })
  30. }