102 lines
3.0 KiB
Go
102 lines
3.0 KiB
Go
package shared
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"gitea.maximumdirect.net/eric/notarius/internal/core/source"
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
|
)
|
|
|
|
func TestChunkPromptMaterial(t *testing.T) {
|
|
chunk := &source.Chunk{
|
|
ID: "session-alpha:chunk:0",
|
|
Content: []byte(`{"units":[1,2]}`),
|
|
MediaType: "application/json",
|
|
}
|
|
tests := []struct {
|
|
name string
|
|
sourceInput contracts.LLMInputMaterial
|
|
want contracts.LLMInputMaterial
|
|
wantErr string
|
|
mutateOutput bool
|
|
}{
|
|
{
|
|
name: "fallback to chunk content",
|
|
want: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "", ""),
|
|
},
|
|
{
|
|
name: "clone isolation",
|
|
sourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:source", "file:///source.json"),
|
|
want: contracts.NewLLMInputMaterial("source", chunk.MediaType, chunk.Content, "sha256:source", "file:///source.json"),
|
|
mutateOutput: true,
|
|
},
|
|
{
|
|
name: "mismatched content",
|
|
sourceInput: contracts.NewLLMInputMaterial("source", chunk.MediaType, []byte(`{"units":[9]}`), "sha256:other", "file:///other.json"),
|
|
wantErr: "source input must match chunk",
|
|
},
|
|
{
|
|
name: "default fields",
|
|
sourceInput: contracts.LLMInputMaterial{
|
|
Content: append([]byte(nil), chunk.Content...),
|
|
Digest: "sha256:source",
|
|
OriginURI: "file:///source.json",
|
|
},
|
|
want: contracts.LLMInputMaterial{
|
|
Name: "source",
|
|
MediaType: chunk.MediaType,
|
|
Content: append([]byte(nil), chunk.Content...),
|
|
Digest: "sha256:source",
|
|
OriginURI: "file:///source.json",
|
|
SizeBytes: int64(len(chunk.Content)),
|
|
},
|
|
},
|
|
{
|
|
name: "preserve explicit metadata",
|
|
sourceInput: contracts.LLMInputMaterial{
|
|
Name: "transcript",
|
|
MediaType: "text/plain",
|
|
Content: append([]byte(nil), chunk.Content...),
|
|
Digest: "sha256:explicit",
|
|
OriginURI: "file:///explicit.txt",
|
|
SizeBytes: 42,
|
|
},
|
|
want: contracts.LLMInputMaterial{
|
|
Name: "transcript",
|
|
MediaType: "text/plain",
|
|
Content: append([]byte(nil), chunk.Content...),
|
|
Digest: "sha256:explicit",
|
|
OriginURI: "file:///explicit.txt",
|
|
SizeBytes: 42,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
req := contracts.TypedExtractionRequest{Chunk: chunk, SourceInput: test.sourceInput}
|
|
got, err := ChunkPromptMaterial(req)
|
|
if test.wantErr != "" {
|
|
if err == nil || !strings.Contains(err.Error(), test.wantErr) {
|
|
t.Fatalf("ChunkPromptMaterial() error = %v, want %q", err, test.wantErr)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("ChunkPromptMaterial() error = %v, want nil", err)
|
|
}
|
|
if !reflect.DeepEqual(got, test.want) {
|
|
t.Fatalf("ChunkPromptMaterial() = %#v, want %#v", got, test.want)
|
|
}
|
|
if test.mutateOutput {
|
|
got.Content[0] = 'x'
|
|
if string(test.sourceInput.Content) != string(chunk.Content) {
|
|
t.Fatalf("ChunkPromptMaterial() output shares content with source input")
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|