|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | + |
| 3 | +package skillinject |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +// TestPickWebhookURL_FirstInstalled walks tools in manifest order and |
| 12 | +// returns the first installed tool's URL. Confirms the precedence |
| 13 | +// contract: openclaw beats hermes if both are installed and both |
| 14 | +// declare a URL, because openclaw comes first in the manifest. |
| 15 | +func TestPickWebhookURL_FirstInstalled(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + dir := t.TempDir() |
| 18 | + // Make BOTH ~/.openclaw and ~/.hermes exist |
| 19 | + if err := os.MkdirAll(filepath.Join(dir, ".openclaw"), 0o700); err != nil { |
| 20 | + t.Fatal(err) |
| 21 | + } |
| 22 | + if err := os.MkdirAll(filepath.Join(dir, ".hermes"), 0o700); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + tools := []ManifestTool{ |
| 26 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 27 | + {Name: "hermes", RootDir: "~/.hermes", WebhookURL: "http://127.0.0.1:8644/pilot-events"}, |
| 28 | + } |
| 29 | + url, name := pickWebhookURL(tools, dir) |
| 30 | + if name != "openclaw" { |
| 31 | + t.Fatalf("expected openclaw to win precedence, got %q", name) |
| 32 | + } |
| 33 | + if url != "http://127.0.0.1:18789/pilot-webhook" { |
| 34 | + t.Fatalf("url=%q", url) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// TestPickWebhookURL_SkipsUninstalledTools confirms an asking tool |
| 39 | +// whose rootDir doesn't exist is skipped (no false positive). |
| 40 | +func TestPickWebhookURL_SkipsUninstalledTools(t *testing.T) { |
| 41 | + t.Parallel() |
| 42 | + dir := t.TempDir() |
| 43 | + // Only hermes is installed; openclaw asks but isn't present. |
| 44 | + if err := os.MkdirAll(filepath.Join(dir, ".hermes"), 0o700); err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + tools := []ManifestTool{ |
| 48 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 49 | + {Name: "hermes", RootDir: "~/.hermes", WebhookURL: "http://127.0.0.1:8644/pilot-events"}, |
| 50 | + } |
| 51 | + url, name := pickWebhookURL(tools, dir) |
| 52 | + if name != "hermes" { |
| 53 | + t.Fatalf("expected hermes (only one installed), got %q", name) |
| 54 | + } |
| 55 | + if url != "http://127.0.0.1:8644/pilot-events" { |
| 56 | + t.Fatalf("url=%q", url) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +// TestPickWebhookURL_NoTools returns empty when no tool is installed |
| 61 | +// or no tool declares a URL. |
| 62 | +func TestPickWebhookURL_NoTools(t *testing.T) { |
| 63 | + t.Parallel() |
| 64 | + dir := t.TempDir() |
| 65 | + tools := []ManifestTool{ |
| 66 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 67 | + } |
| 68 | + // Nothing installed in dir → pickWebhookURL returns empty. |
| 69 | + url, name := pickWebhookURL(tools, dir) |
| 70 | + if url != "" || name != "" { |
| 71 | + t.Fatalf("expected empty when no tool installed, got name=%q url=%q", name, url) |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// TestReconcileWebhookURL_WritesFileWhenMissing covers the cold-start |
| 76 | +// case: ~/.pilot/webhook_url doesn't exist; we should create it with |
| 77 | +// the picked URL. |
| 78 | +func TestReconcileWebhookURL_WritesFileWhenMissing(t *testing.T) { |
| 79 | + t.Parallel() |
| 80 | + dir := t.TempDir() |
| 81 | + if err := os.MkdirAll(filepath.Join(dir, ".openclaw"), 0o700); err != nil { |
| 82 | + t.Fatal(err) |
| 83 | + } |
| 84 | + tools := []ManifestTool{ |
| 85 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 86 | + } |
| 87 | + o := reconcileWebhookURL(dir, tools) |
| 88 | + if o.Action != ActionCreate { |
| 89 | + t.Fatalf("expected Create, got %v (err=%q)", o.Action, o.Err) |
| 90 | + } |
| 91 | + got, err := os.ReadFile(filepath.Join(dir, ".pilot", "webhook_url")) |
| 92 | + if err != nil { |
| 93 | + t.Fatalf("read-back: %v", err) |
| 94 | + } |
| 95 | + if string(got) != "http://127.0.0.1:18789/pilot-webhook" { |
| 96 | + t.Fatalf("on-disk url=%q", got) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +// TestReconcileWebhookURL_IdempotentWhenCorrect runs reconcile twice; |
| 101 | +// the second call should be a Noop. |
| 102 | +func TestReconcileWebhookURL_IdempotentWhenCorrect(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + dir := t.TempDir() |
| 105 | + if err := os.MkdirAll(filepath.Join(dir, ".openclaw"), 0o700); err != nil { |
| 106 | + t.Fatal(err) |
| 107 | + } |
| 108 | + tools := []ManifestTool{ |
| 109 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 110 | + } |
| 111 | + _ = reconcileWebhookURL(dir, tools) |
| 112 | + o := reconcileWebhookURL(dir, tools) |
| 113 | + if o.Action != ActionNoop { |
| 114 | + t.Fatalf("second tick should be Noop, got %v", o.Action) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +// TestReconcileWebhookURL_LeavesFileWhenNoToolWants ensures we don't |
| 119 | +// blank out an operator-set URL when no installed tool asks for one |
| 120 | +// — the daemon would lose its webhook config otherwise. |
| 121 | +func TestReconcileWebhookURL_LeavesFileWhenNoToolWants(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + dir := t.TempDir() |
| 124 | + // Pre-populate with an operator URL. |
| 125 | + if err := os.MkdirAll(filepath.Join(dir, ".pilot"), 0o700); err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + manual := filepath.Join(dir, ".pilot", "webhook_url") |
| 129 | + if err := os.WriteFile(manual, []byte("https://my-custom-endpoint.example.com/hook"), 0o600); err != nil { |
| 130 | + t.Fatal(err) |
| 131 | + } |
| 132 | + // No installed tool asks for a URL. |
| 133 | + tools := []ManifestTool{ |
| 134 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 135 | + } |
| 136 | + o := reconcileWebhookURL(dir, tools) |
| 137 | + if o.Action != ActionNoop { |
| 138 | + t.Fatalf("expected Noop when no tool asks, got %v", o.Action) |
| 139 | + } |
| 140 | + got, _ := os.ReadFile(manual) |
| 141 | + if string(got) != "https://my-custom-endpoint.example.com/hook" { |
| 142 | + t.Fatalf("operator-set URL was blanked: %q", got) |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +// TestReconcileWebhookURL_RewritesOnDrift confirms that when the file |
| 147 | +// has an old URL and the picked URL differs, we update the file. |
| 148 | +func TestReconcileWebhookURL_RewritesOnDrift(t *testing.T) { |
| 149 | + t.Parallel() |
| 150 | + dir := t.TempDir() |
| 151 | + if err := os.MkdirAll(filepath.Join(dir, ".openclaw"), 0o700); err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + if err := os.MkdirAll(filepath.Join(dir, ".pilot"), 0o700); err != nil { |
| 155 | + t.Fatal(err) |
| 156 | + } |
| 157 | + path := filepath.Join(dir, ".pilot", "webhook_url") |
| 158 | + if err := os.WriteFile(path, []byte("http://127.0.0.1:18789/old-path"), 0o600); err != nil { |
| 159 | + t.Fatal(err) |
| 160 | + } |
| 161 | + tools := []ManifestTool{ |
| 162 | + {Name: "openclaw", RootDir: "~/.openclaw", WebhookURL: "http://127.0.0.1:18789/pilot-webhook"}, |
| 163 | + } |
| 164 | + o := reconcileWebhookURL(dir, tools) |
| 165 | + if o.Action != ActionRewrite { |
| 166 | + t.Fatalf("expected Rewrite on drift, got %v (err=%q)", o.Action, o.Err) |
| 167 | + } |
| 168 | + got, _ := os.ReadFile(path) |
| 169 | + if string(got) != "http://127.0.0.1:18789/pilot-webhook" { |
| 170 | + t.Fatalf("url not updated: %q", got) |
| 171 | + } |
| 172 | +} |
0 commit comments