Files
Reims-Go/internal/cli/window_args_test.go
T
NwflowerandClaude Opus 5.5 2f1e0bb0e4 feat(cli): fold the command line into reims-go.exe
Only reims-go.exe ships now; reims-go-cli.exe is gone. Without
arguments it opens the window, with a command it runs command-line mode
(cmd/reims-go moved to internal/cli). Missing standard handles are
attached to the parent console; handles the caller passes in (pipes,
files) are kept, so scripts and agents get output and exit codes.

`vm start` hands the VM to the window process so it gets Guest Tools
and the Win10 cold-reboot watcher, which a command that exits could not
run:
- a running window gets the request through Wails' single-instance
  protocol and stays hidden;
- otherwise the command launches the window tray-only (--background),
  detached from the caller's console and handles;
- the window writes the outcome, with the VM's last log lines on
  failure, to <DataRoot>\tmp\cli-start-*.json (it accepts no other
  path) and the command reports it.
With --data-root the VM still starts in the command itself.

The window now takes a single-instance lock: a second double-click
brings the running window forward. `wails dev` builds use their own
lock name.

Tested on Win10 19045 with macos-tahoe: start from the command line
with no window running (4 s, exit 0) and with one running (3 s, exit 0,
same process); the cold-reboot monitor was held by the window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-28 22:52:07 -07:00

76 lines
2.3 KiB
Go

package cli
import (
"context"
"errors"
"os"
"path/filepath"
"reflect"
"testing"
"time"
)
func TestParseWindowArgs(t *testing.T) {
cases := []struct {
args []string
want WindowArgs
isPlain bool // window launch, not a command
}{
{nil, WindowArgs{}, true},
{[]string{"--background"}, WindowArgs{Background: true}, true},
{[]string{"--background", "--start-vm=m0", `--start-result=D:\tmp\cli-start-1.json`},
WindowArgs{Background: true, StartVM: "m0", StartResult: `D:\tmp\cli-start-1.json`}, true},
{[]string{"vm", "start", "m0"}, WindowArgs{}, false},
{[]string{"--background", "vm", "list"}, WindowArgs{}, false},
{[]string{"--help"}, WindowArgs{}, false},
{[]string{"--start-vm", "m0"}, WindowArgs{}, false}, // value must be joined with =
}
for _, c := range cases {
got, ok := ParseWindowArgs(c.args)
if ok != c.isPlain || !reflect.DeepEqual(got, c.want) {
t.Errorf("ParseWindowArgs(%q) = %+v, %v; want %+v, %v", c.args, got, ok, c.want, c.isPlain)
}
if IsCommand(c.args) == c.isPlain {
t.Errorf("IsCommand(%q) = %v", c.args, !c.isPlain)
}
}
}
func TestWindowArgsRoundTrip(t *testing.T) {
w := WindowArgs{Background: true, StartVM: "mac os", StartResult: `D:\Data\tmp\cli-start-9.json`}
got, ok := ParseWindowArgs(w.Args())
if !ok || got != w {
t.Fatalf("round trip = %+v, %v", got, ok)
}
}
func TestStartResultRoundTrip(t *testing.T) {
dir := t.TempDir()
okPath := filepath.Join(dir, "cli-start-1.json")
if err := WriteStartResult(okPath, nil); err != nil {
t.Fatal(err)
}
res, err := waitStartResult(context.Background(), okPath, time.Second)
if err != nil || !res.OK {
t.Fatalf("ok result = %+v, %v", res, err)
}
if _, err := os.Stat(okPath); !os.IsNotExist(err) {
t.Fatal("result file should be removed once read")
}
failPath := filepath.Join(dir, "cli-start-2.json")
go func() {
time.Sleep(300 * time.Millisecond)
_ = WriteStartResult(failPath, errors.New("not enough memory"))
}()
res, err = waitStartResult(context.Background(), failPath, 5*time.Second)
if err != nil || res.OK || res.Error != "not enough memory" {
t.Fatalf("failed result = %+v, %v", res, err)
}
_, err = waitStartResult(context.Background(), filepath.Join(dir, "never.json"), 300*time.Millisecond)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("missing result err = %v", err)
}
}