package httpapi import ( "context" "encoding/json" "fmt" "net/http" "testing" "git.5iot.com/iotlcm-public/iotlcm-worker-svr/internal/fullsync/contract" ) type liveFunc func(context.Context, string) (contract.RunMeta, error) func (f liveFunc) GetRunMeta(ctx context.Context, id string) (contract.RunMeta, error) { return f(ctx, id) } type archiveFunc func(context.Context, string) (contract.RunArchive, error) func (f archiveFunc) GetArchivedRun(ctx context.Context, id string) (contract.RunArchive, error) { return f(ctx, id) } type roundsFunc func(context.Context, string) ([]contract.RoundStats, error) func (f roundsFunc) GetRoundStats(ctx context.Context, id string) ([]contract.RoundStats, error) { return f(ctx, id) } func TestRunLookupLiveFirstAndArchiveFallback(t *testing.T) { for _, test := range []struct { name string liveErr error roundsErr error archiveErr error wantStatus int wantSource contract.RunSource wantArchives int wantRounds int }{ {name: "live", wantStatus: 200, wantSource: contract.SourceLive, wantRounds: 1}, {name: "archive", liveErr: fmt.Errorf("expired: %w", contract.ErrNotFound), wantStatus: 200, wantSource: contract.SourceArchive, wantArchives: 1}, {name: "unknown", liveErr: contract.ErrNotFound, archiveErr: contract.ErrNotFound, wantStatus: 404, wantArchives: 1}, {name: "live failure never falls back", liveErr: fmt.Errorf("live offline"), wantStatus: 500}, {name: "round error never falls back", roundsErr: fmt.Errorf("rounds offline"), wantStatus: 500, wantRounds: 1}, {name: "missing rounds never fall back", roundsErr: contract.ErrNotFound, wantStatus: 404, wantRounds: 1}, {name: "archive error", liveErr: contract.ErrNotFound, archiveErr: fmt.Errorf("archive offline"), wantStatus: 500, wantArchives: 1}, } { t.Run(test.name, func(t *testing.T) { var liveCalls, archiveCalls, roundsCalls int reader, err := NewRunReader( liveFunc(func(_ context.Context, id string) (contract.RunMeta, error) { liveCalls++ if id != "run-1" { t.Fatalf("live ID = %q", id) } return contract.RunMeta{RunID: id, CurrentRound: 2}, test.liveErr }), archiveFunc(func(_ context.Context, id string) (contract.RunArchive, error) { archiveCalls++ if id != "run-1" { t.Fatalf("archive ID = %q", id) } return contract.RunArchive{Run: contract.RunMeta{RunID: id, CurrentRound: 3}, Rounds: []contract.RoundStats{{RunID: id, Round: 3}}}, test.archiveErr }), roundsFunc(func(_ context.Context, id string) ([]contract.RoundStats, error) { roundsCalls++ if id != "run-1" { t.Fatalf("rounds ID = %q", id) } return []contract.RoundStats{{RunID: id, Round: 2}}, test.roundsErr }), ) if err != nil { t.Fatal(err) } deps := testDependencies() deps.Runs = reader response := request(testHandler(t, deps), http.MethodGet, runPath, "") if response.Code != test.wantStatus { t.Fatalf("response = %d %s", response.Code, response.Body) } if liveCalls != 1 || archiveCalls != test.wantArchives || roundsCalls != test.wantRounds { t.Fatalf("calls: live=%d archive=%d rounds=%d", liveCalls, archiveCalls, roundsCalls) } if response.Code == 200 { var view contract.RunView if err := json.Unmarshal(response.Body.Bytes(), &view); err != nil { t.Fatal(err) } if view.Source != test.wantSource || view.Run.RunID != "run-1" || len(view.Rounds) != 1 || view.Rounds[0].Round != view.Run.CurrentRound { t.Fatalf("view = %+v", view) } } }) } } func TestRunReaderOptionalServicesAndValidation(t *testing.T) { if _, err := NewRunReader(nil, nil, nil); err == nil { t.Fatal("accepted nil live reader") } calls := 0 reader, err := NewRunReader(liveFunc(func(_ context.Context, id string) (contract.RunMeta, error) { calls++ if id == "missing" { return contract.RunMeta{}, contract.ErrNotFound } return contract.RunMeta{RunID: id}, nil }), nil, nil) if err != nil { t.Fatal(err) } if _, err := reader.GetRun(context.Background(), "bad/id"); err == nil || calls != 0 { t.Fatal("invalid ID reached live reader") } view, err := reader.GetRun(context.Background(), "run-1") if err != nil || view.Source != contract.SourceLive || view.Rounds != nil { t.Fatalf("view = %+v, err = %v", view, err) } if _, err := reader.GetRun(context.Background(), "missing"); err != contract.ErrNotFound { t.Fatalf("error = %v", err) } }