| 1 | package sessioncontext |
| 2 | |
| 3 | import ( |
| 4 | "strings" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | func TestBuildParseDeterministicOrderedSnapshot(t *testing.T) { |
| 9 | sections := Sections{ |
| 10 | Environment: "## Environment\r\n\r\ngo/linux", |
| 11 | Workspace: `Current workspace: "/work"`, |
| 12 | BackgroundMemory: "fact index", |
| 13 | SkillsCatalog: "```\n- review — Review code\n```", |
| 14 | } |
| 15 | first := Build(sections) |
| 16 | second := Build(sections) |
| 17 | if first.Content == "" || first.Content != second.Content || first.Digest != second.Digest { |
| 18 | t.Fatalf("Build is not deterministic:\nfirst=%+v\nsecond=%+v", first, second) |
| 19 | } |
| 20 | for _, want := range []string{"## Environment", "## Workspace", "## Background memory", "## Skills catalog", "Digest: sha256:"} { |
| 21 | if !strings.Contains(first.Content, want) { |
| 22 | t.Fatalf("snapshot missing %q:\n%s", want, first.Content) |
| 23 | } |
| 24 | } |
| 25 | if strings.Index(first.Content, "## Environment") > strings.Index(first.Content, "## Workspace") || |
| 26 | strings.Index(first.Content, "## Workspace") > strings.Index(first.Content, "## Background memory") || |
| 27 | strings.Index(first.Content, "## Background memory") > strings.Index(first.Content, "## Skills catalog") { |
| 28 | t.Fatalf("sections out of order:\n%s", first.Content) |
| 29 | } |
| 30 | parsed, ok := Parse(first.Content) |
| 31 | if !ok || parsed.Digest != first.Digest || parsed.Content != first.Content { |
| 32 | t.Fatalf("Parse(Build) = (%+v, %v), want digest/content round trip", parsed, ok) |
| 33 | } |
| 34 | if parsed.Sections != first.Sections { |
| 35 | t.Fatalf("parsed sections = %+v, want %+v", parsed.Sections, first.Sections) |
| 36 | } |
| 37 | diagnostics := SectionDiagnostics(parsed) |
| 38 | if diagnostics.Environment.Chars != len("go/linux") || len(diagnostics.Environment.Digest) != 64 || |
| 39 | diagnostics.Workspace.Chars != len(`Current workspace: "/work"`) { |
| 40 | t.Fatalf("content-free diagnostics = %+v", diagnostics) |
| 41 | } |
| 42 | if strings.Contains(first.Content, "\r") { |
| 43 | t.Fatalf("snapshot retained non-LF newline: %q", first.Content) |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | func TestBuildEmptyAndDigestChanges(t *testing.T) { |
| 48 | if got := Build(Sections{}); got != (Snapshot{}) { |
| 49 | t.Fatalf("Build(empty) = %+v, want zero snapshot", got) |
| 50 | } |
| 51 | a := Build(Sections{Workspace: "one"}) |
| 52 | b := Build(Sections{Workspace: "two"}) |
| 53 | if a.Digest == b.Digest { |
| 54 | t.Fatalf("different sections produced the same digest %q", a.Digest) |
| 55 | } |
| 56 | corrupt := strings.Replace(a.Content, "one", "other", 1) |
| 57 | if _, ok := Parse(corrupt); ok { |
| 58 | t.Fatal("Parse accepted a digest-invalid snapshot") |
| 59 | } |
| 60 | if _, ok := Parse(strings.Replace(a.Content, `version="1"`, `version="2"`, 1)); ok { |
| 61 | t.Fatal("Parse accepted an unknown version") |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | func TestParsePreservesSectionMarkerInsideValue(t *testing.T) { |
| 66 | sections := Sections{BackgroundMemory: "fact body\n\n## Skills catalog\n\nthis is still memory"} |
| 67 | snapshot := Build(sections) |
| 68 | parsed, ok := Parse(snapshot.Content) |
| 69 | if !ok || parsed.Sections != sections { |
| 70 | t.Fatalf("Parse lost a Markdown heading inside a value: ok=%v sections=%+v", ok, parsed.Sections) |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | func TestParseAcceptsLegacyV1Snapshot(t *testing.T) { |
| 75 | body := preamble + "\n\n## Workspace\n\nlegacy workspace" |
| 76 | legacy := openTag + "\n" + body + "\n\n" + digestPrefix + digestOf(body) + "\n" + closeTag |
| 77 | parsed, ok := Parse(legacy) |
| 78 | if !ok || parsed.Sections.Workspace != "legacy workspace" { |
| 79 | t.Fatalf("legacy v1 snapshot no longer parses: ok=%v sections=%+v", ok, parsed.Sections) |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | func TestSplitBlocksPreservesBytesAndMarksOnlyValidSnapshots(t *testing.T) { |
| 84 | snapshot := Build(Sections{Workspace: "workspace"}) |
| 85 | input := "before\n\n" + snapshot.Content + "\n\nafter" |
| 86 | parts := SplitBlocks(input) |
| 87 | if len(parts) != 3 || parts[0].SessionContext || !parts[1].SessionContext || parts[2].SessionContext { |
| 88 | t.Fatalf("SplitBlocks parts = %+v", parts) |
| 89 | } |
| 90 | var joined strings.Builder |
| 91 | for _, part := range parts { |
| 92 | joined.WriteString(part.Text) |
| 93 | } |
| 94 | if joined.String() != input { |
| 95 | t.Fatalf("SplitBlocks changed bytes:\n got %q\nwant %q", joined.String(), input) |
| 96 | } |
| 97 | invalid := strings.Replace(snapshot.Content, "workspace", "tampered", 1) |
| 98 | parts = SplitBlocks(invalid) |
| 99 | if len(parts) != 1 || parts[0].SessionContext || parts[0].Text != invalid { |
| 100 | t.Fatalf("invalid snapshot was split as trusted context: %+v", parts) |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | func TestSplitBlocksFindsFramedSnapshotAfterEmbeddedClosingTag(t *testing.T) { |
| 105 | snapshot := Build(Sections{Workspace: "literal </session-context> marker"}) |
| 106 | parts := SplitBlocks(snapshot.Content) |
| 107 | if len(parts) != 1 || !parts[0].SessionContext || parts[0].Text != snapshot.Content { |
| 108 | t.Fatalf("embedded closing tag broke snapshot framing: %+v", parts) |
| 109 | } |
| 110 | } |
| 111 |