Files
silo-server/internal/scanner/file_repo_test.go
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

242 lines
6.8 KiB
Go

package scanner
import (
"testing"
"time"
"github.com/Silo-Server/silo-server/internal/models"
)
func TestRecomputeSharedMarkerAttributionPreservesHigherPrioritySegment(t *testing.T) {
manual := models.MarkerSourceManual
scannerSource := models.MarkerSourceScanner
manualConfidence := 1.0
scannerConfidence := 0.5
nextSource, nextConfidence := recomputeSharedMarkerAttribution(
nil,
nil,
segmentState{
start: floatPtr(0),
end: floatPtr(10),
source: &manual,
confidence: &manualConfidence,
},
segmentState{
start: floatPtr(20),
end: floatPtr(30),
source: &scannerSource,
confidence: &scannerConfidence,
},
)
if nextSource == nil || *nextSource != models.MarkerSourceManual {
t.Fatalf("next source = %v, want manual", nextSource)
}
if nextConfidence == nil || *nextConfidence != manualConfidence {
t.Fatalf("next confidence = %v, want %v", nextConfidence, manualConfidence)
}
}
func TestRecomputeSharedMarkerAttributionUpdatesSamePriorityConfidence(t *testing.T) {
scannerSource := models.MarkerSourceScanner
lowerConfidence := 0.5
higherConfidence := 0.8
nextSource, nextConfidence := recomputeSharedMarkerAttribution(
nil,
nil,
segmentState{
start: floatPtr(0),
end: floatPtr(10),
source: &scannerSource,
confidence: &lowerConfidence,
},
segmentState{
start: floatPtr(20),
end: floatPtr(30),
source: &scannerSource,
confidence: &higherConfidence,
},
)
if nextSource == nil || *nextSource != models.MarkerSourceScanner {
t.Fatalf("next source = %v, want scanner", nextSource)
}
if nextConfidence == nil || *nextConfidence != higherConfidence {
t.Fatalf("next confidence = %v, want %v", nextConfidence, higherConfidence)
}
}
func TestRecomputeSharedMarkerAttributionPromotesHigherPrioritySource(t *testing.T) {
scannerSource := models.MarkerSourceScanner
manual := models.MarkerSourceManual
scannerConfidence := 0.5
manualConfidence := 0.9
nextSource, nextConfidence := recomputeSharedMarkerAttribution(
nil,
nil,
segmentState{
start: floatPtr(0),
end: floatPtr(10),
source: &scannerSource,
confidence: &scannerConfidence,
},
segmentState{
start: floatPtr(20),
end: floatPtr(30),
source: &manual,
confidence: &manualConfidence,
},
)
if nextSource == nil || *nextSource != models.MarkerSourceManual {
t.Fatalf("next source = %v, want manual", nextSource)
}
if nextConfidence == nil || *nextConfidence != manualConfidence {
t.Fatalf("next confidence = %v, want %v", nextConfidence, manualConfidence)
}
}
func TestRecomputeSharedMarkerAttributionUsesLegacyAttributionForUnattributedSegment(t *testing.T) {
existingSource := models.MarkerSourceScanner
existingConfidence := 0.9
nextSource, nextConfidence := recomputeSharedMarkerAttribution(
&existingSource,
&existingConfidence,
segmentState{
start: floatPtr(0),
end: floatPtr(10),
},
)
if nextSource == nil || *nextSource != models.MarkerSourceScanner {
t.Fatalf("next source = %v, want scanner", nextSource)
}
if nextConfidence == nil || *nextConfidence != existingConfidence {
t.Fatalf("next confidence = %v, want %v", nextConfidence, existingConfidence)
}
}
func TestApplySegmentPatchSkipsSemanticNoop(t *testing.T) {
manual := models.MarkerSourceManual
confidence := 1.0
algorithm := "manual:v1"
detectedAt := time.Unix(100, 0).UTC()
state := segmentState{
start: floatPtr(0),
end: floatPtr(60),
source: &manual,
confidence: &confidence,
algorithm: &algorithm,
detectedAt: &detectedAt,
}
changed, err := applySegmentPatch(
&state,
nil,
manual,
nil,
&confidence,
algorithm,
floatPtr(0),
floatPtr(60),
1800,
"intro",
time.Unix(200, 0).UTC(),
)
if err != nil {
t.Fatalf("applySegmentPatch returned error: %v", err)
}
if changed {
t.Fatal("identical marker patch should be a semantic no-op")
}
if state.detectedAt == nil || !state.detectedAt.Equal(detectedAt) {
t.Fatalf("detected_at changed on no-op: %v", state.detectedAt)
}
}
func TestClearSegmentStateSkipsSemanticNoop(t *testing.T) {
empty := segmentState{}
if clearSegmentState(&empty) {
t.Fatal("clearing an empty segment should be a no-op")
}
manual := models.MarkerSourceManual
state := segmentState{start: floatPtr(0), end: floatPtr(60), source: &manual}
if !clearSegmentState(&state) {
t.Fatal("clearing a populated segment should report a change")
}
if state.start != nil || state.end != nil || state.source != nil {
t.Fatalf("segment was not cleared: %+v", state)
}
}
func TestMarkerAuditSegmentForStatePreservesBeforeAfterShape(t *testing.T) {
manual := models.MarkerSourceManual
algorithm := "manual:v1"
confidence := 1.0
detectedAt := time.Unix(300, 0).UTC()
segment := markerAuditSegmentForState(segmentState{
start: floatPtr(5),
end: floatPtr(65),
source: &manual,
confidence: &confidence,
algorithm: &algorithm,
detectedAt: &detectedAt,
})
if segment == nil {
t.Fatal("expected audit segment")
}
if segment.Start == nil || *segment.Start != 5 || segment.End == nil || *segment.End != 65 {
t.Fatalf("audit segment bounds = %v..%v, want 5..65", segment.Start, segment.End)
}
if segment.Source == nil || *segment.Source != manual || segment.Algorithm == nil || *segment.Algorithm != algorithm {
t.Fatalf("audit provenance = source %v algorithm %v", segment.Source, segment.Algorithm)
}
if segment.DetectedAt == nil || !segment.DetectedAt.Equal(detectedAt) {
t.Fatalf("detected_at = %v, want %v", segment.DetectedAt, detectedAt)
}
}
func TestRecomputeSharedMarkerAttributionDropsClearedManualSegment(t *testing.T) {
manual := models.MarkerSourceManual
scannerSource := models.MarkerSourceScanner
manualConfidence := 1.0
scannerConfidence := 0.8
nextSource, nextConfidence := recomputeSharedMarkerAttribution(
&manual,
&manualConfidence,
segmentState{},
segmentState{
start: floatPtr(120),
end: floatPtr(180),
source: &scannerSource,
confidence: &scannerConfidence,
},
)
if nextSource == nil || *nextSource != models.MarkerSourceScanner {
t.Fatalf("next source = %v, want scanner after manual segment clear", nextSource)
}
if nextConfidence == nil || *nextConfidence != scannerConfidence {
t.Fatalf("next confidence = %v, want %v", nextConfidence, scannerConfidence)
}
}
func TestRecomputeSharedMarkerAttributionClearsWhenNoSegmentsRemain(t *testing.T) {
manual := models.MarkerSourceManual
manualConfidence := 1.0
nextSource, nextConfidence := recomputeSharedMarkerAttribution(&manual, &manualConfidence, segmentState{})
if nextSource != nil || nextConfidence != nil {
t.Fatalf("shared attribution = %v/%v, want nil/nil with no remaining segments", nextSource, nextConfidence)
}
}
func floatPtr(v float64) *float64 { return &v }