返回 DeepSeek-Reasonix
file_preview_encoding_test.go
根目录 / desktop / file_preview_encoding_test.go
1 package main
2
3 import (
4 "os"
5 "strings"
6 "testing"
7
8 "golang.org/x/text/encoding/simplifiedchinese"
9 )
10
11 // A GB18030 file larger than the preview is cut at 2 MiB, which here lands inside
12 // a two-byte character; the preview must still read as GB18030 text.
13 func TestReadFileGB18030TruncatedMidCharacter(t *testing.T) {
14 orig, _ := os.Getwd()
15 defer os.Chdir(orig)
16 dir := t.TempDir()
17 if err := os.Chdir(dir); err != nil {
18 t.Fatal(err)
19 }
20 line, _ := simplifiedchinese.GB18030.NewEncoder().String(strings.Repeat("中", 50) + "\n")
21 if filePreviewLimit%len(line)%2 == 0 {
22 t.Fatal("fixture does not cut a character at the preview limit")
23 }
24 if err := os.WriteFile("big.gbk", []byte(strings.Repeat(line, filePreviewLimit/len(line)+10)), 0o644); err != nil {
25 t.Fatal(err)
26 }
27 p := (&App{}).ReadFile("big.gbk")
28 if p.Binary || !p.Truncated {
29 t.Fatalf("Binary = %v, Truncated = %v; want a truncated text preview", p.Binary, p.Truncated)
30 }
31 if !strings.HasPrefix(p.Body, strings.Repeat("中", 50)+"\n") || strings.ContainsRune(p.Body, '�') {
32 t.Fatalf("preview did not decode as GB18030: %q...", p.Body[:min(len(p.Body), 60)])
33 }
34 }
35
36 // With no newline anywhere in the preview, the cut character is dropped by
37 // itself rather than read as proof the text is not GB18030.
38 func TestReadFileGB18030TruncatedWithoutNewline(t *testing.T) {
39 orig, _ := os.Getwd()
40 defer os.Chdir(orig)
41 dir := t.TempDir()
42 if err := os.Chdir(dir); err != nil {
43 t.Fatal(err)
44 }
45 gb, _ := simplifiedchinese.GB18030.NewEncoder().String("x" + strings.Repeat("中", filePreviewLimit/2+10))
46 if err := os.WriteFile("line.gbk", []byte(gb), 0o644); err != nil {
47 t.Fatal(err)
48 }
49 p := (&App{}).ReadFile("line.gbk")
50 if p.Binary || !p.Truncated {
51 t.Fatalf("Binary = %v, Truncated = %v; want a truncated text preview", p.Binary, p.Truncated)
52 }
53 if !strings.HasPrefix(p.Body, "x中中") || strings.ContainsRune(p.Body, '�') {
54 t.Fatalf("preview did not decode as GB18030: %q...", p.Body[:min(len(p.Body), 60)])
55 }
56 if p.NextOffset != filePreviewLimit-1 {
57 t.Fatalf("NextOffset = %d, want %d", p.NextOffset, filePreviewLimit-1)
58 }
59 }
60
60 lines GO