55 lines
1.3 KiB
Go
55 lines
1.3 KiB
Go
package web
|
|
|
|
import (
|
|
"runtime"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestPlatformCommandContainsURL(t *testing.T) {
|
|
const u = "https://gitlink.org.cn/x"
|
|
name, args := platformCommand(u)
|
|
if name == "" {
|
|
t.Fatal("empty command name")
|
|
}
|
|
joined := strings.Join(args, " ")
|
|
if !strings.Contains(joined, u) {
|
|
t.Errorf("args %v do not contain URL %q", args, u)
|
|
}
|
|
}
|
|
|
|
func TestPlatformCommandMatchesRuntime(t *testing.T) {
|
|
// Lock the expected dispatcher per GOOS so a future refactor that breaks
|
|
// the switch is caught immediately.
|
|
name, _ := platformCommand("u")
|
|
switch runtime.GOOS {
|
|
case "darwin":
|
|
if name != "open" {
|
|
t.Errorf("darwin: name=%q want open", name)
|
|
}
|
|
case "windows":
|
|
if name != "cmd" {
|
|
t.Errorf("windows: name=%q want cmd", name)
|
|
}
|
|
default:
|
|
if name != "xdg-open" {
|
|
t.Errorf("unix: name=%q want xdg-open", name)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOpenBrowserDoesNotBlock(t *testing.T) {
|
|
// OpenBrowser must return promptly regardless of whether a browser is
|
|
// actually available on the host (CI runners usually have none).
|
|
done := make(chan error, 1)
|
|
go func() { done <- OpenBrowser("about:blank") }()
|
|
select {
|
|
case err := <-done:
|
|
// Both nil and non-nil are acceptable; we only require non-blocking.
|
|
_ = err
|
|
case <-time.After(3 * time.Second):
|
|
t.Fatal("OpenBrowser blocked for >3s")
|
|
}
|
|
}
|