Files
ARTEX/db/customtool_test.go
T
BaaRai 53fc8478da test(db): close the pool after the row cleanups, report cleanup errors
A `defer d.Close()` runs when the test function returns, before its
t.Cleanup functions. The cleanups that delete the test's rows then ran on
a closed pool, the error was ignored, and the rows stayed in the database
for the next run. PR #220 fixed three files; this applies the same pattern
to every remaining db test that combines `defer d.Close()` with row
cleanups.

- Close the pool with t.Cleanup, registered before the row cleanups so it
  runs last (LIFO).
- Report cleanup errors with t.Errorf through small helpers in
  db/cleanup_helpers_test.go. A profile the test already deleted is not an
  error.

Test files only. No assertion or production change.
2026-11-14 00:38:00 +01:00

84 lines
2.2 KiB
Go

package db
import (
"encoding/json"
"testing"
)
// TestCustomToolCRUD exercises create/list/update/delete of a user-defined tool
// (system=false) with kind/exec/deferred. Skips when no Postgres is configured.
func TestCustomToolCRUD(t *testing.T) {
d, err := Open(testDSN(t))
if err != nil {
t.Skipf("postgres unavailable (%v) — skipping", err)
}
// Registered FIRST so it runs LAST, after the row cleanup below.
t.Cleanup(func() { d.Close() })
key := "ct_test_tool"
_ = d.DeleteCustomTool(key) // clean slate
in := &Tool{
Key: key,
Description: "test",
Schema: json.RawMessage(`{"type":"object","properties":{"x":{"type":"string"}}}`),
Agents: []string{"worker"},
Enabled: true,
Kind: "script",
Exec: json.RawMessage(`{"code":"print(1)"}`),
Deferred: true,
}
if err := d.CreateCustomTool(in); err != nil {
t.Fatalf("create: %v", err)
}
t.Cleanup(func() { cleanupErr(t, "custom tool", d.DeleteCustomTool(key)) })
got, err := d.GetTool(key)
if err != nil || got == nil {
t.Fatalf("get after create: %v (nil=%v)", err, got == nil)
}
if got.System || got.Kind != "script" || !got.Deferred || len(got.Agents) != 1 {
t.Fatalf("unexpected row: %+v", got)
}
// custom tools appear in ListCustomTools, not among system-only.
customs, err := d.ListCustomTools()
if err != nil {
t.Fatal(err)
}
found := false
for _, c := range customs {
if c.Key == key {
found = true
if c.System {
t.Fatal("custom tool should have system=false")
}
}
}
if !found {
t.Fatal("created tool not in ListCustomTools")
}
// update: change kind + disable + rebind.
in.Kind = "command"
in.Exec = json.RawMessage(`{"command":"echo hi"}`)
in.Enabled = false
in.Agents = []string{"worker", "planner"}
in.Deferred = false
if err := d.UpdateCustomTool(in); err != nil {
t.Fatalf("update: %v", err)
}
got2, _ := d.GetTool(key)
if got2.Kind != "command" || got2.Enabled || got2.Deferred || len(got2.Agents) != 2 {
t.Fatalf("update not applied: %+v", got2)
}
// delete only removes non-system rows.
if err := d.DeleteCustomTool(key); err != nil {
t.Fatalf("delete: %v", err)
}
if g, _ := d.GetTool(key); g != nil {
t.Fatal("tool still present after delete")
}
}