]> git.giacomolli.uk - onechat/blob - config_test.go
Record the subagent name in the usage records
[onechat] / config_test.go
1 package main
2
3 import (
4 "os"
5 "path/filepath"
6 "testing"
7
8 "github.com/stretchr/testify/require"
9 )
10
11 func TestLoadConfigGivesDefaultsWithoutAFile(t *testing.T) {
12 dir := t.TempDir()
13 config, err := loadConfig(dir)
14 require.NoError(t, err)
15 require.Equal(t, defaultConfig(), config)
16 require.NoFileExists(t, filepath.Join(dir, "config.json"))
17 }
18
19 func TestLoadConfigKeepsDefaultsForMissingFields(t *testing.T) {
20 dir := t.TempDir()
21 configWriteTest(t, dir, `{"endpoint": "api", "compactor": {"effort": "xhigh"}}`)
22
23 config, err := loadConfig(dir)
24 require.NoError(t, err)
25 require.Equal(t, Config{
26 Endpoint: configEndpointAPI,
27 Compactor: ModelConfig{
28 Model: "gpt-6.1-sol",
29 Effort: "xhigh",
30 },
31 }, config)
32 }
33
34 func TestLoadConfigRejectsInvalidFiles(t *testing.T) {
35 testCases := []struct {
36 name string
37 text string
38 }{
39 {
40 name: "unknown field",
41 text: `{"endpont": "api"}`,
42 },
43 {
44 name: "unknown nested field",
45 text: `{"compactor": {"modle": "x"}}`,
46 },
47 {
48 name: "endpoint URL",
49 text: `{"endpoint": "https://example.com/v1/responses"}`,
50 },
51 {
52 name: "empty model",
53 text: `{"compactor": {"model": ""}}`,
54 },
55 {
56 name: "second object",
57 text: `{} {}`,
58 },
59 {
60 name: "broken JSON",
61 text: `{"endpoint": `,
62 },
63 }
64
65 for _, tc := range testCases {
66 t.Run(tc.name, func(t *testing.T) {
67 dir := t.TempDir()
68 configWriteTest(t, dir, tc.text)
69
70 _, err := loadConfig(dir)
71 require.Error(t, err)
72 })
73 }
74 }
75
76 func configWriteTest(t *testing.T, dir, text string) {
77 t.Helper()
78
79 err := os.WriteFile(filepath.Join(dir, "config.json"), []byte(text), 0o600)
80 require.NoError(t, err)
81 }