rerunArguments.test.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import {
  4. buildRerunPrefilledArguments,
  5. buildRerunStartActionArgs,
  6. hasMissingRerunArguments,
  7. logEntryArgumentsToStartActionArgs,
  8. rerunNeedsArgumentForm
  9. } from '../utils/rerunArguments.js'
  10. test('logEntryArgumentsToStartActionArgs maps proto arguments for StartAction', () => {
  11. assert.deepEqual(
  12. logEntryArgumentsToStartActionArgs({
  13. arguments: [
  14. { name: 'host', value: 'example.com' },
  15. { name: 'port', value: '443' }
  16. ]
  17. }),
  18. [
  19. { name: 'host', value: 'example.com' },
  20. { name: 'port', value: '443' }
  21. ]
  22. )
  23. })
  24. test('hasMissingRerunArguments requires password fields to be re-entered', () => {
  25. const action = {
  26. arguments: [
  27. { name: 'user', type: 'ascii_identifier' },
  28. { name: 'pass', type: 'password' }
  29. ]
  30. }
  31. assert.equal(
  32. hasMissingRerunArguments(action, [{ name: 'user', value: 'alice' }]),
  33. true
  34. )
  35. })
  36. test('hasMissingRerunArguments requires very_dangerous_raw_string fields to be re-entered', () => {
  37. const action = {
  38. arguments: [
  39. { name: 'host', type: 'ascii_identifier' },
  40. { name: 'payload', type: 'very_dangerous_raw_string' }
  41. ]
  42. }
  43. assert.equal(
  44. hasMissingRerunArguments(action, [{ name: 'host', value: 'db-1' }]),
  45. true
  46. )
  47. })
  48. test('hasMissingRerunArguments detects missing stored args without relying on required flag', () => {
  49. // Proto ActionArgument has no `required` field — only defaultValue.
  50. const action = {
  51. arguments: [{ name: 'host', type: 'ascii_identifier' }]
  52. }
  53. assert.equal(hasMissingRerunArguments(action, []), true)
  54. assert.equal(
  55. hasMissingRerunArguments(action, [{ name: 'host', value: 'db-1' }]),
  56. false
  57. )
  58. })
  59. test('hasMissingRerunArguments treats empty defaultValue as required', () => {
  60. const action = {
  61. arguments: [{ name: 'host', type: 'ascii_identifier', defaultValue: '' }]
  62. }
  63. assert.equal(hasMissingRerunArguments(action, []), true)
  64. })
  65. test('hasMissingRerunArguments allows args that have a defaultValue', () => {
  66. const action = {
  67. arguments: [{ name: 'host', type: 'ascii_identifier', defaultValue: 'example.com' }]
  68. }
  69. assert.equal(hasMissingRerunArguments(action, []), false)
  70. })
  71. test('hasMissingRerunArguments ignores confirmation and html args', () => {
  72. const action = {
  73. arguments: [
  74. { name: 'confirm', type: 'confirmation' },
  75. { name: 'help', type: 'html' },
  76. { name: 'host', type: 'ascii_identifier' }
  77. ]
  78. }
  79. assert.equal(
  80. hasMissingRerunArguments(action, [{ name: 'host', value: 'db-1' }]),
  81. false
  82. )
  83. })
  84. test('rerunNeedsArgumentForm can start directly when stored args are complete', () => {
  85. const action = {
  86. arguments: [{ name: 'host', type: 'ascii_identifier' }]
  87. }
  88. const logEntry = {
  89. arguments: [{ name: 'host', value: 'db-1' }]
  90. }
  91. assert.equal(rerunNeedsArgumentForm(action, logEntry), false)
  92. })
  93. test('rerunNeedsArgumentForm always opens the form when justification is required', () => {
  94. const action = {
  95. justification: ' ',
  96. arguments: [{ name: 'host', type: 'ascii_identifier' }]
  97. }
  98. const logEntry = {
  99. arguments: [{ name: 'host', value: 'db-1' }],
  100. justification: 'approved change'
  101. }
  102. assert.equal(rerunNeedsArgumentForm(action, logEntry), true)
  103. assert.equal(rerunNeedsArgumentForm(action, {}), true)
  104. })
  105. test('buildRerunStartActionArgs uses stored arguments without justification', () => {
  106. assert.deepEqual(
  107. buildRerunStartActionArgs('binding-1', {
  108. arguments: [{ name: 'host', value: 'db-1' }],
  109. justification: 'maintenance window'
  110. }),
  111. {
  112. bindingId: 'binding-1',
  113. arguments: [{ name: 'host', value: 'db-1' }]
  114. }
  115. )
  116. })
  117. test('buildRerunPrefilledArguments maps stored args for history.state', () => {
  118. assert.deepEqual(
  119. buildRerunPrefilledArguments({
  120. arguments: [
  121. { name: 'host', value: 'db-1' },
  122. { name: 'port', value: '5432' }
  123. ]
  124. }),
  125. {
  126. host: 'db-1',
  127. port: '5432'
  128. }
  129. )
  130. })
  131. test('buildRerunPrefilledArguments returns empty object when nothing was stored', () => {
  132. assert.deepEqual(buildRerunPrefilledArguments({}), {})
  133. assert.deepEqual(buildRerunPrefilledArguments(undefined), {})
  134. })