Developer tools dump stack traces, exit codes, and jargon. Figuring out *what to do next* often means searching the same Stack Overflow answers again.
| 1 | """Tests for input normalization and secret redaction.""" |
| 2 | |
| 3 | from __future__ import annotations |
| 4 | |
| 5 | from why_failed.normalize import normalize, redact_secrets, strip_ansi |
| 6 | |
| 7 | |
| 8 | def test_strip_ansi() -> None: |
| 9 | colored = "\x1b[31mError\x1b[0m: failed" |
| 10 | assert strip_ansi(colored) == "Error: failed" |
| 11 | |
| 12 | |
| 13 | def test_crlf_normalization() -> None: |
| 14 | result = normalize("line1\r\nline2\r\n") |
| 15 | assert "\r" not in result.text |
| 16 | assert "line1\nline2" == result.text |
| 17 | |
| 18 | |
| 19 | def test_whitespace_collapse() -> None: |
| 20 | result = normalize("error: too many spaces") |
| 21 | assert " " not in result.text |
| 22 | assert "error: too many spaces" == result.text |
| 23 | |
| 24 | |
| 25 | def test_excessive_blank_lines() -> None: |
| 26 | result = normalize("a\n\n\n\n\nb") |
| 27 | assert result.text == "a\n\nb" |
| 28 | |
| 29 | |
| 30 | def test_empty_input() -> None: |
| 31 | result = normalize(" \n\n ") |
| 32 | assert result.text == "" |
| 33 | |
| 34 | |
| 35 | def test_unicode() -> None: |
| 36 | result = normalize("错误: 连接被拒绝 🚫") |
| 37 | assert "错误" in result.text |
| 38 | assert "连接被拒绝" in result.text |
| 39 | |
| 40 | |
| 41 | def test_long_input_truncation() -> None: |
| 42 | huge = "START\n" + ("x" * 200_000) + "\nEND_MARKER" |
| 43 | result = normalize(huge, max_bytes=100 * 1024) |
| 44 | assert result.truncated is True |
| 45 | assert "START" in result.text |
| 46 | assert "END_MARKER" in result.text |
| 47 | assert "truncated" in result.text.lower() |
| 48 | |
| 49 | |
| 50 | def test_small_input_not_truncated() -> None: |
| 51 | result = normalize("short error") |
| 52 | assert result.truncated is False |
| 53 | assert result.text == "short error" |
| 54 | |
| 55 | |
| 56 | def test_null_bytes_removed() -> None: |
| 57 | result = normalize("err\x00or") |
| 58 | assert "\x00" not in result.text |
| 59 | assert "error" == result.text |
| 60 | |
| 61 | |
| 62 | def test_prompt_prefix_stripped() -> None: |
| 63 | result = normalize("$ npm install\nnpm ERR! boom") |
| 64 | assert result.text.startswith("npm") or "npm ERR! boom" in result.text |
| 65 | |
| 66 | |
| 67 | def test_redact_bearer_token() -> None: |
| 68 | text = "Authorization: Bearer sk-secret-value-here" |
| 69 | assert "[REDACTED]" in redact_secrets(text) |
| 70 | assert "sk-secret-value-here" not in redact_secrets(text) |
| 71 | |
| 72 | |
| 73 | def test_redact_api_key() -> None: |
| 74 | text = "OPENAI_API_KEY=sk-abcdefghijklmnopqrstuvwxyz" |
| 75 | redacted = redact_secrets(text) |
| 76 | assert "[REDACTED]" in redacted |
| 77 | assert "sk-abcdefghijklmnopqrstuvwxyz" not in redacted |
| 78 | |
| 79 | |
| 80 | def test_redact_password() -> None: |
| 81 | text = "password=supersecret123" |
| 82 | assert "supersecret123" not in redact_secrets(text) |
| 83 | |
| 84 | |
| 85 | def test_redact_mongodb_uri() -> None: |
| 86 | text = "mongodb://user:s3cret@localhost:27017/db" |
| 87 | redacted = redact_secrets(text) |
| 88 | assert "[REDACTED]" in redacted |
| 89 | assert "s3cret" not in redacted |
| 90 | assert "user" in redacted |
| 91 | |
| 92 | |
| 93 | def test_redact_multiline() -> None: |
| 94 | text = "line1\nAuthorization: Bearer tok_abc\npassword=xyz\nline4" |
| 95 | redacted = redact_secrets(text) |
| 96 | assert "tok_abc" not in redacted |
| 97 | assert "xyz" not in redacted |
| 98 | assert "line1" in redacted |
| 99 | |
| 100 | |
| 101 | def test_normalize_redacts_secrets() -> None: |
| 102 | result = normalize("AWS_SECRET_ACCESS_KEY=abcd1234secret") |
| 103 | assert "abcd1234secret" not in result.text |
| 104 | assert "[REDACTED]" in result.text |
| 105 | |
| 106 | |
| 107 | def test_normalize_preserves_npm_eresolve() -> None: |
| 108 | raw = "npm ERR! ERESOLVE unable to resolve dependency tree" |
| 109 | result = normalize(raw) |
| 110 | assert "ERESOLVE" in result.text |
| 111 | assert "unable to resolve dependency tree" in result.text |