package profile import ( "context" "errors" "io/fs" "reflect" "testing" "testing/fstest" ) func TestLoadFSRepositoryLoadsSortedIndependentProfiles(t *testing.T) { fsys := fstest.MapFS{ "profiles/z.yml": {Data: []byte("id: z\nbackend: local\nmodel: z-model\nextra_params:\n nested:\n value: one\n")}, "profiles/a.yml": {Data: []byte("id: a\nbackend: local\nmodel: a-model\nendpoint: ''\n")}, } repository, metadata, err := LoadFSRepository(context.Background(), fsys, "profiles") if err != nil { t.Fatalf("load repository: %v", err) } if len(metadata) != 2 || metadata[0].ID != "a" || metadata[1].ID != "z" || metadata[0].Path != "a.yml" { t.Fatalf("unexpected metadata: %#v", metadata) } if len(metadata[0].ExplicitFields) != 4 || metadata[0].ExplicitFields[0] != "backend" || metadata[0].ExplicitFields[1] != "endpoint" { t.Fatalf("expected explicitly empty endpoint metadata, got %#v", metadata[0].ExplicitFields) } metadata[1].ExplicitFields[0] = "changed" first, err := repository.GetProfile(context.Background(), "z") if err != nil { t.Fatalf("load profile: %v", err) } first.ExtraParams["nested"].(map[string]any)["value"] = "changed" second, err := repository.GetProfile(context.Background(), "z") if err != nil { t.Fatalf("reload profile: %v", err) } if second.ExtraParams["nested"].(map[string]any)["value"] != "one" { t.Fatalf("profile value was not defensively copied: %#v", second) } } func TestLoadFSRepositoryRejectsInvalidProfiles(t *testing.T) { tests := map[string]fstest.MapFS{ "duplicate IDs": { "profiles/one.yml": {Data: []byte("id: duplicate\nbackend: local\nmodel: one\n")}, "profiles/two.yml": {Data: []byte("id: duplicate\nbackend: local\nmodel: two\n")}, }, "raw API key": { "profiles/one.yml": {Data: []byte("id: one\nbackend: local\nmodel: one\napi_key: forbidden\n")}, }, "multiple documents": { "profiles/one.yml": {Data: []byte("id: one\nbackend: local\nmodel: one\n---\nid: two\n")}, }, } for name, fsys := range tests { t.Run(name, func(t *testing.T) { _, _, err := LoadFSRepository(context.Background(), fsys, "profiles") if err == nil { t.Fatal("expected load failure") } }) } } func TestLoadedRepositoryHonorsCancellationAndMissingProfiles(t *testing.T) { repository, _, err := LoadFSRepository(context.Background(), fstest.MapFS{ "profiles/one.yml": {Data: []byte("id: one\nbackend: local\nmodel: one\n")}, }, "profiles") if err != nil { t.Fatalf("load repository: %v", err) } ctx, cancel := context.WithCancel(context.Background()) cancel() if _, err := repository.GetProfile(ctx, "one"); !errors.Is(err, context.Canceled) { t.Fatalf("expected cancellation, got %v", err) } if _, err := repository.GetProfile(context.Background(), "missing"); !errors.Is(err, ErrProfileNotFound) { t.Fatalf("expected missing profile, got %v", err) } } func TestLoadFSRepositoryMatchesPointLookupForRawProfiles(t *testing.T) { fsys := fstest.MapFS{ "profiles/base.yml": {Data: []byte("id: base\nbackend: local\nmodel: base-model\n")}, "profiles/derived.yml": {Data: []byte("id: derived\nbase_profile: base\nreasoning_effort: high\n")}, } eager, _, err := LoadFSRepository(context.Background(), fsys, "profiles") if err != nil { t.Fatalf("load eager repository: %v", err) } pointLookup := NewFSRepository(fsys, "profiles") for _, id := range []string{"base", "derived"} { t.Run(id, func(t *testing.T) { got, err := eager.GetProfile(context.Background(), id) if err != nil { t.Fatalf("load eager profile: %v", err) } want, err := pointLookup.GetProfile(context.Background(), id) if err != nil { t.Fatalf("load point-in-time profile: %v", err) } if !reflect.DeepEqual(got, want) { t.Fatalf("raw profiles differ: got %#v, want %#v", got, want) } }) } } var _ fs.FS = fstest.MapFS{}