feat: add preferred debug port planning

This commit is contained in:
QiuSW
2026-07-27 09:29:10 +08:00
parent 3f382a431d
commit 79630e0d48
10 changed files with 518 additions and 74 deletions
+44
View File
@@ -42,6 +42,50 @@ func TestStoreAddsDefaultRemoteDebugPortForExistingConfig(t *testing.T) {
}
}
func TestStoreAssignsPreferredPortsForLegacyInstancesInStableOrder(t *testing.T) {
path := filepath.Join(t.TempDir(), "config.json")
contents := `{"version":1,"settings":{"remoteDebugStartPort":9777},"instances":[{"id":"first","launch":{"Kind":"chrome","UserDataDir":"C:\\profiles\\first"}},{"id":"second","launch":{"Kind":"edge","UserDataDir":"C:\\profiles\\second"}}]}`
if err := os.WriteFile(path, []byte(contents), 0o600); err != nil {
t.Fatal(err)
}
store, err := New(path)
if err != nil {
t.Fatal(err)
}
got, err := store.Load()
if err != nil {
t.Fatal(err)
}
if got.Instances[0].PreferredRemoteDebugPort != 9777 || got.Instances[1].PreferredRemoteDebugPort != 9778 {
t.Fatalf("legacy preferred ports = %#v", got.Instances)
}
if err := store.Save(got); err != nil {
t.Fatal(err)
}
reloaded, err := store.Load()
if err != nil || reloaded.Instances[0].PreferredRemoteDebugPort != 9777 || reloaded.Instances[1].PreferredRemoteDebugPort != 9778 {
t.Fatalf("persisted preferred ports = %#v, error = %v", reloaded.Instances, err)
}
}
func TestStoreRejectsInvalidOrDuplicatePreferredPorts(t *testing.T) {
store, err := New(filepath.Join(t.TempDir(), "config.json"))
if err != nil {
t.Fatal(err)
}
duplicate := File{Instances: []Instance{
{ID: "one", PreferredRemoteDebugPort: 9666},
{ID: "two", PreferredRemoteDebugPort: 9666},
}}
if err := store.Save(duplicate); err == nil {
t.Fatal("duplicate preferred port was accepted")
}
invalid := File{Instances: []Instance{{ID: "one", PreferredRemoteDebugPort: domain.MinRemoteDebugPort - 1}}}
if err := store.Save(invalid); err == nil {
t.Fatal("invalid preferred port was accepted")
}
}
func TestStoreMissingFileReturnsDefaults(t *testing.T) {
store, err := New(filepath.Join(t.TempDir(), "config.json"))
if err != nil {