package ontology import ( "context" "encoding/json" "testing" "github.com/missionworkforce/missionmatch/internal/platform/audit" "github.com/missionworkforce/missionmatch/internal/platform/storage" ) type fakeSource struct{ recs []VocabRecord } func (f *fakeSource) Name() string { return "Fake" } func (f *fakeSource) Fetch(_ context.Context, yield func(VocabRecord) error) error { for _, r := range f.recs { if err := yield(r); err != nil { return err } } return nil } type fakeWriter struct{ got []VocabRecord } func (w *fakeWriter) Upsert(_ context.Context, rec VocabRecord, key, sha string) error { if key == "" || sha == "" { return &simpleErr{"expected non empty raw key and sha"} } w.got = append(w.got, rec) return nil } type fakeAuditor struct{ events int } func (a *fakeAuditor) System(_ context.Context, _ audit.Event) error { a.events++; return nil } type simpleErr struct{ s string } func (e *simpleErr) Error() string { return e.s } func TestIngesterRetainsUpsertsAudits(t *testing.T) { src := &fakeSource{recs: []VocabRecord{ {SourceName: "Fake", SourceRecordID: "R375", Kind: KindPSC, Term: "Maintenance of Aircraft", Code: "R375", Raw: json.RawMessage(`{"code":"R375"}`)}, {SourceRecordID: "AC12", Kind: KindPSC, Term: "National Defense R and D", Code: "AC12"}, }} w := &fakeWriter{} a := &fakeAuditor{} ing := NewIngester(storage.NewMemory(), w, a) res, err := ing.Ingest(context.Background(), src) if err != nil { t.Fatalf("ingest: %v", err) } if res.Fetched != 2 || res.Upserted != 2 { t.Fatalf("counts: %+v", res) } if len(w.got) != 2 { t.Fatalf("writer got %d", len(w.got)) } if w.got[1].SourceName != "Fake" { t.Fatal("empty SourceName should default to the source Name") } if a.events != 1 { t.Fatalf("expected one audit event, got %d", a.events) } } func TestIngesterRejectsInvalidKind(t *testing.T) { src := &fakeSource{recs: []VocabRecord{{SourceRecordID: "X", Kind: "bogus"}}} ing := NewIngester(storage.NewMemory(), &fakeWriter{}, &fakeAuditor{}) if _, err := ing.Ingest(context.Background(), src); err == nil { t.Fatal("expected an error on an invalid kind") } }