|
| 1 | +package services_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/highcard-dev/daemon/internal/core/services" |
| 9 | +) |
| 10 | + |
| 11 | +const minimalScrollYaml = `name: test-scroll |
| 12 | +version: 0.0.1 |
| 13 | +commands: {} |
| 14 | +` |
| 15 | + |
| 16 | +func writeFile(t *testing.T, path, content string) { |
| 17 | + t.Helper() |
| 18 | + if err := os.WriteFile(path, []byte(content), 0644); err != nil { |
| 19 | + t.Fatalf("failed to write %s: %v", path, err) |
| 20 | + } |
| 21 | +} |
| 22 | + |
| 23 | +func TestRenderCwdTemplates_BootstrapsScrollConfig(t *testing.T) { |
| 24 | + dir := t.TempDir() |
| 25 | + writeFile(t, filepath.Join(dir, "scroll.yaml"), minimalScrollYaml) |
| 26 | + writeFile(t, filepath.Join(dir, "scroll-config.yml.scroll_template"), "key: generated-value\n") |
| 27 | + |
| 28 | + svc, err := services.NewScrollService(dir) |
| 29 | + if err != nil { |
| 30 | + t.Fatalf("NewScrollService: %v", err) |
| 31 | + } |
| 32 | + |
| 33 | + if err := svc.RenderCwdTemplates(); err != nil { |
| 34 | + t.Fatalf("RenderCwdTemplates: %v", err) |
| 35 | + } |
| 36 | + |
| 37 | + content, err := os.ReadFile(filepath.Join(dir, "scroll-config.yml")) |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("scroll-config.yml should exist after bootstrap: %v", err) |
| 40 | + } |
| 41 | + if string(content) != "key: generated-value\n" { |
| 42 | + t.Errorf("unexpected config content: %q", string(content)) |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func TestRenderCwdTemplates_DoesNotOverwriteExistingConfig(t *testing.T) { |
| 47 | + dir := t.TempDir() |
| 48 | + writeFile(t, filepath.Join(dir, "scroll.yaml"), minimalScrollYaml) |
| 49 | + writeFile(t, filepath.Join(dir, "scroll-config.yml"), "key: user-edited\n") |
| 50 | + writeFile(t, filepath.Join(dir, "scroll-config.yml.scroll_template"), "key: {{ randAlphaNum 50 }}\n") |
| 51 | + |
| 52 | + svc, err := services.NewScrollService(dir) |
| 53 | + if err != nil { |
| 54 | + t.Fatalf("NewScrollService: %v", err) |
| 55 | + } |
| 56 | + |
| 57 | + if err := svc.RenderCwdTemplates(); err != nil { |
| 58 | + t.Fatalf("RenderCwdTemplates: %v", err) |
| 59 | + } |
| 60 | + |
| 61 | + content, err := os.ReadFile(filepath.Join(dir, "scroll-config.yml")) |
| 62 | + if err != nil { |
| 63 | + t.Fatalf("reading config: %v", err) |
| 64 | + } |
| 65 | + if string(content) != "key: user-edited\n" { |
| 66 | + t.Errorf("existing config was overwritten: %q", string(content)) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +func TestRenderCwdTemplates_OtherTemplatesReceiveBootstrappedConfig(t *testing.T) { |
| 71 | + dir := t.TempDir() |
| 72 | + writeFile(t, filepath.Join(dir, "scroll.yaml"), minimalScrollYaml) |
| 73 | + writeFile(t, filepath.Join(dir, "scroll-config.yml.scroll_template"), "greeting: hello\n") |
| 74 | + writeFile(t, filepath.Join(dir, "app.conf.scroll_template"), "value: {{ .Config.greeting }}\n") |
| 75 | + |
| 76 | + svc, err := services.NewScrollService(dir) |
| 77 | + if err != nil { |
| 78 | + t.Fatalf("NewScrollService: %v", err) |
| 79 | + } |
| 80 | + |
| 81 | + if err := svc.RenderCwdTemplates(); err != nil { |
| 82 | + t.Fatalf("RenderCwdTemplates: %v", err) |
| 83 | + } |
| 84 | + |
| 85 | + content, err := os.ReadFile(filepath.Join(dir, "app.conf")) |
| 86 | + if err != nil { |
| 87 | + t.Fatalf("app.conf should exist: %v", err) |
| 88 | + } |
| 89 | + if string(content) != "value: hello\n" { |
| 90 | + t.Errorf("other template did not see bootstrapped config: %q", string(content)) |
| 91 | + } |
| 92 | +} |
0 commit comments