openai_automation_bootstrap_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package handler
  2. import (
  3. "strings"
  4. "testing"
  5. "github.com/stretchr/testify/require"
  6. "github.com/tidwall/gjson"
  7. )
  8. const automationBootstrapPrompt = "Review the project and report any important changes."
  9. func codexAutomationBootstrap(automationID, lastRun, prompt string) string {
  10. return "Automation: Scheduled project review\n" +
  11. "Automation ID: " + automationID + "\n" +
  12. "Automation memory: $CODEX_HOME/automations/" + automationID + "/memory.md\n" +
  13. "Last run: " + lastRun + "\n\n" + prompt
  14. }
  15. func codexAutomationBootstrapBody(t *testing.T, output, callID string) []byte {
  16. t.Helper()
  17. return []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` +
  18. mustJSON(t, output) + callID + `}]}`)
  19. }
  20. func TestNormalizeCodexAutomationBootstrapSupportedLastRunValues(t *testing.T) {
  21. tests := []struct {
  22. name string
  23. lastRun string
  24. crlf bool
  25. }{
  26. {name: "never", lastRun: "never"},
  27. {name: "timestamp", lastRun: "2026-09-01T02:06:34.536Z (1788228394536)"},
  28. {name: "crlf", lastRun: "never", crlf: true},
  29. }
  30. for _, tt := range tests {
  31. t.Run(tt.name, func(t *testing.T) {
  32. output := codexAutomationBootstrap("wiki-maintenance", tt.lastRun, automationBootstrapPrompt)
  33. if tt.crlf {
  34. output = strings.ReplaceAll(output, "\n", "\r\n")
  35. }
  36. got, changed := normalizeCodexAutomationBootstrap(codexAutomationBootstrapBody(t, output, ""))
  37. require.True(t, changed)
  38. require.Equal(t, "message", gjson.GetBytes(got, "input.0.type").String())
  39. require.Equal(t, "user", gjson.GetBytes(got, "input.0.role").String())
  40. require.Equal(t, output, gjson.GetBytes(got, "input.0.content.0.text").String())
  41. require.False(t, gjson.GetBytes(got, "input.0.call_id").Exists())
  42. })
  43. }
  44. }
  45. func TestNormalizeCodexAutomationBootstrapHeartbeat(t *testing.T) {
  46. output := `<heartbeat><automation_id>wiki</automation_id></heartbeat>`
  47. got, changed := normalizeCodexAutomationBootstrap(codexAutomationBootstrapBody(t, output, ""))
  48. require.True(t, changed)
  49. require.Equal(t, "message", gjson.GetBytes(got, "input.0.type").String())
  50. require.Equal(t, "user", gjson.GetBytes(got, "input.0.role").String())
  51. require.Equal(t, output, gjson.GetBytes(got, "input.0.content.0.text").String())
  52. require.False(t, gjson.GetBytes(got, "input.0.call_id").Exists())
  53. again, changedAgain := normalizeCodexAutomationBootstrap(got)
  54. require.False(t, changedAgain)
  55. require.Equal(t, got, again)
  56. }
  57. func TestNormalizeCodexAutomationBootstrapRejectsUnsafeShapes(t *testing.T) {
  58. validOutput := codexAutomationBootstrap("wiki", "never", automationBootstrapPrompt)
  59. tests := []struct {
  60. name string
  61. body []byte
  62. }{
  63. {
  64. name: "ordinary missing call id",
  65. body: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","namespace":"codex_app","name":"other","output":` + mustJSON(t, validOutput) + `}]}`),
  66. },
  67. {
  68. name: "tui namespace",
  69. body: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","namespace":"codex_tui","name":"automation_update","output":` + mustJSON(t, validOutput) + `}]}`),
  70. },
  71. {
  72. name: "valid call id",
  73. body: codexAutomationBootstrapBody(t, validOutput, `,"call_id":"call-1"`),
  74. },
  75. {
  76. name: "previous response",
  77. body: []byte(`{"model":"gpt-5","previous_response_id":"resp-1","input":[{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + mustJSON(t, validOutput) + `}]}`),
  78. },
  79. {
  80. name: "real call context",
  81. body: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + mustJSON(t, validOutput) + `},{"type":"function_call","call_id":"call-1"}]}`),
  82. },
  83. {
  84. name: "mismatched memory id",
  85. body: codexAutomationBootstrapBody(t, strings.Replace(validOutput, "/wiki/memory.md", "/other/memory.md", 1), ""),
  86. },
  87. {
  88. name: "unsafe automation id",
  89. body: codexAutomationBootstrapBody(t, codexAutomationBootstrap("../wiki", "never", automationBootstrapPrompt), ""),
  90. },
  91. {
  92. name: "invalid timestamp",
  93. body: codexAutomationBootstrapBody(t, codexAutomationBootstrap("wiki", "yesterday", automationBootstrapPrompt), ""),
  94. },
  95. {
  96. name: "mismatched timestamp epoch",
  97. body: codexAutomationBootstrapBody(t, codexAutomationBootstrap("wiki", "2026-09-01T02:06:34.536Z (1)", automationBootstrapPrompt), ""),
  98. },
  99. {
  100. name: "missing separator",
  101. body: codexAutomationBootstrapBody(t, strings.Replace(validOutput, "\n\n"+automationBootstrapPrompt, "\n"+automationBootstrapPrompt, 1), ""),
  102. },
  103. {
  104. name: "empty prompt",
  105. body: codexAutomationBootstrapBody(t, codexAutomationBootstrap("wiki", "never", " \n"), ""),
  106. },
  107. {
  108. name: "mixed missing call id output",
  109. body: []byte(`{"model":"gpt-5","input":[{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + mustJSON(t, validOutput) + `},{"type":"computer_call_output","output":"done"}]}`),
  110. },
  111. }
  112. for _, tt := range tests {
  113. t.Run(tt.name, func(t *testing.T) {
  114. got, changed := normalizeCodexAutomationBootstrap(tt.body)
  115. require.False(t, changed)
  116. require.Equal(t, tt.body, got)
  117. })
  118. }
  119. }
  120. func TestNormalizeCodexAutomationBootstrapRejectsUnsafeHeartbeatShapes(t *testing.T) {
  121. tests := []struct {
  122. name string
  123. output string
  124. }{
  125. {name: "arbitrary tool output", output: `<result><automation_id>wiki</automation_id></result>`},
  126. {name: "root attribute", output: `<heartbeat status="ok"><automation_id>wiki</automation_id></heartbeat>`},
  127. {name: "namespaced root", output: `<heartbeat xmlns="urn:codex"><automation_id>wiki</automation_id></heartbeat>`},
  128. {name: "extra child", output: `<heartbeat><automation_id>wiki</automation_id><status>ok</status></heartbeat>`},
  129. {name: "nested id content", output: `<heartbeat><automation_id><value>wiki</value></automation_id></heartbeat>`},
  130. {name: "padded id", output: `<heartbeat><automation_id> wiki </automation_id></heartbeat>`},
  131. {name: "unsafe id", output: `<heartbeat><automation_id>../wiki</automation_id></heartbeat>`},
  132. {name: "comment", output: `<heartbeat><!-- ok --><automation_id>wiki</automation_id></heartbeat>`},
  133. {name: "trailing content", output: `<heartbeat><automation_id>wiki</automation_id></heartbeat>extra`},
  134. }
  135. for _, tt := range tests {
  136. t.Run(tt.name, func(t *testing.T) {
  137. body := codexAutomationBootstrapBody(t, tt.output, "")
  138. got, changed := normalizeCodexAutomationBootstrap(body)
  139. require.False(t, changed)
  140. require.Equal(t, body, got)
  141. })
  142. }
  143. }
  144. func TestNormalizeCodexAutomationBootstrapPreservesOrderAndIsIdempotent(t *testing.T) {
  145. output := codexAutomationBootstrap("wiki", "never", automationBootstrapPrompt)
  146. body := []byte(`{"model":"gpt-5","input":[{"type":"message","role":"user","content":"before"},{"type":"function_call_output","namespace":"codex_app","name":"automation_update","output":` + mustJSON(t, output) + `},{"type":"message","role":"user","content":"after"}]}`)
  147. got, changed := normalizeCodexAutomationBootstrap(body)
  148. require.True(t, changed)
  149. require.Equal(t, "before", gjson.GetBytes(got, "input.0.content").String())
  150. require.Equal(t, output, gjson.GetBytes(got, "input.1.content.0.text").String())
  151. require.Equal(t, "after", gjson.GetBytes(got, "input.2.content").String())
  152. again, changedAgain := normalizeCodexAutomationBootstrap(got)
  153. require.False(t, changedAgain)
  154. require.Equal(t, got, again)
  155. }