package config

import (
	"os"
	"strings"
	"testing"
)

func TestInitializeCreatesLocalState(t *testing.T) {
	paths := PathsForHome(t.TempDir())

	if err := Initialize(paths); err != nil {
		t.Fatalf("Initialize returned error: %v", err)
	}

	for _, dir := range []string{paths.Home, paths.Logs, paths.Workspaces, paths.Evals, paths.ArtifactBlobs} {
		if info, err := os.Stat(dir); err != nil || !info.IsDir() {
			t.Fatalf("%s was not created as directory, info=%v err=%v", dir, info, err)
		} else if info.Mode().Perm() != 0o700 {
			t.Fatalf("%s mode = %o, want 700", dir, info.Mode().Perm())
		}
	}

	config, err := os.ReadFile(paths.ConfigFile)
	if err != nil {
		t.Fatalf("read config: %v", err)
	}
	if !strings.Contains(string(config), "database") {
		t.Fatalf("config missing database path:\n%s", string(config))
	}
	if !strings.Contains(string(config), "artifact_blobs") {
		t.Fatalf("config missing artifact blob path:\n%s", string(config))
	}
	if !strings.Contains(string(config), "[parallel_sessions]") {
		t.Fatalf("config missing parallel session policy:\n%s", string(config))
	}
}
