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-driver=test.driver.linux", `--start-result=D:\tmp\cli-start-1.json`}, WindowArgs{Background: true, StartVM: "m0", StartDriver: "test.driver.linux", 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 = {[]string{"--close-vm=mac os"}, WindowArgs{CloseVM: "mac os"}, true}, {[]string{"--close-vm="}, WindowArgs{}, false}, {[]string{"--wait-pid=4242"}, WindowArgs{WaitPID: 4242}, true}, {[]string{"--background", "--wait-pid=7"}, WindowArgs{Background: true, WaitPID: 7}, true}, {[]string{"--wait-pid=0"}, WindowArgs{}, false}, {[]string{"--wait-pid=abc"}, WindowArgs{}, false}, {[]string{"--background", "--resume-vm=m0", "--start-result=result.json"}, WindowArgs{Background: true, ResumeVM: "m0", StartResult: "result.json"}, true}, {[]string{"--resume-vm="}, WindowArgs{}, false}, {[]string{"--resume-vm=m0", "--start-vm=m0"}, WindowArgs{ResumeVM: "m0", StartVM: "m0"}, false}, } 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 TestCloseWindowArgsRoundTrip(t *testing.T) { want := WindowArgs{CloseVM: "macOS Test"} if got, ok := ParseWindowArgs(want.Args()); !ok || got != want { t.Fatalf("close request round trip = %+v, %v", got, ok) } } func TestWindowArgsRoundTrip(t *testing.T) { w := WindowArgs{Background: true, StartVM: "mac os", StartDriver: "test.driver.linux", StartResult: `D:\Data\tmp\cli-start-9.json`, WaitPID: 31337} got, ok := ParseWindowArgs(w.Args()) if !ok || got != w { t.Fatalf("round trip = %+v, %v", got, ok) } } func TestResumeWindowArgsRoundTrip(t *testing.T) { w := WindowArgs{Background: true, ResumeVM: "mac os", StartResult: "cli-start-42.json"} if got, ok := ParseWindowArgs(w.Args()); !ok || got != w { t.Fatalf("恢复请求往返解析 = %+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) } }