Vellum/internal/engine/engine_test.go
mithral 67f145a9b1 Waves 7-8: playback engine selection, and missing files you can see
Wave 7 - engines, chosen per file by the client's own capabilities
- internal/engine: a registry. html5 is built in and declares a CONSERVATIVE
  browser baseline (h264/vp8/vp9/av1, deliberately no hevc and no mpeg4, because
  that is what was measured). mpv, vlc and mdk are registered as native engines
  declaring the broad support those libraries really have.
- The server never declares a file playable. It reports what is in the file and
  matches it against the engines the CLIENT declares. Proven:
      engines=["html5"]       -> playable=false engine=null reason=vcodec:hevc
      engines=["html5","mpv"] -> playable=true  engine=mpv
  Same file, different answer, because the client offered a different engine.
  That is what makes adding a native client a registration rather than a rewrite.
- internal/playback: GET /playback/engines and POST /playback/resolve, behind
  auth and the same grant chokepoint. An unreadable item is 404, not 403.
- web/src/lib/capabilities.ts: uses navigator.mediaCapabilities.decodingInfo(),
  which returns {supported, smooth, powerEfficient}. That is real client-hardware
  detection and it distinguishes "cannot decode" from "can decode but it will
  stutter and drain the battery". Feature-detected; degrades to the server
  baseline rather than throwing.
- VideoPlayer consults the server as an ADVISORY only. The element remains the
  source of truth: the server's note appears first, then the element's own
  confirmation replaces it once videoWidth proves the answer. Verified on HEVC —
  "Playback may not work here" at 0-906ms, "Video cannot play here" from ~1208ms.

Wave 8 - missing files: visibility and an explicit purge
- migrations/0011 adds missing_since to media_items and media_files.
- It is written only on the TRANSITION into missing, so the age says when the
  file first vanished rather than the last rescan. Proven byte-identical across a
  rescan, and cleared when a file is restored.
- GET /api/v1/missing (grant-scoped) and POST /api/v1/libraries/{id}/purge
  (admin only). The purge deletes only items that are missing AND have no
  present file, and its cascade into progress/item_people/item_views/game_saves
  is enumerated in a comment — that cascade is exactly why deletion is never
  automatic. Proven: 19 items -> 18, only the missing one, the non-missing item
  untouched and its files intact, second purge returns 0.
- MissingFiles.svelte in the Manage nav: title, kind, library, path, human age,
  and a per-library Purge behind a confirmation that names the count and says
  progress cannot be regenerated.

Coordinator wired playback.NewHandlers into main.go (the one line agents were
told to leave alone).

Verified end to end: 17 packages pass, gofmt/vet clean, svelte-check 0/0, and
every claim above checked against the running server.
2026-10-11 14:36:55 -04:00

245 lines
6.8 KiB
Go

package engine
import (
"encoding/json"
"strings"
"testing"
)
// TestRegistryHasBuiltins pins the engines a client can name out of the box.
func TestRegistryHasBuiltins(t *testing.T) {
for _, id := range []string{HTML5ID, "mpv", "vlc", "mdk"} {
if _, ok := Get(id); !ok {
t.Errorf("engine %q is not registered", id)
}
}
if _, ok := Get("nope"); ok {
t.Error("Get returned an unknown engine")
}
// Get is case/space tolerant so a client's casing never matters.
if _, ok := Get(" HTML5 "); !ok {
t.Error("Get should trim and lower-case the id")
}
}
// TestHTML5BaselineIsHonest guards the measured facts: the browser baseline must
// NOT claim HEVC or mpeg4, which Chrome cannot decode, and must not claim a
// container it does not handle.
func TestHTML5BaselineIsHonest(t *testing.T) {
html5, _ := Get(HTML5ID)
if contains(html5.VCodecs, "hevc") || contains(html5.VCodecs, "h265") {
t.Error("html5 baseline claims HEVC, which browsers cannot decode")
}
if contains(html5.VCodecs, "mpeg4") {
t.Error("html5 baseline claims mpeg4, which browsers cannot decode")
}
if !contains(html5.VCodecs, "h264") || !contains(html5.VCodecs, "vp9") || !contains(html5.VCodecs, "av1") {
t.Errorf("html5 baseline is missing a measured-playable codec: %v", html5.VCodecs)
}
if !contains(html5.Containers, "mkv") {
t.Error("html5 baseline must accept mkv (h264-in-mkv plays)")
}
if contains(html5.Containers, "avi") {
t.Error("html5 baseline must not claim avi")
}
}
// TestEngineJSONMatchesContract pins the frozen wire shape (web/src/lib/api.ts).
func TestEngineJSONMatchesContract(t *testing.T) {
e, _ := Get(HTML5ID)
b, err := json.Marshal(e)
if err != nil {
t.Fatal(err)
}
var raw map[string]json.RawMessage
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}
for _, key := range []string{"id", "name", "kind", "containers", "vcodecs", "acodecs"} {
if _, ok := raw[key]; !ok {
t.Errorf("engine JSON is missing %q", key)
}
}
if len(raw) != 6 {
t.Errorf("engine JSON has %d keys, want exactly 6: %s", len(raw), b)
}
}
func TestResolve(t *testing.T) {
tests := []struct {
name string
facts []string
engines []string
playable bool
engine string // expected engine id, or "" for nil
reason string
msgSubstr string // must appear in Message when not playable
}{
{
name: "mp4 h264 aac plays in html5",
facts: Facts("mp4", "h264", "aac"),
engines: []string{"html5"},
playable: true,
engine: "html5",
},
{
name: "mkv h264 plays despite the container",
facts: Facts("mkv", "h264", "aac"),
engines: []string{"html5"},
playable: true,
engine: "html5",
},
{
name: "mp4 hevc aac is not playable",
facts: Facts("mp4", "hevc", "aac"),
engines: []string{"html5"},
playable: false,
reason: "vcodec:hevc",
msgSubstr: "HEVC",
},
{
name: "avi mpeg4 is a container blocker",
facts: Facts("avi", "mpeg4", "mp3"),
engines: []string{"html5"},
playable: false,
reason: "container:avi",
msgSubstr: "AVI",
},
{
name: "unsupported audio codec",
facts: Facts("mp4", "h264", "dts"),
engines: []string{"html5"},
playable: false,
reason: "acodec:dts",
msgSubstr: "DTS",
},
{
name: "audio-only mp3 is playable",
facts: Facts("mp3", "", "mp3"),
engines: []string{"html5"},
playable: true,
engine: "html5",
},
{
name: "audio-only flac is playable",
facts: Facts("flac", "", "flac"),
engines: []string{"html5"},
playable: true,
engine: "html5",
},
{
name: "audio-only dts is not playable",
facts: Facts("m4a", "", "dts"),
engines: []string{"html5"},
playable: false,
reason: "acodec:dts",
msgSubstr: "DTS",
},
{
name: "empty engine list falls back to html5 baseline",
facts: Facts("mp4", "h264", "aac"),
engines: nil,
playable: true,
engine: "html5",
},
{
name: "empty engine list still rejects hevc",
facts: Facts("mp4", "hevc", "aac"),
engines: nil,
playable: false,
reason: "vcodec:hevc",
msgSubstr: "mpv",
},
{
name: "native engine changes the answer for hevc",
facts: Facts("mp4", "hevc", "aac"),
engines: []string{"html5", "mpv"},
playable: true,
engine: "mpv",
},
{
name: "native engine handles avi",
facts: Facts("avi", "mpeg4", "mp3"),
engines: []string{"html5", "vlc"},
playable: true,
engine: "vlc",
},
{
name: "client ordering wins among capable engines",
facts: Facts("mkv", "h264", "aac"),
engines: []string{"mpv", "html5"},
playable: true,
engine: "mpv",
},
{
name: "capable engine beats an earlier incapable one",
facts: Facts("mp4", "hevc", "aac"),
engines: []string{"html5", "vlc", "mpv"},
playable: true,
engine: "vlc",
},
{
name: "unknown engines are skipped, reason uses fallback",
facts: Facts("mp4", "hevc", "aac"),
engines: []string{"bogus"},
playable: false,
reason: "vcodec:hevc",
msgSubstr: "mpv",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := Resolve(tt.facts, tt.engines)
if got.Playable != tt.playable {
t.Fatalf("playable = %v, want %v (reason %q)", got.Playable, tt.playable, got.Reason)
}
if tt.playable {
if got.Engine == nil || *got.Engine != tt.engine {
t.Errorf("engine = %v, want %q", got.Engine, tt.engine)
}
if got.Reason != "" || got.Message != "" {
t.Errorf("playable result carried reason=%q message=%q", got.Reason, got.Message)
}
return
}
if got.Engine != nil {
t.Errorf("engine = %q, want null", *got.Engine)
}
if got.Reason != tt.reason {
t.Errorf("reason = %q, want %q", got.Reason, tt.reason)
}
if tt.msgSubstr != "" && !strings.Contains(got.Message, tt.msgSubstr) {
t.Errorf("message = %q, want it to mention %q", got.Message, tt.msgSubstr)
}
if strings.Contains(strings.ToLower(got.Message), "you ") {
t.Errorf("message blames the user: %q", got.Message)
}
})
}
}
// TestResolutionJSONEngineNull pins that a non-playable answer serialises
// engine as JSON null, exactly as PlaybackResolution declares.
func TestResolutionJSONEngineNull(t *testing.T) {
got := Resolve(Facts("mp4", "hevc", "aac"), []string{"html5"})
b, err := json.Marshal(got)
if err != nil {
t.Fatal(err)
}
var raw struct {
Playable bool `json:"playable"`
Engine json.RawMessage `json:"engine"`
Reason string `json:"reason"`
Message string `json:"message"`
}
if err := json.Unmarshal(b, &raw); err != nil {
t.Fatal(err)
}
if string(raw.Engine) != "null" {
t.Errorf("engine = %s, want null", raw.Engine)
}
if raw.Playable || raw.Reason != "vcodec:hevc" || raw.Message == "" {
t.Errorf("unexpected resolution JSON: %s", b)
}
}