返回 DeepSeek-Reasonix
post_write_receipt_test.go
根目录 / internal / tool / builtin / post_write_receipt_test.go
1 package builtin
2
3 import (
4 "strings"
5 "testing"
6 "unicode/utf8"
7 )
8
9 func TestClipPostWriteSpanPreservesUTF8AndBothEnds(t *testing.T) {
10 text := "head\n" + strings.Repeat("修改后的内容\n", 800) + "tail\n"
11 got := clipPostWriteSpan(text, 512)
12
13 if len(got) > 512 {
14 t.Fatalf("clipped span = %d bytes, want at most 512", len(got))
15 }
16 if !utf8.ValidString(got) {
17 t.Fatal("clipped span split a UTF-8 rune")
18 }
19 for _, want := range []string{"head\n", "tail\n", postWriteSpanTruncated} {
20 if !strings.Contains(got, want) {
21 t.Fatalf("clipped span should preserve %q:\n%s", want, got)
22 }
23 }
24 }
25
26 func TestRenderPostWriteReceiptsKeepsFirstAndLastBounded(t *testing.T) {
27 receipts := []editReplacementReceipt{
28 {matched: "first-old", replacement: "first-new", occurrences: 1},
29 {matched: "middle-old", replacement: "middle-new", occurrences: 2},
30 {matched: strings.Repeat("最后一项", 500), replacement: "last-new", occurrences: 1, fuzzy: true},
31 }
32 got := renderPostWriteReceipts(receipts)
33
34 if len(got) > maxPostWriteReceiptBytes {
35 t.Fatalf("receipt = %d bytes, want at most %d", len(got), maxPostWriteReceiptBytes)
36 }
37 if !utf8.ValidString(got) {
38 t.Fatal("receipt split a UTF-8 rune")
39 }
40 for _, want := range []string{"-first-old", "+first-new", "+last-new", "1 intermediate replacement receipt(s) omitted", "fuzzy match", postWriteSpanTruncated} {
41 if !strings.Contains(got, want) {
42 t.Fatalf("receipt should contain %q:\n%s", want, got)
43 }
44 }
45 if strings.Contains(got, "middle-old") || strings.Contains(got, "middle-new") {
46 t.Fatalf("bounded receipt should omit intermediate spans:\n%s", got)
47 }
48 }
49
50 func TestPostWriteReceiptLargeSpanAllocationIsBounded(t *testing.T) {
51 large := strings.Repeat("large replacement payload\n", 700_000)
52 receipts := []editReplacementReceipt{{matched: large, replacement: large, occurrences: 1}}
53 var got string
54 result := testing.Benchmark(func(b *testing.B) {
55 for i := 0; i < b.N; i++ {
56 got = withActualPostWriteReceipts("edited large.txt", receipts)
57 }
58 })
59 if len(got) > maxPostWriteReceiptBytes+256 {
60 t.Fatalf("bounded result is too large: %d bytes", len(got))
61 }
62 if bytes := result.AllocedBytesPerOp(); bytes > 128<<10 {
63 t.Fatalf("receipt rendering allocated %d bytes/op for a large span, want at most 128 KiB", bytes)
64 }
65 }
66
67 func TestMatchedRangeSampleIsCapturedBounded(t *testing.T) {
68 content := strings.Repeat("actual fuzzy match content\n", 2_000)
69 got := matchedRangeSample(content, "fallback", []editRange{{start: 0, end: len(content)}})
70 if len(got) > maxCapturedReceiptSpanBytes {
71 t.Fatalf("captured fuzzy sample = %d bytes, want at most %d", len(got), maxCapturedReceiptSpanBytes)
72 }
73 if !strings.Contains(got, postWriteSpanTruncated) {
74 t.Fatalf("captured fuzzy sample should disclose truncation:\n%s", got)
75 }
76 }
77
77 lines GO