File size: 3,001 Bytes
3201ca6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import { describe, it, expect } from "vitest";
import { buildAutomationDebugPrompt } from "#/utils/automation-debug-prompt";

describe("buildAutomationDebugPrompt", () => {
  it("includes the automation name, its instructions, the stderr, and the run id", () => {
    // Arrange
    const input = {
      automationName: "Daily Jira digest",
      automationPrompt: "Summarize new Jira issues each morning",
      errorDetail: "Process exited with code 1",
      stderr: "Traceback ...\nHTTP Error 410: Gone",
      runId: "run-42",
    };

    // Act
    const prompt = buildAutomationDebugPrompt(input);

    // Assert
    expect(prompt).toContain("Daily Jira digest");
    expect(prompt).toContain("Summarize new Jira issues each morning");
    expect(prompt).toContain("HTTP Error 410: Gone");
    expect(prompt).toContain("run-42");
    // The live stderr is preferred over the run-level error_detail.
    expect(prompt).not.toContain("Process exited with code 1");
  });

  it("falls back to error_detail when stderr is blank", () => {
    // Arrange — sandbox gone, so no stderr was fetched.
    const prompt = buildAutomationDebugPrompt({
      automationName: "Daily Jira digest",
      automationPrompt: "Summarize new Jira issues",
      errorDetail: "Process exited with code 1",
      stderr: "   \n  ",
      runId: "run-42",
    });

    // Assert
    expect(prompt).toContain("Process exited with code 1");
    expect(prompt).not.toContain("No error output");
  });

  it("omits the instructions section when the automation prompt is absent", () => {
    // Act
    const prompt = buildAutomationDebugPrompt({
      automationName: "Daily Jira digest",
      automationPrompt: null,
      errorDetail: "boom",
      stderr: "",
      runId: "run-42",
    });

    // Assert
    expect(prompt).not.toContain("What the automation was set up to do");
  });

  it("reports missing output when neither stderr nor error_detail is available", () => {
    // Act
    const prompt = buildAutomationDebugPrompt({
      automationName: "Daily Jira digest",
      automationPrompt: "Summarize new Jira issues",
      errorDetail: null,
      stderr: "",
      runId: "run-42",
    });

    // Assert
    expect(prompt).toContain("No error output was captured");
  });

  it("truncates oversized stderr, keeping the tail", () => {
    // Arrange — an error stream well over the 4000-char cap, with distinct
    // head and tail markers so we can prove which end survives.
    const stderr = `HEAD_MARKER${"x".repeat(5000)}TAIL_MARKER`;

    // Act
    const prompt = buildAutomationDebugPrompt({
      automationName: "Daily Jira digest",
      automationPrompt: "Summarize new Jira issues",
      errorDetail: null,
      stderr,
      runId: "run-42",
    });

    // Assert — the tail (the actual error) is kept, the head is dropped.
    expect(prompt).toContain("TAIL_MARKER");
    expect(prompt).toContain("…(truncated)…");
    expect(prompt).not.toContain("HEAD_MARKER");
  });
});