| 1 | package limaembed |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | |
| 10 | |
| 11 | func TestEventWatch(t *testing.T) { |
| 12 | var out bytes.Buffer |
| 13 | var told []string |
| 14 | e := newEventWatch(&out, "proj-1a2b3c4d") |
| 15 | e.notify = func(title, msg string) { told = append(told, title+": "+msg) } |
| 16 | in := `{"status":{"running":true}} |
| 17 | {"status":{"portForward":{"type":"forwarding","protocol":"tcp","guestAddr":"127.0.0.1:5173","hostAddr":"127.0.0.1:5173"}}} |
| 18 | {"status":{"portForward":{"type":"failed","protocol":"tcp","guestAddr":"127.0.0.1:5432","hostAddr":"127.0.0.1:5432","error":"in use"}}} |
| 19 | {"status":{"portForward":{"type":"forwarding","hostAddr":"not an address"}}} |
| 20 | ` |
| 21 | |
| 22 | e.Write([]byte(in[:40])) |
| 23 | e.Write([]byte(in[40:])) |
| 24 | if out.String() != in { |
| 25 | t.Errorf("events changed: %q", out.String()) |
| 26 | } |
| 27 | want := "agent-vm: proj-1a2b3c4d: The VM now serves 127.0.0.1:5173 on this machine.\n" + |
| 28 | "agent-vm: proj-1a2b3c4d: Not forwarded: 127.0.0.1:5432 is in use on this machine." |
| 29 | if strings.Join(told, "\n") != want { |
| 30 | t.Errorf("told:\n%s", strings.Join(told, "\n")) |
| 31 | } |
| 32 | } |