-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathmain_test.go
More file actions
167 lines (153 loc) · 5.46 KB
/
Copy pathmain_test.go
File metadata and controls
167 lines (153 loc) · 5.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"encoding/base64"
"errors"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/terasum/medict/pkg/model"
)
func TestParseCSSWindowArgs(t *testing.T) {
dictID, dictName, seedPath, ok := parseCSSWindowArgs([]string{cssEditorWindowFlag, "online-bing", "Bing Dictionary", "/tmp/base.css"})
if !ok || dictID != "online-bing" || dictName != "Bing Dictionary" || seedPath != "/tmp/base.css" {
t.Fatalf("unexpected parse result: id=%q name=%q seed=%q ok=%v", dictID, dictName, seedPath, ok)
}
if _, _, _, ok := parseCSSWindowArgs([]string{cssEditorWindowFlag, "../escape", "bad", ""}); ok {
t.Fatal("unsafe dictionary id must be rejected")
}
}
func TestReplaceProcessEnv(t *testing.T) {
got := replaceProcessEnv([]string{"PATH=/bin", "devserver=localhost:34115"}, "devserver", "localhost:0")
if len(got) != 2 || got[0] != "PATH=/bin" || got[1] != "devserver=localhost:0" {
t.Fatalf("unexpected environment: %#v", got)
}
}
func TestNewCSSWindowCommand(t *testing.T) {
if runtime.GOOS != "darwin" {
t.Skip("macOS LaunchServices behavior")
}
cmd := newCSSWindowCommand("/Applications/Medict.app/Contents/MacOS/Medict", "dict-1", "Test Dict", "/tmp/base.css")
if len(cmd.Args) < 9 || cmd.Args[0] != "/usr/bin/open" || cmd.Args[3] != "/Applications/Medict.app" || cmd.Args[8] != "/tmp/base.css" {
t.Fatalf("packaged macOS editor must use LaunchServices: %#v", cmd.Args)
}
}
func TestCSSWindowAppDoesNotInitialiseProductServices(t *testing.T) {
app := newCSSWindowApp("abc123", "Test Dictionary", "/tmp/base.css")
if app.WindowMode() != "css-editor" || app.dictSvc != nil || app.bs != nil {
t.Fatalf("CSS editor must stay lightweight: %#v", app)
}
context := app.CSSWindowDictionary()
if context["id"] != "abc123" || context["name"] != "Test Dictionary" {
t.Fatalf("unexpected editor context: %#v", context)
}
}
func TestCollectDictionaryCSSUsesLooseFilesInStableOrder(t *testing.T) {
dir := t.TempDir()
if err := os.WriteFile(filepath.Join(dir, "b.css"), []byte(".b{}"), 0o644); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "a.css"), []byte(".a{}"), 0o644); err != nil {
t.Fatal(err)
}
css, err := collectDictionaryCSS(dir, []string{"Dictionary.css"}, func(string) ([]byte, error) {
return nil, model.ErrNotFound
})
if err != nil {
t.Fatal(err)
}
if css != "/* a.css */\n.a{}\n\n/* b.css */\n.b{}" {
t.Fatalf("unexpected CSS composition: %q", css)
}
}
func TestCollectDictionaryCSSFallsBackToEmbeddedResource(t *testing.T) {
css, err := collectDictionaryCSS(t.TempDir(), []string{"Fancy.css"}, func(name string) ([]byte, error) {
if name == `\Fancy.css` {
return []byte(".embedded{}"), nil
}
return nil, model.ErrNotFound
})
if err != nil {
t.Fatal(err)
}
if css != "/* Fancy.css */\n.embedded{}" {
t.Fatalf("unexpected embedded CSS: %q", css)
}
}
func TestCollectDictionaryCSSPropagatesNotReadyError(t *testing.T) {
notReady := errors.New("dictionary not ready")
_, err := collectDictionaryCSS(t.TempDir(), []string{"book.css"}, func(string) ([]byte, error) {
return nil, notReady
})
if !errors.Is(err, notReady) {
t.Fatalf("expected readiness error, got %v", err)
}
}
func TestCollectDictionaryCSSIncludesNestedLooseAndEmbeddedSources(t *testing.T) {
dir := t.TempDir()
if err := os.MkdirAll(filepath.Join(dir, "css"), 0o755); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(filepath.Join(dir, "css", "style.css"), []byte(".nested{}"), 0o644); err != nil {
t.Fatal(err)
}
css, err := collectDictionaryCSS(dir, []string{"book.css"}, func(name string) ([]byte, error) {
if name == "book.css" {
return []byte(".embedded{}"), nil
}
return nil, model.ErrNotFound
})
if err != nil {
t.Fatal(err)
}
want := "/* css/style.css */\n.nested{}\n\n/* book.css */\n.embedded{}"
if css != want {
t.Fatalf("mixed CSS: got %q want %q", css, want)
}
}
func TestExtractInlinedCSSSupportsArbitraryMultipleStylesheets(t *testing.T) {
first := base64.StdEncoding.EncodeToString([]byte(".style{}"))
second := base64.StdEncoding.EncodeToString([]byte(".oald{}"))
snapshot := `<link href="data:text/css;base64,` + first + `"><link href="data:text/css;base64,` + second + `">`
if got := extractInlinedCSS(snapshot); got != ".style{}\n\n.oald{}" {
t.Fatalf("unexpected extracted CSS: %q", got)
}
}
func TestReadEditorCSSPrefersOverrideThenSeed(t *testing.T) {
dir := t.TempDir()
override := filepath.Join(dir, "override.css")
seed := filepath.Join(dir, "seed.css")
if err := os.WriteFile(seed, []byte("seed"), 0o600); err != nil {
t.Fatal(err)
}
css, err := readEditorCSS(override, seed)
if err != nil || css != "seed" {
t.Fatalf("seed fallback: css=%q err=%v", css, err)
}
if err := os.WriteFile(override, []byte("override"), 0o600); err != nil {
t.Fatal(err)
}
css, err = readEditorCSS(override, seed)
if err != nil || css != "override" {
t.Fatalf("override priority: css=%q err=%v", css, err)
}
}
func TestWriteFileAtomicReplacesContent(t *testing.T) {
path := filepath.Join(t.TempDir(), "custom.css")
if err := os.WriteFile(path, []byte("old"), 0o644); err != nil {
t.Fatal(err)
}
if err := writeFileAtomic(path, []byte("new content"), 0o644); err != nil {
t.Fatal(err)
}
data, err := os.ReadFile(path)
if err != nil || string(data) != "new content" {
t.Fatalf("atomic replacement failed: data=%q err=%v", data, err)
}
}
func TestPlatformReportsHostGOOS(t *testing.T) {
got := (&App{}).Platform()
if got != runtime.GOOS {
t.Fatalf("Platform() = %q, want runtime.GOOS %q", got, runtime.GOOS)
}
}