zot/packages/core/intercept_test.go
patriceckhart fa7d8d8be5 refactor: split source into packages/{provider,core,tui,agent}
Single Go module, four top-level packages under packages/. Import
paths become github.com/patriceckhart/zot/packages/<name>; downstream
consumers can depend on individual packages without pulling the rest.

Layout:
  packages/provider/     LLM clients + catalog
  packages/provider/auth/ credential store + OAuth + login server
  packages/core/         agent loop, sessions, cost
  packages/tui/          terminal toolkit + chat view
  packages/agent/        CLI wiring, system prompt
    extensions/ extproto/ modes/ tools/ skills/ swarm/
    sdk/  (was pkg/zotcore, package renamed zotcore -> sdk)
    ext/  (was pkg/zotext, package renamed zotext -> ext)

internal/ and pkg/ removed. The internal/assets logo moved into
packages/provider/auth/assets.

Public Go SDK identifiers renamed:
  pkg/zotcore (package zotcore) -> packages/agent/sdk (package sdk)
  pkg/zotext  (package zotext)  -> packages/agent/ext (package ext)

This breaks Go-based extensions and embedders; the JSON wire protocol
for extensions and RPC is unchanged, so non-Go extensions, already-
built extension binaries, and zot rpc consumers are unaffected.

Docs, examples, and the built-in write-zot-extension skill updated
for the new paths and identifiers. Shadow-bug fixes in code samples
(ext := ext.New -> e := ext.New).
2026-05-27 09:07:15 +02:00

109 lines
3.3 KiB
Go

package core
import (
"context"
"encoding/json"
"testing"
"github.com/patriceckhart/zot/packages/provider"
)
// recordingTool captures the args it was invoked with so the test
// can verify the interceptor-rewritten args reached execution.
type recordingTool struct {
lastArgs json.RawMessage
}
func (r *recordingTool) Name() string { return "echo" }
func (r *recordingTool) Description() string { return "echoes" }
func (r *recordingTool) Schema() json.RawMessage { return json.RawMessage(`{"type":"object"}`) }
func (r *recordingTool) Execute(_ context.Context, args json.RawMessage, _ func(string)) (ToolResult, error) {
r.lastArgs = append(json.RawMessage(nil), args...)
return ToolResult{
Content: []provider.Content{provider.TextBlock{Text: "ok"}},
}, nil
}
// TestBeforeToolExecuteModifiesArgs verifies that a non-nil
// modifiedArgs returned from BeforeToolExecute is what the tool
// actually sees.
func TestBeforeToolExecuteModifiesArgs(t *testing.T) {
rec := &recordingTool{}
reg := Registry{"echo": rec}
a := NewAgent(nil, "test", "", reg)
newArgs := json.RawMessage(`{"command":"echo GUARDED: ls"}`)
a.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
return true, "", newArgs
}
ctx := context.Background()
res := a.runOneTool(ctx, provider.ToolCallBlock{
ID: "T1",
Name: "echo",
Arguments: json.RawMessage(`{"command":"ls"}`),
}, func(AgentEvent) {})
if res.IsError {
t.Fatalf("unexpected error result: %v", res.Content)
}
if string(rec.lastArgs) != string(newArgs) {
t.Errorf("tool saw %s, want %s", string(rec.lastArgs), string(newArgs))
}
}
// TestBeforeToolExecuteInvalidJSONIgnored verifies that returning
// malformed JSON as modifiedArgs leaves the original args intact
// (safety: a buggy interceptor can't corrupt the call).
func TestBeforeToolExecuteInvalidJSONIgnored(t *testing.T) {
rec := &recordingTool{}
reg := Registry{"echo": rec}
a := NewAgent(nil, "test", "", reg)
a.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
return true, "", json.RawMessage(`{not json`)
}
ctx := context.Background()
orig := json.RawMessage(`{"command":"ls"}`)
a.runOneTool(ctx, provider.ToolCallBlock{
ID: "T1",
Name: "echo",
Arguments: orig,
}, func(AgentEvent) {})
if string(rec.lastArgs) != string(orig) {
t.Errorf("tool saw %s, want original %s", string(rec.lastArgs), string(orig))
}
}
// TestBeforeToolExecuteBlockSurfacesReason verifies a refusal from
// the interceptor returns an error ToolResult with the reason text.
func TestBeforeToolExecuteBlockSurfacesReason(t *testing.T) {
rec := &recordingTool{}
reg := Registry{"echo": rec}
a := NewAgent(nil, "test", "", reg)
a.BeforeToolExecute = func(call provider.ToolCallBlock) (bool, string, json.RawMessage) {
return false, "nope", nil
}
ctx := context.Background()
res := a.runOneTool(ctx, provider.ToolCallBlock{
ID: "T1",
Name: "echo",
Arguments: json.RawMessage(`{"command":"ls"}`),
}, func(AgentEvent) {})
if !res.IsError {
t.Fatal("want error result, got success")
}
if len(res.Content) == 0 {
t.Fatal("no content")
}
tb, ok := res.Content[0].(provider.TextBlock)
if !ok || tb.Text != "nope" {
t.Errorf("want reason 'nope', got %v", res.Content[0])
}
if rec.lastArgs != nil {
t.Error("tool ran despite block")
}
}