File size: 1,702 Bytes
6a7089a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cli

import (
	"testing"
)

func TestDoGetPrettyPrintsJSON(t *testing.T) {
	m := newMockServer()
	m.response = `{"a":1,"b":2}`
	defer m.close()
	client := m.server.Client()

	// Just verify it doesn't panic with valid JSON
	DoGet(client, m.base(), "", "/health", nil)
}

func TestDoGetNonJSON(t *testing.T) {
	m := newMockServer()
	m.response = "plain text response"
	defer m.close()
	client := m.server.Client()

	// Should handle non-JSON gracefully
	DoGet(client, m.base(), "", "/text", nil)
}

func TestDoPostContentType(t *testing.T) {
	m := newMockServer()
	defer m.close()
	client := m.server.Client()

	_ = DoPost(client, m.base(), "", "/action", map[string]any{"kind": "click"})
	ct := m.lastHeaders.Get("Content-Type")
	if ct != "application/json" {
		t.Errorf("expected application/json, got %q", ct)
	}
}

func TestResolveInstanceBase(t *testing.T) {
	orch := newMockServer()
	orch.response = `{"id":"abc123","port":"9901","status":"running"}`
	defer orch.close()

	got := ResolveInstanceBase(orch.base(), "", "abc123", "127.0.0.1")

	if orch.lastPath != "/instances/abc123" {
		t.Errorf("expected GET /instances/abc123, got %s", orch.lastPath)
	}
	if got != "http://127.0.0.1:9901" {
		t.Errorf("ResolveInstanceBase = %q, want %q", got, "http://127.0.0.1:9901")
	}
}

func TestResolveInstanceBase_ForwardsToken(t *testing.T) {
	orch := newMockServer()
	orch.response = `{"id":"xyz","port":"9902","status":"running"}`
	defer orch.close()

	ResolveInstanceBase(orch.base(), "my-token", "xyz", "localhost")

	authHeader := orch.lastHeaders.Get("Authorization")
	if authHeader != "Bearer my-token" {
		t.Errorf("Authorization header = %q, want %q", authHeader, "Bearer my-token")
	}
}