-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtool_use_writer_parity_test.go
More file actions
91 lines (81 loc) · 3.02 KB
/
Copy pathtool_use_writer_parity_test.go
File metadata and controls
91 lines (81 loc) · 3.02 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2023-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package conversation
import (
"encoding/json"
"testing"
"github.com/mattermost/mattermost-plugin-agents/v2/llm"
"github.com/stretchr/testify/require"
)
// persistedToolUseFields are the tool_use ContentBlock JSON tags the writers
// in this package must emit so a tool call renders identically live and after
// reload. Keep in sync with the persisted fields in
// streaming/tool_call_parity_test.go, which covers the live-path writer and
// redaction parity but cannot reach the unexported toolUseBlocks.
var persistedToolUseFields = []string{
"id",
"name",
"server_origin",
"input",
"mcp_bare_name",
"status",
"title",
"description",
"user_interaction",
}
func parityToolCall() llm.ToolCall {
return llm.ToolCall{
ID: "tc-1",
Name: "mattermost__create_post",
Description: "Create a post",
Title: "Create Post",
Arguments: json.RawMessage(`{"channel_id":"c1"}`),
Status: llm.ToolCallStatusSuccess,
MCPBareName: "create_post",
UserInteraction: llm.UserInteractionSelect,
ServerOrigin: "embedded://mattermost",
}
}
func toolUseBlockJSONMap(t *testing.T, block ContentBlock) map[string]any {
t.Helper()
data, err := json.Marshal(block)
require.NoError(t, err)
var m map[string]any
require.NoError(t, json.Unmarshal(data, &m))
return m
}
// TestToolUseBlocksPersistsPolicyFields asserts the auto-run writer
// (toolUseBlocks) emits every persisted tool_use identity/metadata field for a
// fully-populated call, so an auto-run round renders the same as a live one.
func TestToolUseBlocksPersistsPolicyFields(t *testing.T) {
blocks := toolUseBlocks("", llm.ReasoningData{}, []llm.ToolCall{parityToolCall()}, true)
require.Len(t, blocks, 1)
require.Equal(t, BlockTypeToolUse, blocks[0].Type)
m := toolUseBlockJSONMap(t, blocks[0])
for _, field := range persistedToolUseFields {
t.Run(field, func(t *testing.T) {
require.Contains(t, m, field, "toolUseBlocks dropped the field")
require.NotEmpty(t, m[field], "toolUseBlocks emitted an empty field")
})
}
}
// TestPostToBlocksPersistsPolicyFields asserts the generic Post->blocks
// converter carries the same tool identity/metadata. It intentionally omits
// user_interaction: PostToBlocks converts completed posts, where the pending
// interaction kind is not meaningful (it is set by the approval-path writers).
func TestPostToBlocksPersistsPolicyFields(t *testing.T) {
post := llm.Post{Role: llm.PostRoleBot, ToolUse: []llm.ToolCall{parityToolCall()}}
blocks := PostToBlocks(post, true)
require.GreaterOrEqual(t, len(blocks), 1)
require.Equal(t, BlockTypeToolUse, blocks[0].Type)
m := toolUseBlockJSONMap(t, blocks[0])
for _, field := range persistedToolUseFields {
if field == "user_interaction" {
continue
}
t.Run(field, func(t *testing.T) {
require.Contains(t, m, field, "PostToBlocks dropped the field")
require.NotEmpty(t, m[field], "PostToBlocks emitted an empty field")
})
}
}