]> git.giacomolli.uk - onechat/blob - prompt_test.go
Name and order the view code
[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.NotContains(t, text, promptName)
20
21         // The old agent name is built from parts, so the source does not spell
22         // it.
23         require.NotContains(t, strings.ToLower(text), "u"+"nii")
24 }
25
26 func TestPromptLeavesOutAbsentFeatures(t *testing.T) {
27         text := prompt("Ada", "")
28         for _, absent := range []string{"image", "ubagent", "computer", "device", `zoom("Name")`, "instructions at the end"} {
29                 require.NotContains(t, text, absent)
30         }
31 }
32
33 func TestPromptEndsWithTheInstructions(t *testing.T) {
34         text := prompt("Ada", "\nI am Lena. Keep replies short.\n\n")
35         require.Contains(t, text, `Do the user's tasks yourself, with your tools, following the user's instructions
36 at the end of this prompt: who they are, how their files are organized and how
37 they want work done.`)
38         require.True(t, strings.HasSuffix(text, "\n\n# The user's instructions\n\nI am Lena. Keep replies short."))
39 }
40
41 func TestPromptWithoutInstructionsHasNoClause(t *testing.T) {
42         for _, instructions := range []string{"", " \n\t"} {
43                 text := prompt("Ada", instructions)
44                 require.Contains(t, text, "Do the user's tasks yourself, with your tools.\n")
45                 require.NotContains(t, text, "instructions")
46         }
47 }
48
49 func TestAgentToolsAreStrictAndStable(t *testing.T) {
50         tools := agentTools()
51         names := make([]string, len(tools))
52         for n, tool := range tools {
53                 names[n] = tool.Name
54         }
55
56         require.Equal(t, []string{"zoom", "date", "shell", "read", "write", "edit"}, names)
57
58         for _, tool := range tools {
59                 schema := tool.Parameters.(map[string]any)
60                 properties := schema["properties"].(map[string]any)
61                 require.True(t, tool.Strict, tool.Name)
62                 require.Equal(t, false, schema["additionalProperties"], tool.Name)
63                 require.Equal(t, slices.Sorted(maps.Keys(properties)), schema["required"], tool.Name)
64         }
65
66         first, err := json.Marshal(tools)
67         require.NoError(t, err)
68
69         for range 10 {
70                 again, err := json.Marshal(agentTools())
71                 require.NoError(t, err)
72                 require.Equal(t, first, again)
73         }
74 }