Add installer, launch-to-browser, and switching from the web GUI

Three features plus the design-system swap.

Installer. Inno-compile.iss builds a per-user installer into %APPDATA%\ipswap
with PrivilegesRequired=lowest, so it never shows a UAC prompt. That matches
the app's asInvoker manifest, and it is also what lets the in-app updater
replace the .exe later without elevation — a Program Files install could not.
It offers a desktop shortcut and an optional start-with-Windows entry, and
reuses the app's own single-instance mutex as AppMutex so setup notices a
running copy, since a running .exe cannot be overwritten. Uninstall leaves
presets.json, config.json and the log in place.

Launching. Opening ipswap opens the editor in a browser, starting the tray
first if it is not already up; if it is, the running copy opens the browser and
the second process exits. internal/instance does this with a loopback control
listener whose port and token live in a mode-600 session.json, plus a
session-local named mutex on Windows to settle a launch race. A stale session
file from a crash is detected by a failed call and treated as "no primary", so
it can never wedge startup. The Run-key entry now passes --background, because
a browser tab at every login is not wanted.

Switching from the browser. New endpoints for active state, apply preview and
apply. The browser confirms against the same before/after text the tray's
MessageBox shows, built from a live read at prompt time. Apply requires the
session token in a header like every other mutation — it changes the machine's
network, so it is not a weaker case than editing a preset. A netsh refusal for
lack of elevation comes back as 409 with a flag, so the page can point at
"Relaunch as administrator" instead of showing a generic failure.

To avoid two copies of that sequence, internal/switcher now owns everything
between "the user said yes" and "the adapter changed", and both front ends call
it. It serialises applies: two interleaved netsh sequences on one adapter would
leave it matching neither preset, and there are now two ways to start one.

CSS. apointless.css is vendored from bsncubed/css and left untouched; the
previous file was the boarding-pass stylesheet and its class names did not
match this markup. ipswap.css adds only what the design system does not ship —
page chrome, tables, modals, stat tiles, a success alert — on its tokens, so
re-pulling apointless.css restyles the app.

Not verified: the installer is uncompiled (no Inno Setup or wine on this box)
and the UI is not visually rendered (headless Firefox hangs here). Both are
checked as far as the tooling allows — assets and endpoints serve, and every
DOM id the JS touches exists in the markup.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
2026-08-20 14:32:15 +10:00
parent a8bbf91223
commit f267c430d9
21 changed files with 2330 additions and 565 deletions
+169
View File
@@ -0,0 +1,169 @@
package instance
import (
"encoding/json"
"errors"
"os"
"path/filepath"
"sync/atomic"
"testing"
"time"
"gitea.apointless.space/bsncubed/ipswap/internal/config"
)
func testPaths(t *testing.T) config.Paths {
t.Helper()
dir := t.TempDir()
return config.Paths{Dir: dir, Presets: filepath.Join(dir, "presets.json")}
}
func TestSignalWithNoPrimary(t *testing.T) {
paths := testPaths(t)
if err := Signal(paths); !errors.Is(err, ErrNoPrimary) {
t.Errorf("Signal with no session file = %v, want ErrNoPrimary", err)
}
if IsRunning(paths) {
t.Error("IsRunning should be false with no session file")
}
}
func TestAcquireThenSignalOpensEditor(t *testing.T) {
paths := testPaths(t)
var opened atomic.Int32
p, err := Acquire(paths, func() { opened.Add(1) })
if err != nil {
t.Fatal(err)
}
defer p.Release()
if !IsRunning(paths) {
t.Fatal("IsRunning should be true once a primary is up")
}
if err := Signal(paths); err != nil {
t.Fatalf("Signal returned %v", err)
}
// The handler answers before running the callback, so give it a moment.
deadline := time.Now().Add(2 * time.Second)
for opened.Load() == 0 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
if opened.Load() != 1 {
t.Errorf("expected the open callback to fire once, got %d", opened.Load())
}
}
func TestSessionFileIsPrivateAndWellFormed(t *testing.T) {
paths := testPaths(t)
p, err := Acquire(paths, nil)
if err != nil {
t.Fatal(err)
}
defer p.Release()
path := filepath.Join(paths.Dir, "session.json")
info, err := os.Stat(path)
if err != nil {
t.Fatal(err)
}
// The token in here authorises control calls, so it must not be readable
// by other users on a shared machine.
if perm := info.Mode().Perm(); perm != 0o600 {
t.Errorf("session file mode = %o, want 600", perm)
}
b, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
var s session
if err := json.Unmarshal(b, &s); err != nil {
t.Fatal(err)
}
if s.Port == 0 || s.Token == "" || s.PID != os.Getpid() {
t.Errorf("session file looks wrong: %+v", s)
}
}
func TestReleaseRemovesSessionFile(t *testing.T) {
paths := testPaths(t)
p, err := Acquire(paths, nil)
if err != nil {
t.Fatal(err)
}
p.Release()
if _, err := os.Stat(filepath.Join(paths.Dir, "session.json")); !os.IsNotExist(err) {
t.Error("Release should remove the session file")
}
if IsRunning(paths) {
t.Error("IsRunning should be false after Release")
}
}
// A crash leaves a session file pointing at a dead port. Startup must treat
// that as "no primary" rather than hanging or refusing to start.
func TestStaleSessionFileIsIgnored(t *testing.T) {
paths := testPaths(t)
stale, _ := json.Marshal(session{Port: 1, Token: "dead", PID: 999999})
if err := os.WriteFile(filepath.Join(paths.Dir, "session.json"), stale, 0o600); err != nil {
t.Fatal(err)
}
start := time.Now()
if IsRunning(paths) {
t.Error("a stale session file must not look like a running instance")
}
if err := Signal(paths); !errors.Is(err, ErrNoPrimary) {
t.Errorf("Signal against a stale file = %v, want ErrNoPrimary", err)
}
// Startup waits on this, so it must fail fast rather than block.
if elapsed := time.Since(start); elapsed > 5*time.Second {
t.Errorf("stale session detection took %s, too slow for startup", elapsed)
}
}
func TestGarbageSessionFileIsIgnored(t *testing.T) {
paths := testPaths(t)
if err := os.WriteFile(filepath.Join(paths.Dir, "session.json"), []byte("{not json"), 0o600); err != nil {
t.Fatal(err)
}
if IsRunning(paths) {
t.Error("a corrupt session file must not look like a running instance")
}
}
// The control channel must not answer without the token.
func TestControlRequiresToken(t *testing.T) {
paths := testPaths(t)
var opened atomic.Int32
p, err := Acquire(paths, func() { opened.Add(1) })
if err != nil {
t.Fatal(err)
}
defer p.Release()
s, err := readSession(filepath.Join(paths.Dir, "session.json"))
if err != nil {
t.Fatal(err)
}
wrong := session{Port: s.Port, Token: "not-the-token"}
if err := call(wrong, "/open"); !errors.Is(err, ErrNoPrimary) {
t.Errorf("call with a bad token = %v, want it rejected", err)
}
time.Sleep(100 * time.Millisecond)
if opened.Load() != 0 {
t.Error("an unauthorised control call must not open the editor")
}
}