]> git.giacomolli.uk - onechat/blob - prompt_test.go
Keep the schedules in a file
[onechat] / prompt_test.go
1 package main
2
3 import (
4         "encoding/json"
5         "maps"
6         "slices"
7         "strings"
8         "testing"
9
10         "github.com/stretchr/testify/require"
11 )
12
13 func TestPromptNamesTheAgent(t *testing.T) {
14         text := prompt("Ada", "")
15         require.Contains(t, text, "You are Ada, an AI agent")
16         require.Contains(t, text, "- reply: Ada's replies")
17         require.Contains(t, text, "Goal: let Ada work later")
18         require.Contains(t, text, `- work: an agent's report, starting "[Name]"`)
19         require.Contains(t, text, "- zoom(id, 1) gives message id whole\n- zoom(\"Name\") gives an agent's whole chat\n")
20         require.NotContains(t, text, promptName)
21
22         // The old agent name is built from parts, so the source does not spell
23         // it.
24         require.NotContains(t, strings.ToLower(text), "u"+"nii")
25 }
26
27 func TestPromptLeavesOutAbsentFeatures(t *testing.T) {
28         text := prompt("Ada", "")
29         for _, absent := range []string{"image", "computer", "device", "instructions at the end"} {
30                 require.NotContains(t, text, absent)
31         }
32 }
33
34 func TestPromptHasTheLinesOnSubagents(t *testing.T) {
35         text := prompt("Ada", "")
36         require.Contains(t, text, "Do the user's tasks yourself, with your tools.\nUse subagents only when the user asks for them.\n")
37         require.Contains(t, text, `Messages the user sends while you work reach you between tool calls.
38 Subagents run in the background; each one's report reaches you as a message
39 starting "[Name]", between your tool calls or as a new turn. Never wait for one
40 (no sleep, no polling): go on, or end your turn and tell the user what is
41 running.
42 `)
43 }
44
45 func TestSubagentPromptNamesTheAgentAndEndsWithTheInstructions(t *testing.T) {
46         text := promptSubagent("Ada", "\nI am Lena.\n")
47         require.True(t, strings.HasPrefix(text, "You are a subagent of Ada, an AI agent"))
48         require.Contains(t, text, "Ada gave you a task. Do it yourself, with your tools,\nfollowing the user's instructions at the end of this prompt: they say who the\nuser is, how their files are organized and how they want work done.\n")
49         require.Contains(t, text, "zoom(\"Name\") gives an agent's\nwhole chat.")
50         require.True(t, strings.HasSuffix(text, "\n\n# The user's instructions\n\nI am Lena."))
51         require.NotContains(t, text, promptName)
52
53         for _, absent := range []string{"image", "computer", "device", "Compaction"} {
54                 require.NotContains(t, text, absent)
55         }
56
57         text = promptSubagent("Ada", " \n")
58         require.Contains(t, text, "Ada gave you a task. Do it yourself, with your tools.\n")
59         require.NotContains(t, text, "instructions")
60 }
61
62 func TestPromptEndsWithTheInstructions(t *testing.T) {
63         text := prompt("Ada", "\nI am Lena. Keep replies short.\n\n")
64         require.Contains(t, text, `Do the user's tasks yourself, with your tools, following the user's instructions
65 at the end of this prompt: who they are, how their files are organized and how
66 they want work done.`)
67         require.True(t, strings.HasSuffix(text, "\n\n# The user's instructions\n\nI am Lena. Keep replies short."))
68 }
69
70 func TestPromptWithoutInstructionsHasNoClause(t *testing.T) {
71         for _, instructions := range []string{"", " \n\t"} {
72                 text := prompt("Ada", instructions)
73                 require.Contains(t, text, "Do the user's tasks yourself, with your tools.\n")
74                 require.NotContains(t, text, "instructions")
75         }
76 }
77
78 func TestAgentToolsAreStrictAndStable(t *testing.T) {
79         tools := promptTools()
80         require.Equal(t, []string{"zoom", "date", "shell", "read", "write", "edit", "spawn", "tell", "whatsapp_find", "whatsapp_send"}, promptToolNames(tools))
81         require.Equal(t, []string{"zoom", "date", "shell", "read", "write", "edit"}, promptToolNames(promptSubagentTools()))
82
83         for _, tool := range tools {
84                 schema := tool.Parameters.(map[string]any)
85                 properties := schema["properties"].(map[string]any)
86                 require.True(t, tool.Strict, tool.Name)
87                 require.Equal(t, false, schema["additionalProperties"], tool.Name)
88                 require.Equal(t, slices.Sorted(maps.Keys(properties)), schema["required"], tool.Name)
89         }
90
91         first, err := json.Marshal(tools)
92         require.NoError(t, err)
93
94         for range 10 {
95                 again, err := json.Marshal(promptTools())
96                 require.NoError(t, err)
97                 require.Equal(t, first, again)
98         }
99 }
100
101 // promptToolNames returns the names of tools.
102 func promptToolNames(tools []ModelTool) []string {
103         names := make([]string, len(tools))
104         for n, tool := range tools {
105                 names[n] = tool.Name
106         }
107
108         return names
109 }