Files
silo-server/internal/markers/plugin_provider_test.go
T
0163df3683 [codex] Add IntroDB marker integration and dialogue-aware Chromaprint refinement (#57)
* docs(markers): design + implementation plans for multi-source markers & TheIntroDB contribution

* fix(markers): TheIntroDB read-path correctness (TVDB, real confidence, best candidate)

Honor TVDB ids in /media lookups (previously dropped — anime/TheTVDB-first
libraries got no markers), decode and use the real per-segment confidence and
submission_count instead of a hardcoded 0.9, and pick the most-submitted /
highest-confidence candidate when several are returned. Adds httptest coverage
for the introdb client and provider.

Phase 1 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(markers): multi-source dispatch, per-provider config, per-segment provenance

Add marker_provider_config (per-provider fetch enable/priority + contribute
gates, contribution off by default) and a cached ProviderConfigStore. Add
Registry.FetchMerged: query all fetch-enabled providers concurrently and keep
the best candidate per segment (submission_count, then confidence, then fetch
priority), stamping each winning marker with its provider/algorithm. Thread
per-segment provenance through MarkerUpdatePayload and scanner.MarkerUpdate
(additive SegmentProvenance overrides) so a merged result writes correct
per-segment provider/confidence/algorithm; the legacy shared columns keep a
summary. The lazy-playback path now uses FetchMerged. With only TheIntroDB
enabled, behavior is unchanged.

Phase 2 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(markers): TheIntroDB submission client, contribution audit, service engine

Add a markers.Submitter capability and implement it on the introdb provider
(POST /v3/submit, GET /v3/user/stats; key required, usage-limit aware, applies
the null start/end conventions). Add the marker_contributions audit table and a
value-hash-keyed ContributionStore for idempotency. Add ContributionService:
resolves enabled submitter providers, gates eligibility (never re-submit
online-sourced markers; auto runs require contribute_auto_local + scanner-intro
above the per-provider confidence threshold), checks idempotency, submits, and
records. Wired in main.go; no trigger yet (admin API and task follow).

Phase 3 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(api): admin marker editing, contribution, and provider config endpoints

Add the RequireAdmin marker API: GET/PUT /admin/files/{id}/markers (read with
provenance; manual upsert where a segment object sets and null clears),
DELETE .../markers/{segment}, POST .../contribute and GET .../contributions,
plus GET/PUT /admin/markers/providers[/{provider}] and a
.../validate key-check returning user stats. Manual writes go through the
priority-gated UpsertMarkers (source=manual) and notify live sessions; a new
FileRepository.ClearMarkers nulls a segment's columns. Validation mirrors the
contribution rules.

Phase 4 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* feat(markers): daily auto-contribution task for local intro markers

Add ContributeMarkersTask (daily 04:00, after local detection): when a provider
has contribute_enabled + contribute_auto_local, page through episode files with
a scanner intro marker at/above the provider's confidence threshold (new
ContributionStore.CandidateLocalIntroFiles keyset query) and run them through
ContributionService with Auto=true. No-op when no provider opts in; idempotent
and resumable across runs.

Phase 5 of docs/superpowers/plans/2026-06-06-marker-sources-and-contribution-implementation.md

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* fix(intromarkers): refine chromaprint starts with dialogue cues

* feat(markers): finish marker management backend

* feat(web): add marker editing UI

* feat(markers): use plugin marker providers

* fix(markers): address PR review feedback

* feat(player): show marker labels on seek hover

* fix(markers): type nullable marker mutation params

* feat(markers): audit marker edits and add permission

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-06-06 22:29:26 -04:00

162 lines
5.5 KiB
Go

package markers
import (
"context"
"testing"
"time"
pluginv1 "github.com/Silo-Server/silo-plugin-sdk/pkg/pluginproto/silo/plugin/v1"
"github.com/Silo-Server/silo-server/internal/models"
)
type fakePluginMarkerClient struct {
fetchResp *pluginv1.FetchMarkersResponse
fetchReq *pluginv1.FetchMarkersRequest
submitReq *pluginv1.SubmitMarkerRequest
}
func (f *fakePluginMarkerClient) FetchMarkers(_ context.Context, req *pluginv1.FetchMarkersRequest) (*pluginv1.FetchMarkersResponse, error) {
f.fetchReq = req
return f.fetchResp, nil
}
func (f *fakePluginMarkerClient) SubmitMarker(_ context.Context, req *pluginv1.SubmitMarkerRequest) (*pluginv1.SubmitMarkerResponse, error) {
f.submitReq = req
return &pluginv1.SubmitMarkerResponse{SubmissionId: "sub1", Status: SubmissionStatusPending, Weight: 2}, nil
}
func (f *fakePluginMarkerClient) GetMarkerProviderStats(context.Context, *pluginv1.GetMarkerProviderStatsRequest) (*pluginv1.MarkerProviderStatsResponse, error) {
return &pluginv1.MarkerProviderStatsResponse{Total: 3, Accepted: 2, Pending: 1, AcceptanceRate: 0.66}, nil
}
func TestPluginProviderFetchMapsAllSegments(t *testing.T) {
start10, end60 := 10.0, 60.0
creditsStart := 1700.0
previewStart := 1750.0
client := &fakePluginMarkerClient{fetchResp: &pluginv1.FetchMarkersResponse{Markers: []*pluginv1.MarkerSegment{
{Segment: "intro", StartSeconds: &start10, EndSeconds: &end60, Confidence: 0.8, SubmissionCount: 2, Algorithm: "intro:v1"},
{Segment: "credits", StartSeconds: &creditsStart, Confidence: 0.9, SubmissionCount: 3},
{Segment: "recap", EndSeconds: &start10, Confidence: 0.7},
{Segment: "preview", StartSeconds: &previewStart, Confidence: 0.6},
}}}
provider, err := NewPluginProviderWithClientFactory(PluginProviderOptions{
InstallationID: 12,
CapabilityID: "markers",
DisplayName: "Markers",
PluginID: "silo.markers",
}, func(context.Context, int, string) (pluginMarkerClient, error) {
return client, nil
})
if err != nil {
t.Fatalf("NewPluginProviderWithClientFactory: %v", err)
}
res, err := provider.FetchMarkers(context.Background(), Request{
Kind: ItemKindEpisode,
ExternalIDs: map[string]string{ExternalIDKeyTVDB: "777"},
SeasonNumber: 1,
EpisodeNumber: 2,
Duration: 1800 * time.Second,
})
if err != nil {
t.Fatalf("FetchMarkers: %v", err)
}
if client.fetchReq.GetItemType() != "episode" || client.fetchReq.GetExternalIds().GetTvdbId() != "777" {
t.Fatalf("fetch request = %+v", client.fetchReq)
}
if res.SourceClass != models.MarkerSourcePlugin || res.ProviderID != "plugin:12:markers" {
t.Fatalf("result provenance = source %q provider %q", res.SourceClass, res.ProviderID)
}
byKind := map[MarkerKind]Marker{}
for _, marker := range res.Markers {
byKind[marker.Kind] = marker
if marker.SourceClass != models.MarkerSourcePlugin || marker.ProviderID != "plugin:12:markers" {
t.Fatalf("marker provenance = %+v", marker)
}
}
if len(byKind) != 4 {
t.Fatalf("mapped %d markers, want 4: %+v", len(byKind), res.Markers)
}
if got := byKind[MarkerKindCredits]; got.End != 1800*time.Second {
t.Fatalf("credits end = %s, want duration default", got.End)
}
if got := byKind[MarkerKindRecap]; got.Start != 0 {
t.Fatalf("recap start = %s, want zero default", got.Start)
}
}
func TestPluginProviderRejectsOutOfBoundsSegments(t *testing.T) {
negativeStart := -1.0
start10, end61 := 10.0, 61.0
validStart := 50.0
duration := time.Minute
tests := []struct {
name string
segment *pluginv1.MarkerSegment
wantOK bool
wantEnd time.Duration
}{
{
name: "negative start",
segment: &pluginv1.MarkerSegment{Segment: "intro", StartSeconds: &negativeStart},
},
{
name: "end past duration",
segment: &pluginv1.MarkerSegment{Segment: "intro", StartSeconds: &start10, EndSeconds: &end61},
},
{
name: "default end uses duration",
segment: &pluginv1.MarkerSegment{Segment: "credits", StartSeconds: &validStart},
wantOK: true,
wantEnd: duration,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
marker, ok := markerFromPluginSegment(tt.segment, duration)
if ok != tt.wantOK {
t.Fatalf("ok = %v, want %v", ok, tt.wantOK)
}
if tt.wantOK && marker.End != tt.wantEnd {
t.Fatalf("end = %s, want %s", marker.End, tt.wantEnd)
}
})
}
}
func TestPluginProviderSubmitMapsRequest(t *testing.T) {
client := &fakePluginMarkerClient{}
provider, err := NewPluginProviderWithClientFactory(PluginProviderOptions{
InstallationID: 12,
CapabilityID: "markers",
}, func(context.Context, int, string) (pluginMarkerClient, error) {
return client, nil
})
if err != nil {
t.Fatalf("NewPluginProviderWithClientFactory: %v", err)
}
start, end := 5*time.Second, 30*time.Second
result, err := provider.SubmitMarker(context.Background(), SubmissionRequest{
Kind: ItemKindMovie,
ExternalIDs: map[string]string{ExternalIDKeyIMDB: "tt1"},
Segment: MarkerKindIntro,
Start: &start,
End: &end,
Duration: 90 * time.Minute,
})
if err != nil {
t.Fatalf("SubmitMarker: %v", err)
}
if result.ID != "sub1" || result.Status != SubmissionStatusPending || result.Weight != 2 {
t.Fatalf("submit result = %+v", result)
}
if client.submitReq.GetItemType() != "movie" || client.submitReq.GetExternalIds().GetImdbId() != "tt1" {
t.Fatalf("submit request identity = %+v", client.submitReq)
}
if client.submitReq.GetSegment() != "intro" || client.submitReq.GetStartSeconds() != 5 || client.submitReq.GetEndSeconds() != 30 {
t.Fatalf("submit request segment = %+v", client.submitReq)
}
}