package server import ( "os" "path/filepath" "strings" "testing" "git.a-wan.net/alan/pipelines/internal/protocol" "git.a-wan.net/alan/pipelines/internal/stage" ) func TestSessionExportCompleteness(t *testing.T) { for _, tc := range []struct { evidence protocol.SessionExport want string }{ {protocol.SessionExport{Source: "native_export", HTML: "session.html", HTMLBytes: 100}, "empty"}, {protocol.SessionExport{Source: "native_export", Diagnostic: "export failed"}, "failed"}, {protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100, Truncated: true}, "partial"}, {protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 100}, "complete"}, {protocol.SessionExport{Source: "unavailable"}, "unavailable"}, } { classifySessionExport(&tc.evidence) if tc.evidence.Status != tc.want { t.Fatalf("%+v want %s", tc.evidence, tc.want) } if tc.want == "empty" && tc.evidence.Diagnostic == "" { t.Fatal("empty export has no warning") } } } func TestRuntimeSessionExportFileVerificationDoesNotClaimMissingEvidence(t *testing.T) { for _, kind := range []string{"missing", "empty", "symlink", "normal"} { t.Run(kind, func(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "session.html") switch kind { case "empty": if err := os.WriteFile(path, nil, 0600); err != nil { t.Fatal(err) } case "normal": if err := os.WriteFile(path, []byte("html"), 0600); err != nil { t.Fatal(err) } case "symlink": if err := os.Symlink(filepath.Join(t.TempDir(), "outside"), path); err != nil { t.Fatal(err) } } evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: 4} verifySessionExportFile(evidence, dir) classifySessionExport(evidence) if (evidence.Status == "complete") != (kind == "normal") { t.Fatalf("%s: %+v", kind, evidence) } }) } } func sizedSessionFile(t *testing.T, size int64) string { t.Helper() dir := t.TempDir() path := filepath.Join(dir, "session.html") f, err := os.Create(path) if err != nil { t.Fatal(err) } if err := f.Truncate(size); err != nil { f.Close() t.Fatal(err) } if err := f.Close(); err != nil { t.Fatal(err) } return dir } // TestRuntimeSessionExportVerificationAcceptsAboveTheOldRenderCap pins the // ordinary-session case the previous 2 MiB rendered bound cut. The file is // larger than that old cap and inside the artifact-aligned bound, so normal // export verification must keep it and classify the evidence complete. func TestRuntimeSessionExportVerificationAcceptsAboveTheOldRenderCap(t *testing.T) { size := int64(2<<20) + 4096 dir := sizedSessionFile(t, size) evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: size} verifySessionExportFile(evidence, dir) classifySessionExport(evidence) if evidence.Status != "complete" || evidence.HTML != "session.html" || evidence.HTMLBytes != size || evidence.Diagnostic != "" { t.Fatalf("complete export above the old cap was rejected: %+v", evidence) } } // TestRuntimeSessionExportVerificationRejectsAboveTheNewCap confirms the larger // bound is still a hard bound: an oversized file is not presented as evidence. func TestRuntimeSessionExportVerificationRejectsAboveTheNewCap(t *testing.T) { size := int64(stage.SessionExportFileLimit) + 1 dir := sizedSessionFile(t, size) evidence := &protocol.SessionExport{Source: "native_export", Messages: 2, HTML: "session.html", HTMLBytes: size} verifySessionExportFile(evidence, dir) classifySessionExport(evidence) if evidence.Status != "partial" || evidence.HTML != "" || evidence.HTMLBytes != 0 || !strings.Contains(evidence.Diagnostic, "bounded verification") { t.Fatalf("oversized export was not rejected as partial: %+v", evidence) } } // TestSessionExportFileLimitStaysInsideArtifactBounds keeps the render cap // principled: larger than the cap that cut ordinary sessions, and no larger // than the ordinary per-artifact file bound that serves the file. func TestSessionExportFileLimitStaysInsideArtifactBounds(t *testing.T) { if stage.SessionExportFileLimit <= 2<<20 { t.Fatalf("render cap %d did not grow beyond the old 2 MiB cutoff", stage.SessionExportFileLimit) } if stage.SessionExportFileLimit > maxArtifactFileBytes { t.Fatalf("render cap %d exceeds the artifact file bound %d", stage.SessionExportFileLimit, maxArtifactFileBytes) } }