test(types): regression test that ContextHeader is hidden from JSON
Locks in two invariants exposed during the integration audit: - ContextHeader on types.Chunk must never leak into the JSON representation served to API clients (json:"-" + gorm:"-"). - types.ParsedChunk.EmbeddingContent prepends ContextHeader with a blank line separator and falls back to plain Content when the header is unset. https://claude.ai/code/session_01XADhx6mtu2ZYW3DE9Lun6k
This commit is contained in:
@@ -0,0 +1,35 @@
|
|||||||
|
package types
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestChunkContextHeaderHiddenInJSON(t *testing.T) {
|
||||||
|
c := Chunk{
|
||||||
|
ID: "test",
|
||||||
|
Content: "body",
|
||||||
|
ContextHeader: "# Heading",
|
||||||
|
}
|
||||||
|
b, err := json.Marshal(c)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if strings.Contains(string(b), "Heading") || strings.Contains(string(b), "context_header") {
|
||||||
|
t.Errorf("ContextHeader leaked into JSON: %s", string(b))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestParsedChunkEmbeddingContent(t *testing.T) {
|
||||||
|
pc := ParsedChunk{Content: "body", ContextHeader: "# H"}
|
||||||
|
got := pc.EmbeddingContent()
|
||||||
|
want := "# H\n\nbody"
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("EmbeddingContent mismatch: got %q, want %q", got, want)
|
||||||
|
}
|
||||||
|
pc2 := ParsedChunk{Content: "only body"}
|
||||||
|
if pc2.EmbeddingContent() != "only body" {
|
||||||
|
t.Errorf("EmbeddingContent without header should equal Content")
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user