|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +// runDetectionForPromptContext runs a full detection pass against a fake copilot |
| 11 | +// that records a safe verdict, returning the parsed prompt_built log record and |
| 12 | +// the captured stderr. Extra args are appended before the artifacts dir. |
| 13 | +func runDetectionForPromptContext(t *testing.T, env map[string]string, extraArgs ...string) (map[string]any, string) { |
| 14 | + t.Helper() |
| 15 | + artifactsDir := t.TempDir() |
| 16 | + logPath := filepath.Join(t.TempDir(), "run.jsonl") |
| 17 | + marker := filepath.Join(t.TempDir(), "copilot-called") |
| 18 | + sinkJSON := `{"prompt_injection":false,"secret_leak":false,"malicious_patch":false,"reasons":[]}` |
| 19 | + fakeBinDir := writeFakeCopilotWithSink(t, marker, sinkJSON, 0) |
| 20 | + |
| 21 | + fullEnv := map[string]string{ |
| 22 | + "PATH": fakeBinDir + string(os.PathListSeparator) + os.Getenv("PATH"), |
| 23 | + // Clear inherited workflow-context vars so the suite is deterministic |
| 24 | + // regardless of the developer's shell; cases overlay explicit values. |
| 25 | + "WORKFLOW_NAME": "", |
| 26 | + "WORKFLOW_DESCRIPTION": "", |
| 27 | + "CUSTOM_PROMPT": "", |
| 28 | + } |
| 29 | + for k, v := range env { |
| 30 | + fullEnv[k] = v |
| 31 | + } |
| 32 | + |
| 33 | + args := append([]string{"threat-detect", "-log-file", logPath}, extraArgs...) |
| 34 | + args = append(args, artifactsDir) |
| 35 | + |
| 36 | + code, stderr := runWithTestArgsCapture(t, args, fullEnv) |
| 37 | + if code != exitSafe { |
| 38 | + t.Fatalf("run() exit code = %d, want %d; stderr:\n%s", code, exitSafe, stderr) |
| 39 | + } |
| 40 | + records := readJSONLRecords(t, logPath) |
| 41 | + built := findRecord(records, "prompt_built") |
| 42 | + if built == nil { |
| 43 | + t.Fatalf("missing prompt_built record: %#v", records) |
| 44 | + } |
| 45 | + return built, stderr |
| 46 | +} |
| 47 | + |
| 48 | +func TestPromptContextDefaultsAreDiagnosable(t *testing.T) { |
| 49 | + built, stderr := runDetectionForPromptContext(t, nil) |
| 50 | + |
| 51 | + if built["workflow_name_defaulted"] != true { |
| 52 | + t.Errorf("workflow_name_defaulted = %v, want true", built["workflow_name_defaulted"]) |
| 53 | + } |
| 54 | + if built["workflow_description_defaulted"] != true { |
| 55 | + t.Errorf("workflow_description_defaulted = %v, want true", built["workflow_description_defaulted"]) |
| 56 | + } |
| 57 | + if built["custom_prompt_applied"] != false { |
| 58 | + t.Errorf("custom_prompt_applied = %v, want false", built["custom_prompt_applied"]) |
| 59 | + } |
| 60 | + if built["custom_prompt_source"] != "none" { |
| 61 | + t.Errorf("custom_prompt_source = %v, want none", built["custom_prompt_source"]) |
| 62 | + } |
| 63 | + if !strings.Contains(stderr, "custom_prompt_source=none") { |
| 64 | + t.Errorf("stderr missing prompt context diagnostic, got:\n%s", stderr) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestPromptContextEnvCustomPrompt(t *testing.T) { |
| 69 | + built, _ := runDetectionForPromptContext(t, map[string]string{ |
| 70 | + "CUSTOM_PROMPT": "check for base64 blobs", |
| 71 | + "WORKFLOW_NAME": "My Flow", |
| 72 | + "WORKFLOW_DESCRIPTION": "does things", |
| 73 | + }) |
| 74 | + |
| 75 | + if built["workflow_name"] != "My Flow" { |
| 76 | + t.Errorf("workflow_name = %v, want My Flow", built["workflow_name"]) |
| 77 | + } |
| 78 | + if built["workflow_name_defaulted"] != false { |
| 79 | + t.Errorf("workflow_name_defaulted = %v, want false", built["workflow_name_defaulted"]) |
| 80 | + } |
| 81 | + if built["custom_prompt_applied"] != true { |
| 82 | + t.Errorf("custom_prompt_applied = %v, want true", built["custom_prompt_applied"]) |
| 83 | + } |
| 84 | + if built["custom_prompt_source"] != "env" { |
| 85 | + t.Errorf("custom_prompt_source = %v, want env", built["custom_prompt_source"]) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestPromptContextFlagOverridesEnv(t *testing.T) { |
| 90 | + built, _ := runDetectionForPromptContext(t, map[string]string{ |
| 91 | + "CUSTOM_PROMPT": "from env", |
| 92 | + "WORKFLOW_NAME": "env name", |
| 93 | + }, |
| 94 | + "-custom-prompt", "from flag", |
| 95 | + "-workflow-name", "flag name", |
| 96 | + "-workflow-description", "flag desc", |
| 97 | + ) |
| 98 | + |
| 99 | + if built["workflow_name"] != "flag name" { |
| 100 | + t.Errorf("workflow_name = %v, want flag name", built["workflow_name"]) |
| 101 | + } |
| 102 | + if built["workflow_description"] != "flag desc" { |
| 103 | + t.Errorf("workflow_description = %v, want flag desc", built["workflow_description"]) |
| 104 | + } |
| 105 | + if built["custom_prompt_source"] != "flag" { |
| 106 | + t.Errorf("custom_prompt_source = %v, want flag", built["custom_prompt_source"]) |
| 107 | + } |
| 108 | +} |
| 109 | + |
| 110 | +func TestPromptContextCustomPromptFileWins(t *testing.T) { |
| 111 | + promptFile := filepath.Join(t.TempDir(), "extra.md") |
| 112 | + if err := os.WriteFile(promptFile, []byte("from file"), 0o644); err != nil { |
| 113 | + t.Fatalf("writing custom prompt file: %v", err) |
| 114 | + } |
| 115 | + |
| 116 | + built, _ := runDetectionForPromptContext(t, map[string]string{ |
| 117 | + "CUSTOM_PROMPT": "from env", |
| 118 | + }, |
| 119 | + "-custom-prompt", "from flag", |
| 120 | + "-custom-prompt-file", promptFile, |
| 121 | + ) |
| 122 | + |
| 123 | + if built["custom_prompt_source"] != "file" { |
| 124 | + t.Errorf("custom_prompt_source = %v, want file", built["custom_prompt_source"]) |
| 125 | + } |
| 126 | + if bytes, ok := built["custom_prompt_bytes"].(float64); !ok || int(bytes) != len("from file") { |
| 127 | + t.Errorf("custom_prompt_bytes = %v, want %d", built["custom_prompt_bytes"], len("from file")) |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +func TestPromptContextExplicitDefaultTextIsNotDefaulted(t *testing.T) { |
| 132 | + // Supplying the fallback text explicitly must not be misreported as defaulted. |
| 133 | + built, stderr := runDetectionForPromptContext(t, nil, |
| 134 | + "-workflow-name", "Unnamed Workflow", |
| 135 | + "-workflow-description", "No description provided", |
| 136 | + ) |
| 137 | + |
| 138 | + if built["workflow_name_defaulted"] != false { |
| 139 | + t.Errorf("workflow_name_defaulted = %v, want false", built["workflow_name_defaulted"]) |
| 140 | + } |
| 141 | + if built["workflow_description_defaulted"] != false { |
| 142 | + t.Errorf("workflow_description_defaulted = %v, want false", built["workflow_description_defaulted"]) |
| 143 | + } |
| 144 | + if !strings.Contains(stderr, "workflow_description=\"No description provided\"") { |
| 145 | + t.Errorf("stderr missing resolved description, got:\n%s", stderr) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestPromptContextEmptyFlagClearsEnvCustomPrompt(t *testing.T) { |
| 150 | + // An explicit empty --custom-prompt must win over CUSTOM_PROMPT (flags win), |
| 151 | + // clearing the applied prompt while recording the flag as its provenance. |
| 152 | + built, _ := runDetectionForPromptContext(t, map[string]string{ |
| 153 | + "CUSTOM_PROMPT": "from env", |
| 154 | + }, "-custom-prompt", "") |
| 155 | + |
| 156 | + if built["custom_prompt_applied"] != false { |
| 157 | + t.Errorf("custom_prompt_applied = %v, want false", built["custom_prompt_applied"]) |
| 158 | + } |
| 159 | + if built["custom_prompt_source"] != "flag" { |
| 160 | + t.Errorf("custom_prompt_source = %v, want flag", built["custom_prompt_source"]) |
| 161 | + } |
| 162 | + if bytes, ok := built["custom_prompt_bytes"].(float64); !ok || int(bytes) != 0 { |
| 163 | + t.Errorf("custom_prompt_bytes = %v, want 0", built["custom_prompt_bytes"]) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func TestPromptContextRejectsUnreadableCustomPromptFile(t *testing.T) { |
| 168 | + missing := filepath.Join(t.TempDir(), "does-not-exist.md") |
| 169 | + |
| 170 | + code, stderr := runWithTestArgsCapture(t, []string{ |
| 171 | + "threat-detect", |
| 172 | + "-custom-prompt-file", missing, |
| 173 | + t.TempDir(), |
| 174 | + }, nil) |
| 175 | + |
| 176 | + if code != exitError { |
| 177 | + t.Fatalf("run() exit code = %d, want %d", code, exitError) |
| 178 | + } |
| 179 | + if !strings.Contains(stderr, "Error reading custom prompt file") { |
| 180 | + t.Fatalf("stderr missing custom prompt read error, got:\n%s", stderr) |
| 181 | + } |
| 182 | + if !strings.Contains(stderr, "reason=config_error") { |
| 183 | + t.Fatalf("stderr missing config_error status, got:\n%s", stderr) |
| 184 | + } |
| 185 | +} |
0 commit comments