返回 DeepSeek-Reasonix
crash_app_test.go
根目录 / desktop / crash_app_test.go
1 package main
2
3 import (
4 "context"
5 "encoding/json"
6 "io"
7 "net/http"
8 "net/http/httptest"
9 "reflect"
10 "strings"
11 "testing"
12 )
13
14 func TestScrubUserPaths(t *testing.T) {
15 cases := map[string]string{
16 `at C:\Users\yuhua\proj\app.ts:12:3`: `at C:\Users\_\proj\app.ts:12:3`,
17 `at c:\users\someone\x.go`: `at c:\users\_\x.go`,
18 `/home/bob/.reasonix/config.toml`: `/home/_/.reasonix/config.toml`,
19 `/Users/alice/Library/Logs`: `/Users/_/Library/Logs`,
20 `Error: ENOENT open '/home/bob/secret'`: `Error: ENOENT open '/home/_/secret'`,
21 `no user path here: /usr/lib/node`: `no user path here: /usr/lib/node`,
22 "first /home/a/x\nsecond C:\\Users\\b\\y": "first /home/_/x\nsecond C:\\Users\\_\\y",
23 }
24 for in, want := range cases {
25 if got := scrubUserPaths(in); got != want {
26 t.Errorf("scrubUserPaths(%q) = %q, want %q", in, got, want)
27 }
28 }
29 }
30
31 func TestScrubSensitiveText(t *testing.T) {
32 apiKey := "sk-proj-" + "abcdefghijklmnopqrstuvwxyz1234567890"
33 bearer := "abcdefghijklmnopqrstuvwxyz1234567890ABCDE"
34 longHex := "0123456789abcdef0123456789abcdef"
35 jwt := "eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjMifQ.signature"
36 got := scrubSensitiveText("user dev@example.com Authorization: Bearer " + bearer + " api_key=" + apiKey + " jwt " + jwt + " hash " + longHex + " env FEISHU_BOT_APP_SECRET WEIXIN_BOT_TOKEN short abc1234 path /Users/alice/x")
37
38 for _, leaked := range []string{"dev@example.com", bearer, apiKey, jwt, longHex, "FEISHU_BOT_APP_SECRET", "WEIXIN_BOT_TOKEN", "alice"} {
39 if strings.Contains(got, leaked) {
40 t.Fatalf("sensitive text leaked %q in %q", leaked, got)
41 }
42 }
43 for _, want := range []string{"[redacted-email]", "Authorization=[redacted]", "api_key=[redacted]", "[redacted-jwt]", "[redacted-hex]", "[redacted-env]", "short abc1234", "/Users/_/x"} {
44 if !strings.Contains(got, want) {
45 t.Fatalf("scrubSensitiveText() = %q, want it to contain %q", got, want)
46 }
47 }
48 }
49
50 func TestPostCrashReport(t *testing.T) {
51 var got crashReport
52 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
53 if r.Method != http.MethodPost {
54 t.Errorf("method = %s, want POST", r.Method)
55 }
56 if ct := r.Header.Get("Content-Type"); ct != "application/json" {
57 t.Errorf("content-type = %q", ct)
58 }
59 body, _ := io.ReadAll(r.Body)
60 if err := json.Unmarshal(body, &got); err != nil {
61 t.Errorf("body not JSON: %v", err)
62 }
63 w.WriteHeader(http.StatusAccepted)
64 }))
65 defer srv.Close()
66
67 r := crashReport{Kind: "crash", Version: "v9.9.9", OS: "windows", Arch: "amd64", Message: "[react]\nboom"}
68 if err := postCrashReport(context.Background(), srv.Client(), srv.URL, r); err != nil {
69 t.Fatal(err)
70 }
71 if !reflect.DeepEqual(got, r) {
72 t.Errorf("server received %+v, want %+v", got, r)
73 }
74 }
75
76 func TestPostCrashReportRejectedStatus(t *testing.T) {
77 srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
78 w.WriteHeader(http.StatusTooManyRequests)
79 }))
80 defer srv.Close()
81
82 err := postCrashReport(context.Background(), srv.Client(), srv.URL, crashReport{Kind: "crash"})
83 if err == nil || !strings.Contains(err.Error(), "429") {
84 t.Fatalf("want 429 error, got %v", err)
85 }
86 }
87
88 func TestReportCrashRejectsBadInput(t *testing.T) {
89 app := NewApp()
90 if err := app.ReportCrash("telemetry", "x"); err == nil {
91 t.Error("unknown kind should be rejected")
92 }
93 if err := app.ReportCrash("crash", ""); err == nil {
94 t.Error("empty detail should be rejected")
95 }
96 }
97
98 func TestCrashReportFromStructuredDetail(t *testing.T) {
99 apiKey := "sk-proj-" + "abcdefghijklmnopqrstuvwxyz1234567890"
100 secretHex := "abcdefabcdefabcdefabcdefabcdef12"
101 buildCommit := "0123456789abcdef0123456789abcdef01234567"
102 payload := frontendCrashPayload{
103 SchemaVersion: 2,
104 Kind: "exception",
105 Source: "frontend",
106 Label: "unhandledrejection",
107 Message: "[unhandledrejection]\n\ninvalid argument at C:\\Users\\alice\\app.ts:1 from alice@example.com",
108 ErrorType: "TypeError",
109 ErrorMessage: "invalid argument at /Users/alice/project/app.ts api_key=" + apiKey,
110 Stack: "TypeError: invalid argument\n at run (/Users/alice/project/app.ts:12:3)\nsecret=" + secretHex,
111 TopFrame: "at run (/Users/alice/project/app.ts:12:3)",
112 FingerprintHint: "build:01234567|view:app://reasonix/|cats:startup>tabs",
113 BuildCommit: buildCommit,
114 Channel: "canary",
115 Language: "zh-CN",
116 View: "wails://wails.localhost/index.html?token=" + secretHex,
117 Breadcrumbs: []crashBreadcrumb{{T: 1, Cat: "bridge", Msg: "turn SubmitToTab token=" + apiKey}},
118 }
119 detail, err := json.Marshal(payload)
120 if err != nil {
121 t.Fatal(err)
122 }
123 r, err := crashReportFromDetail("crash", string(detail))
124 if err != nil {
125 t.Fatal(err)
126 }
127 if r.Kind != "exception" || r.Source != "frontend" || r.Label != "unhandledrejection" {
128 t.Fatalf("structured fields not preserved: %+v", r)
129 }
130 if strings.Contains(r.Message, "alice") || strings.Contains(r.ErrorMessage, "alice") || strings.Contains(r.Stack, "alice") {
131 t.Fatalf("user path was not scrubbed: %+v", r)
132 }
133 if r.TopFrame == "" || r.FingerprintHint != payload.FingerprintHint || r.BuildCommit != buildCommit || r.Channel != "canary" || len(r.Breadcrumbs) != 1 {
134 t.Fatalf("metadata missing: %+v", r)
135 }
136 freeText := strings.Join([]string{
137 r.Message,
138 r.ErrorMessage,
139 r.Stack,
140 r.ComponentStack,
141 r.TopFrame,
142 r.View,
143 r.Breadcrumbs[0].Msg,
144 }, "\n")
145 for _, leaked := range []string{apiKey, secretHex, "alice@example.com"} {
146 if strings.Contains(freeText, leaked) {
147 t.Fatalf("sensitive value leaked %q in %+v", leaked, r)
148 }
149 }
150 }
151
152 func TestCrashReportFromPerformanceDetail(t *testing.T) {
153 payload := frontendCrashPayload{
154 SchemaVersion: 2,
155 Kind: "performance",
156 Source: "frontend.performance",
157 Label: "performance.pressure",
158 Message: "[performance.pressure]\n\n--- performance context ---\nreason: event loop lag 1300ms",
159 ErrorType: "PerformancePressure",
160 ErrorMessage: "UI responsiveness degraded because the app observed long tasks, event-loop lag, or high JS heap pressure.",
161 TopFrame: "frontend.performance",
162 }
163 detail, err := json.Marshal(payload)
164 if err != nil {
165 t.Fatal(err)
166 }
167 r, err := crashReportFromDetail("performance", string(detail))
168 if err != nil {
169 t.Fatal(err)
170 }
171 if r.Kind != "performance" || r.Source != "frontend.performance" || r.Label != "performance.pressure" {
172 t.Fatalf("performance fields not preserved: %+v", r)
173 }
174 if !strings.Contains(r.Message, "--- native runtime context ---") || !strings.Contains(r.Message, "goroutines:") {
175 t.Fatalf("native runtime context missing from performance report: %q", r.Message)
176 }
177 }
178
179 func TestCrashReportFromBotDetail(t *testing.T) {
180 token := "abcdefghijklmnopqrstuvwxyz1234567890ABCDE"
181 payload := frontendCrashPayload{
182 SchemaVersion: 2,
183 Kind: "bot",
184 Source: "bot.runtime",
185 Label: "bot.feishu.lark.send",
186 Message: "[bot]\n\nfailed at /Users/alice/project with token=" + token,
187 ErrorType: "BotConnectionDiagnostic",
188 ErrorMessage: "send failed with Bearer " + token,
189 TopFrame: "bot.send",
190 }
191 detail, err := json.Marshal(payload)
192 if err != nil {
193 t.Fatal(err)
194 }
195 r, err := crashReportFromDetail("bot", string(detail))
196 if err != nil {
197 t.Fatal(err)
198 }
199 if r.Kind != "bot" || r.Source != "bot.runtime" || r.Label != "bot.feishu.lark.send" {
200 t.Fatalf("bot fields not preserved: %+v", r)
201 }
202 if strings.Contains(r.Message, "alice") || strings.Contains(r.Message, token) || strings.Contains(r.ErrorMessage, token) {
203 t.Fatalf("bot report was not scrubbed: %+v", r)
204 }
205 if strings.Contains(r.Message, "--- native runtime context ---") {
206 t.Fatalf("bot report should not include performance runtime context: %q", r.Message)
207 }
208 }
209
209 lines GO