|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +// assertUsageEnvelope checks stderr carries the canonical error envelope with |
| 10 | +// the usage_error code, matching the repo convention of asserting envelope |
| 11 | +// shape (not just exit code) on failures. |
| 12 | +func assertUsageEnvelope(t *testing.T, stderr string) { |
| 13 | + t.Helper() |
| 14 | + var envelope map[string]map[string]any |
| 15 | + if err := json.Unmarshal([]byte(stderr), &envelope); err != nil { |
| 16 | + t.Fatalf("stderr is not valid JSON: %v\nstderr: %s", err, stderr) |
| 17 | + } |
| 18 | + if envelope["error"]["code"] != "usage_error" { |
| 19 | + t.Errorf("error.code = %v, want %q", envelope["error"]["code"], "usage_error") |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestFeedback_Valid(t *testing.T) { |
| 24 | + // No server and no API key: feedback is skipAuth, so it must succeed |
| 25 | + // without credentials. Analytics is disabled in runCommand, so the event |
| 26 | + // isn't sent — the command should report that rather than a false thanks. |
| 27 | + res := runCommand(t, "", "", "feedback", "--rating", "4", "--comment", "nice") |
| 28 | + if res.ExitCode != 0 { |
| 29 | + t.Fatalf("exit = %d, want 0 (stderr: %s)", res.ExitCode, res.Stderr) |
| 30 | + } |
| 31 | + |
| 32 | + var got feedbackResponse |
| 33 | + if err := json.Unmarshal([]byte(res.Stdout), &got); err != nil { |
| 34 | + t.Fatalf("stdout is not valid JSON: %v\n%s", err, res.Stdout) |
| 35 | + } |
| 36 | + if got.Rating != 4 { |
| 37 | + t.Fatalf("rating = %d, want 4", got.Rating) |
| 38 | + } |
| 39 | + if got.Comment != "nice" { |
| 40 | + t.Fatalf("comment = %q, want %q", got.Comment, "nice") |
| 41 | + } |
| 42 | + if !strings.Contains(got.Message, "Analytics is disabled") { |
| 43 | + t.Fatalf("message = %q, want it to report analytics disabled", got.Message) |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +func TestFeedback_RatingOnly(t *testing.T) { |
| 48 | + res := runCommand(t, "", "", "feedback", "--rating", "5") |
| 49 | + if res.ExitCode != 0 { |
| 50 | + t.Fatalf("exit = %d, want 0 (stderr: %s)", res.ExitCode, res.Stderr) |
| 51 | + } |
| 52 | + if !json.Valid([]byte(res.Stdout)) { |
| 53 | + t.Fatalf("stdout is not valid JSON: %s", res.Stdout) |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +func TestFeedback_MissingRating(t *testing.T) { |
| 58 | + res := runCommand(t, "", "", "feedback") |
| 59 | + if res.ExitCode != 2 { |
| 60 | + t.Fatalf("exit = %d, want 2 (stdout: %s)", res.ExitCode, res.Stdout) |
| 61 | + } |
| 62 | + assertUsageEnvelope(t, res.Stderr) |
| 63 | +} |
| 64 | + |
| 65 | +func TestFeedback_CommentTooLong(t *testing.T) { |
| 66 | + long := strings.Repeat("x", 2001) |
| 67 | + res := runCommand(t, "", "", "feedback", "--rating", "3", "--comment", long) |
| 68 | + if res.ExitCode != 2 { |
| 69 | + t.Fatalf("exit = %d, want 2 (stdout: %s)", res.ExitCode, res.Stdout) |
| 70 | + } |
| 71 | + assertUsageEnvelope(t, res.Stderr) |
| 72 | +} |
| 73 | + |
| 74 | +func TestFeedback_CommentAtCap(t *testing.T) { |
| 75 | + atCap := strings.Repeat("x", 2000) |
| 76 | + res := runCommand(t, "", "", "feedback", "--rating", "3", "--comment", atCap) |
| 77 | + if res.ExitCode != 0 { |
| 78 | + t.Fatalf("exit = %d, want 0 (2000 chars is allowed) (stderr: %s)", res.ExitCode, res.Stderr) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +func TestFeedback_CommentCapCountsRunesNotBytes(t *testing.T) { |
| 83 | + // 2000 multibyte runes is 4000 bytes; it must pass because the cap is on |
| 84 | + // characters, not bytes. |
| 85 | + multibyte := strings.Repeat("é", 2000) |
| 86 | + res := runCommand(t, "", "", "feedback", "--rating", "3", "--comment", multibyte) |
| 87 | + if res.ExitCode != 0 { |
| 88 | + t.Fatalf("exit = %d, want 0 (2000 runes is allowed) (stderr: %s)", res.ExitCode, res.Stderr) |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +func TestFeedback_RatingOutOfRange(t *testing.T) { |
| 93 | + for _, r := range []string{"0", "6", "9"} { |
| 94 | + res := runCommand(t, "", "", "feedback", "--rating", r) |
| 95 | + if res.ExitCode != 2 { |
| 96 | + t.Fatalf("rating %s: exit = %d, want 2 (stdout: %s)", r, res.ExitCode, res.Stdout) |
| 97 | + } |
| 98 | + assertUsageEnvelope(t, res.Stderr) |
| 99 | + if !strings.Contains(res.Stderr, "rating must be between 1 and 5") { |
| 100 | + t.Errorf("rating %s: stderr missing range message: %s", r, res.Stderr) |
| 101 | + } |
| 102 | + } |
| 103 | +} |
0 commit comments