governance-audit-model.test.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import assert from 'node:assert/strict'
  2. import test from 'node:test'
  3. import {
  4. auditEventHeaders,
  5. categoryPresentation,
  6. coveragePresentation,
  7. defaultAuditWindow,
  8. formatAuditTime,
  9. integrityPresentation,
  10. shortDigest,
  11. statusPresentation,
  12. toAuditWindow
  13. } from '../src/views/systemManage/governanceAudit/governanceAuditModel.js'
  14. test('presents the six fixed governance audit categories', () => {
  15. const labels = [
  16. 'authentication',
  17. 'ingestion',
  18. 'entity_resolution',
  19. 'publication',
  20. 'remediation',
  21. 'knowledge_query'
  22. ].map(code => categoryPresentation(code).label)
  23. assert.deepEqual(labels, [
  24. '登录认证',
  25. '数据采集',
  26. '实体合并与回滚',
  27. '语义与质量发布',
  28. '质量整改',
  29. '知识问答'
  30. ])
  31. })
  32. test('keeps no records distinct and presents seal integrity explicitly', () => {
  33. assert.deepEqual(coveragePresentation({ count: 0 }), {
  34. label: '暂无记录',
  35. color: 'blue-grey'
  36. })
  37. assert.deepEqual(coveragePresentation({ count: 2 }), {
  38. label: '有记录',
  39. color: 'success'
  40. })
  41. assert.equal(integrityPresentation('intact').label, '校验完整')
  42. assert.equal(integrityPresentation('tampered').label, '检测到变更')
  43. assert.equal(
  44. integrityPresentation('invalid_signature').label,
  45. '封存签名无效'
  46. )
  47. })
  48. test('formats only bounded safe event fields', () => {
  49. const values = auditEventHeaders().map(item => item.value)
  50. assert.deepEqual(values, [
  51. 'occurred_at',
  52. 'category',
  53. 'action',
  54. 'status',
  55. 'actor_uid',
  56. 'resource'
  57. ])
  58. for (const forbidden of [
  59. 'password',
  60. 'query',
  61. 'evidence',
  62. 'ip_address',
  63. 'user_agent',
  64. 'note',
  65. 'payload'
  66. ]) {
  67. assert.ok(!values.includes(forbidden))
  68. }
  69. assert.equal(statusPresentation('failed').color, 'error')
  70. assert.equal(formatAuditTime(null), '-')
  71. assert.match(formatAuditTime('2026-07-30T00:00:00Z'), /2026/)
  72. })
  73. test('shortens hashes without mistaking empty values for evidence', () => {
  74. assert.equal(shortDigest(null), '-')
  75. assert.equal(shortDigest('a'.repeat(64)), 'aaaaaaaaaaaa…aaaaaaaa')
  76. })
  77. test('uses local calendar dates and never seals a future window', () => {
  78. const now = new Date(2026, 6, 30, 1, 30, 0, 0)
  79. assert.deepEqual(defaultAuditWindow(now), {
  80. startDate: '2026-06-30',
  81. endDate: '2026-07-30'
  82. })
  83. assert.deepEqual(toAuditWindow('2026-07-01', '2026-07-30', now), {
  84. period_start: new Date(2026, 6, 1, 0, 0, 0, 0).toISOString(),
  85. period_end: now.toISOString()
  86. })
  87. assert.equal(
  88. toAuditWindow('2026-07-01', '2026-07-29', now).period_end,
  89. new Date(2026, 6, 29, 23, 59, 59, 999).toISOString()
  90. )
  91. })