X-Git-Url: http://git.giacomolli.uk/onechat/blobdiff_plain/d302957c09b192d0fbc09287ba7dcb7c32426556..HEAD:/agent_test.go?ds=sidebyside diff --git a/agent_test.go b/agent_test.go index a9418ae..8b6775f 100644 --- a/agent_test.go +++ b/agent_test.go @@ -4,6 +4,7 @@ import ( "encoding/json" "os" "path/filepath" + "strings" "testing" "github.com/stretchr/testify/require" @@ -103,6 +104,7 @@ func TestAgentPromptReadsTheInstructionsEachTime(t *testing.T) { require.Equal(t, prompt(agent.config.Name, filepath.Join(dir, agentInstructionsFileName), ""), text) require.NoError(t, os.WriteFile(filepath.Join(dir, agentInstructionsFileName), []byte("I am Lena.\n"), 0o600)) + text, err = agent.Prompt() require.NoError(t, err) require.Equal(t, prompt(agent.config.Name, filepath.Join(dir, agentInstructionsFileName), "I am Lena."), text) @@ -125,6 +127,66 @@ func TestAgentPromptUsesAnAbsoluteInstructionsPath(t *testing.T) { require.Contains(t, text, "The user's instructions live in the file "+filepath.Join(dir, agentInstructionsFileName)+",") } +func TestChildPromptDefinesItsAreaAndReports(t *testing.T) { + dir := t.TempDir() + agent, err := openAgent(dir) + require.NoError(t, err) + + config := agent.config + config.Name = "Garden" + config.Parent = "OneChat" + config.Responsibility = "Keep the garden plans and decisions." + agentWriteTestConfig(t, dir, config) + require.NoError(t, os.WriteFile(filepath.Join(dir, agentInstructionsFileName), []byte("Ask before editing plans."), 0o600)) + + agent, err = openAgent(dir) + require.NoError(t, err) + + text, err := agent.Prompt() + require.NoError(t, err) + require.Contains(t, text, "You are Garden, a child agent of OneChat.") + require.Contains(t, text, "You are responsible for: Keep the garden plans and decisions.") + require.Contains(t, text, "Work only in this area.") + require.Contains(t, text, "Report to OneChat only what needs the parent or the") + require.Contains(t, text, "End your turn with an empty reply when nothing needs the parent or the user.") + require.Contains(t, text, "Send nothing to WhatsApp. Draft replies in your report instead") + require.Contains(t, text, filepath.Join(dir, agentInstructionsFileName)) + require.True(t, strings.HasSuffix(text, "Ask before editing plans.")) + require.NotContains(t, text, "You act again only when the user writes.") +} + +func TestChildConfigNeedsAParentAndAnArea(t *testing.T) { + testCases := []struct { + name string + parent string + responsibility string + }{ + { + name: "a parent needs an area", + parent: "OneChat", + }, + { + name: "an area needs a parent", + responsibility: "Keep the garden plans.", + }, + } + for _, tc := range testCases { + t.Run(tc.name, func(t *testing.T) { + dir := t.TempDir() + agent, err := openAgent(dir) + require.NoError(t, err) + + config := agent.config + config.Parent = tc.parent + config.Responsibility = tc.responsibility + agentWriteTestConfig(t, dir, config) + + _, err = openAgent(dir) + require.ErrorContains(t, err, "a child needs both responsibility and parent") + }) + } +} + // agentWriteTestConfig writes config as the config.json of the agent in dir. func agentWriteTestConfig(t *testing.T, dir string, config AgentConfig) { t.Helper()