package store

import (
	"path/filepath"
	"testing"

	"aegis/internal/filter"
)

func TestStoreRoundTrip(t *testing.T) {
	path := filepath.Join(t.TempDir(), "state.json")
	s, err := Open(path)
	if err != nil {
		t.Fatal(err)
	}
	if err := s.UpsertArticle(Article{Key: "comp.test:1", Group: "comp.test", Number: 1, Raw: "Subject: test\r\n\r\nbody"}); err != nil {
		t.Fatal(err)
	}
	if err := s.SaveDraft(Draft{ID: "draft-1", Body: "hello"}); err != nil {
		t.Fatal(err)
	}
	s.SetFilters([]filter.Rule{{ID: "r", Enabled: true, Field: filter.FieldSubject, Operator: filter.OperatorContains, Pattern: "x", Action: filter.ActionHide}})
	if err := s.Save(); err != nil {
		t.Fatal(err)
	}
	reloaded, err := Open(path)
	if err != nil {
		t.Fatal(err)
	}
	snapshot := reloaded.Snapshot()
	if len(snapshot.Articles) != 1 || len(snapshot.Drafts) != 1 || len(snapshot.Filters) != 1 {
		t.Fatalf("unexpected snapshot: %#v", snapshot)
	}
}
