package model

import "testing"

func TestValidateConfig(t *testing.T) {
	c := DefaultConfig()
	c.Repositories = append(c.Repositories, Repository{SchemaVersion: 1, ID: "repo", Name: "Local", Type: RepositoryLocal, Location: "/tmp/repo", PasswordRef: "password"})
	c.Jobs = append(c.Jobs, Job{SchemaVersion: 1, ID: "job", Name: "Share", Type: JobShare, Enabled: true, RepositoryID: "repo", Sources: []Source{{Path: "/mnt/user/data"}}, Schedule: Schedule{Cron: "0 2 * * *"}, Consistency: Consistency{Mode: "live"}, Compression: "auto", Retention: Retention{Daily: 7, Weekly: 4, Monthly: 12}, ShutdownSecs: 120})
	if err := ValidateConfig(c); err != nil {
		t.Fatalf("valid config rejected: %v", err)
	}
	c.Jobs[0].Sources[0].Path = "relative"
	if err := ValidateConfig(c); err == nil {
		t.Fatal("relative source path accepted")
	}
}

func TestRepositoryIsolation(t *testing.T) {
	c := DefaultConfig()
	c.Jobs = []Job{{SchemaVersion: 1, ID: "job", Name: "Broken", Type: JobFlash, RepositoryID: "missing", Consistency: Consistency{Mode: "live"}, Compression: "auto"}}
	if err := ValidateConfig(c); err == nil {
		t.Fatal("unknown repository reference accepted")
	}
}

func TestRepositoryCannotBeSharedByJobs(t *testing.T) {
	c := DefaultConfig()
	c.Repositories = []Repository{{SchemaVersion: 1, ID: "repo", Name: "Local", Type: RepositoryLocal, Location: "/tmp/repo", PasswordRef: "password"}}
	base := Job{SchemaVersion: 1, Name: "Flash", Type: JobFlash, RepositoryID: "repo", Consistency: Consistency{Mode: "live"}, Compression: "auto"}
	first, second := base, base
	first.ID, second.ID = "first", "second"
	c.Jobs = []Job{first, second}
	if err := ValidateConfig(c); err == nil {
		t.Fatal("shared repository accepted")
	}
}
