Add RFC-backed coverage tests
Some checks are pending
Security Scan / security (push) Waiting to run
Test / test (push) Waiting to run

This commit is contained in:
Snider 2026-04-15 19:42:20 +01:00
parent 9d3ce2df2a
commit 85c0d294e2
3 changed files with 133 additions and 1 deletions

View file

@ -2,9 +2,12 @@ package container
import (
"errors"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDetectWithEnvironment_PrefersAppleContainersOnMacOS26(t *testing.T) {
@ -84,3 +87,37 @@ func TestMajorVersion(t *testing.T) {
assert.Equal(t, 0, majorVersion("bogus"))
assert.Equal(t, 0, majorVersion(""))
}
func TestDetect_Good(t *testing.T) {
binDir := t.TempDir()
writeExecutable(t, binDir, "sw_vers", "#!/bin/sh\nprintf '26.0\\n'\n")
writeExecutable(t, binDir, "container", "#!/bin/sh\nexit 0\n")
t.Setenv("PATH", binDir)
assert.Equal(t, RuntimeApple, Detect())
}
func TestDetect_Bad(t *testing.T) {
binDir := t.TempDir()
writeExecutable(t, binDir, "sw_vers", "#!/bin/sh\nprintf '25.0\\n'\n")
writeExecutable(t, binDir, "docker", "#!/bin/sh\nexit 0\n")
t.Setenv("PATH", binDir)
assert.Equal(t, RuntimeDocker, Detect())
}
func TestDetect_Ugly(t *testing.T) {
binDir := t.TempDir()
writeExecutable(t, binDir, "sw_vers", "#!/bin/sh\nprintf 'not-a-version\\n'\n")
t.Setenv("PATH", binDir)
assert.Equal(t, RuntimeNone, Detect())
}
func writeExecutable(t *testing.T, dir, name, script string) string {
t.Helper()
path := filepath.Join(dir, name)
require.NoError(t, os.WriteFile(path, []byte(script), 0o755))
return path
}

View file

@ -1,6 +1,9 @@
package container
import "testing"
import (
"os"
"testing"
)
func TestDetectModeWithEnvironment(t *testing.T) {
mode := DetectModeWithEnvironment(ModeEnvironment{
@ -22,3 +25,39 @@ func TestDetectModeWithEnvironment(t *testing.T) {
t.Fatalf("expected manager mode, got %q", mode)
}
}
func TestMode_DetectMode_Good(t *testing.T) {
oldArgs := os.Args
os.Args = []string{"core-gui", "--mode=worker"}
t.Cleanup(func() { os.Args = oldArgs })
t.Setenv("CORE_GUI_MODE", "manager")
mode := DetectMode()
if mode != ModeWorker {
t.Fatalf("expected worker mode, got %q", mode)
}
}
func TestMode_DetectMode_Bad(t *testing.T) {
oldArgs := os.Args
os.Args = []string{"core-gui"}
t.Cleanup(func() { os.Args = oldArgs })
t.Setenv("CORE_GUI_MODE", "bogus")
mode := DetectMode()
if mode != ModeManager {
t.Fatalf("expected manager mode, got %q", mode)
}
}
func TestMode_DetectMode_Ugly(t *testing.T) {
oldArgs := os.Args
os.Args = []string{"core-gui", "--unexpected=flag"}
t.Cleanup(func() { os.Args = oldArgs })
t.Setenv("CORE_GUI_MODE", " \tbogus\n")
mode := DetectMode()
if mode != ModeManager {
t.Fatalf("expected manager mode after malformed input, got %q", mode)
}
}

View file

@ -2,6 +2,7 @@ package display
import (
"context"
"os"
"path/filepath"
"strings"
"testing"
@ -114,3 +115,58 @@ func TestPreload_TrustedPreloadOrigin_Bad(t *testing.T) {
assert.False(t, trustedPreloadOrigin("https://example.com"))
assert.False(t, trustedPreloadOrigin("http://10.0.0.1:3000"))
}
type preloadCapture struct {
scripts []string
}
func (p *preloadCapture) ExecJS(script string) {
p.scripts = append(p.scripts, script)
}
func TestPreload_InjectPreload_Good(t *testing.T) {
root := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(root, ".core"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(root, "index.html"), []byte("<html></html>"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(root, "preload.js"), []byte("globalThis.__manifestLoaded = true;"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(root, ".core", "view.yaml"), []byte("preloads:\n - path: preload.js\n"), 0o644))
svc, err := New()
require.NoError(t, err)
target := &preloadCapture{}
err = svc.InjectPreload(target, "file://"+filepath.ToSlash(filepath.Join(root, "index.html")))
require.NoError(t, err)
require.Len(t, target.scripts, 1)
assert.Contains(t, target.scripts[0], "globalThis.core.ml")
assert.Contains(t, target.scripts[0], "globalThis.electron")
assert.Contains(t, target.scripts[0], "__manifestLoaded")
}
func TestPreload_InjectPreload_Bad(t *testing.T) {
svc, err := New()
require.NoError(t, err)
target := &preloadCapture{}
err = svc.InjectPreload(target, "https://example.com/app")
require.NoError(t, err)
require.Len(t, target.scripts, 1)
assert.Contains(t, target.scripts[0], "globalThis.core.ml")
assert.NotContains(t, target.scripts[0], "globalThis.electron")
assert.NotContains(t, target.scripts[0], "core.background.serviceWorker.register")
}
func TestPreload_InjectPreload_Ugly(t *testing.T) {
root := t.TempDir()
require.NoError(t, os.MkdirAll(filepath.Join(root, ".core"), 0o755))
require.NoError(t, os.WriteFile(filepath.Join(root, "index.html"), []byte("<html></html>"), 0o644))
require.NoError(t, os.WriteFile(filepath.Join(root, ".core", "view.yaml"), []byte("preloads: [\n"), 0o644))
svc, err := New()
require.NoError(t, err)
target := &preloadCapture{}
err = svc.InjectPreload(target, "file://"+filepath.ToSlash(filepath.Join(root, "index.html")))
require.Error(t, err)
assert.Empty(t, target.scripts)
}