|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "io/fs" |
| 7 | + "log" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// writeWebCommandTestFile 写入 web 命令测试所需的最小文件内容,避免各测试重复拼装目录。 |
| 15 | +func writeWebCommandTestFile(t *testing.T, path, content string) { |
| 16 | + t.Helper() |
| 17 | + if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil { |
| 18 | + t.Fatalf("mkdir %s: %v", path, err) |
| 19 | + } |
| 20 | + if err := os.WriteFile(path, []byte(content), 0o644); err != nil { |
| 21 | + t.Fatalf("write %s: %v", path, err) |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +// chdirForWebCommandTest 切换当前工作目录,并在测试结束后恢复。 |
| 26 | +func chdirForWebCommandTest(t *testing.T, dir string) { |
| 27 | + t.Helper() |
| 28 | + original, err := os.Getwd() |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("getwd: %v", err) |
| 31 | + } |
| 32 | + if err := os.Chdir(dir); err != nil { |
| 33 | + t.Fatalf("chdir %s: %v", dir, err) |
| 34 | + } |
| 35 | + t.Cleanup(func() { |
| 36 | + if err := os.Chdir(original); err != nil { |
| 37 | + t.Fatalf("restore cwd: %v", err) |
| 38 | + } |
| 39 | + }) |
| 40 | +} |
| 41 | + |
| 42 | +// stubResolveExecutablePath 替换可执行文件路径解析,便于覆盖发布包布局分支。 |
| 43 | +func stubResolveExecutablePath(t *testing.T, fn func() (string, error)) { |
| 44 | + t.Helper() |
| 45 | + original := resolveExecutablePath |
| 46 | + resolveExecutablePath = fn |
| 47 | + t.Cleanup(func() { |
| 48 | + resolveExecutablePath = original |
| 49 | + }) |
| 50 | +} |
| 51 | + |
| 52 | +// stubWebCommandHooks 替换 web 命令测试中的可注入执行点,并在结束后恢复。 |
| 53 | +func stubWebCommandHooks( |
| 54 | + t *testing.T, |
| 55 | + startGateway func(context.Context, gatewayCommandOptions, string, fs.FS, func(string)) error, |
| 56 | + build func(string, *log.Logger) error, |
| 57 | + lookPath func(string) (string, error), |
| 58 | +) { |
| 59 | + t.Helper() |
| 60 | + originalStart := webCommandStartGatewayServer |
| 61 | + originalBuild := webCommandBuildFrontend |
| 62 | + originalLookPath := webCommandLookPath |
| 63 | + if startGateway != nil { |
| 64 | + webCommandStartGatewayServer = startGateway |
| 65 | + } |
| 66 | + if build != nil { |
| 67 | + webCommandBuildFrontend = build |
| 68 | + } |
| 69 | + if lookPath != nil { |
| 70 | + webCommandLookPath = lookPath |
| 71 | + } |
| 72 | + t.Cleanup(func() { |
| 73 | + webCommandStartGatewayServer = originalStart |
| 74 | + webCommandBuildFrontend = originalBuild |
| 75 | + webCommandLookPath = originalLookPath |
| 76 | + }) |
| 77 | +} |
| 78 | + |
| 79 | +func TestFindWebSourceDirUsesCurrentWorkdir(t *testing.T) { |
| 80 | + tempDir := t.TempDir() |
| 81 | + chdirForWebCommandTest(t, tempDir) |
| 82 | + stubResolveExecutablePath(t, func() (string, error) { |
| 83 | + return "", errors.New("skip executable lookup") |
| 84 | + }) |
| 85 | + |
| 86 | + writeWebCommandTestFile(t, filepath.Join(tempDir, "web", "package.json"), "{}") |
| 87 | + |
| 88 | + got := findWebSourceDir() |
| 89 | + want := filepath.Join(tempDir, "web") |
| 90 | + if got != want { |
| 91 | + t.Fatalf("findWebSourceDir() = %q, want %q", got, want) |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +func TestFindWebSourceDirFallsBackToExecutableDir(t *testing.T) { |
| 96 | + tempDir := t.TempDir() |
| 97 | + chdirForWebCommandTest(t, tempDir) |
| 98 | + |
| 99 | + releaseDir := filepath.Join(tempDir, "release") |
| 100 | + writeWebCommandTestFile(t, filepath.Join(releaseDir, "web", "package.json"), "{}") |
| 101 | + stubResolveExecutablePath(t, func() (string, error) { |
| 102 | + return filepath.Join(releaseDir, "neocode.exe"), nil |
| 103 | + }) |
| 104 | + |
| 105 | + got := findWebSourceDir() |
| 106 | + want := filepath.Join(releaseDir, "web") |
| 107 | + if got != want { |
| 108 | + t.Fatalf("findWebSourceDir() = %q, want %q", got, want) |
| 109 | + } |
| 110 | +} |
| 111 | + |
| 112 | +func TestResolveWebStaticDirFallsBackToExecutableDir(t *testing.T) { |
| 113 | + tempDir := t.TempDir() |
| 114 | + chdirForWebCommandTest(t, tempDir) |
| 115 | + |
| 116 | + releaseDir := filepath.Join(tempDir, "release") |
| 117 | + writeWebCommandTestFile(t, filepath.Join(releaseDir, "web", "dist", "index.html"), "<html></html>") |
| 118 | + stubResolveExecutablePath(t, func() (string, error) { |
| 119 | + return filepath.Join(releaseDir, "neocode.exe"), nil |
| 120 | + }) |
| 121 | + |
| 122 | + got, err := resolveWebStaticDir("") |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("resolveWebStaticDir returned error: %v", err) |
| 125 | + } |
| 126 | + want := filepath.Join(releaseDir, "web", "dist") |
| 127 | + if got != want { |
| 128 | + t.Fatalf("resolveWebStaticDir() = %q, want %q", got, want) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +func TestFindNPMBinaryMissingMessage(t *testing.T) { |
| 133 | + stubWebCommandHooks(t, nil, nil, func(string) (string, error) { |
| 134 | + return "", errors.New("not found") |
| 135 | + }) |
| 136 | + |
| 137 | + _, err := findNPMBinary() |
| 138 | + if err == nil { |
| 139 | + t.Fatal("findNPMBinary() error = nil, want error") |
| 140 | + } |
| 141 | + message := err.Error() |
| 142 | + if !strings.Contains(message, "Node.js and npm") { |
| 143 | + t.Fatalf("findNPMBinary() error = %q, want Node.js/npm guidance", message) |
| 144 | + } |
| 145 | + if !strings.Contains(message, "`neocode web`") { |
| 146 | + t.Fatalf("findNPMBinary() error = %q, want neocode web guidance", message) |
| 147 | + } |
| 148 | +} |
| 149 | + |
| 150 | +func TestRunWebCommandBuildsFrontendWhenDistMissing(t *testing.T) { |
| 151 | + tempDir := t.TempDir() |
| 152 | + chdirForWebCommandTest(t, tempDir) |
| 153 | + writeWebCommandTestFile(t, filepath.Join(tempDir, "web", "package.json"), "{}") |
| 154 | + |
| 155 | + buildCalled := false |
| 156 | + var capturedStaticDir string |
| 157 | + sentinelErr := errors.New("stop after start") |
| 158 | + stubWebCommandHooks( |
| 159 | + t, |
| 160 | + func(_ context.Context, _ gatewayCommandOptions, staticDir string, _ fs.FS, _ func(string)) error { |
| 161 | + capturedStaticDir = staticDir |
| 162 | + return sentinelErr |
| 163 | + }, |
| 164 | + func(webDir string, _ *log.Logger) error { |
| 165 | + buildCalled = true |
| 166 | + writeWebCommandTestFile(t, filepath.Join(webDir, "dist", "index.html"), "<html></html>") |
| 167 | + return nil |
| 168 | + }, |
| 169 | + nil, |
| 170 | + ) |
| 171 | + |
| 172 | + err := runWebCommand(context.Background(), webCommandOptions{ |
| 173 | + HTTPAddress: "127.0.0.1:8080", |
| 174 | + LogLevel: "info", |
| 175 | + OpenBrowser: false, |
| 176 | + Workdir: tempDir, |
| 177 | + }) |
| 178 | + if !errors.Is(err, sentinelErr) { |
| 179 | + t.Fatalf("runWebCommand() error = %v, want sentinel error %v", err, sentinelErr) |
| 180 | + } |
| 181 | + if !buildCalled { |
| 182 | + t.Fatal("runWebCommand() did not invoke frontend build when dist was missing") |
| 183 | + } |
| 184 | + wantStaticDir := filepath.Join(tempDir, "web", "dist") |
| 185 | + if capturedStaticDir != wantStaticDir { |
| 186 | + t.Fatalf("startGatewayServer staticDir = %q, want %q", capturedStaticDir, wantStaticDir) |
| 187 | + } |
| 188 | +} |
0 commit comments