Add typed context compiler v1
This commit is contained in:
@@ -0,0 +1,59 @@
|
||||
package context_compiler_test
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"git.cin.su/m/Assistant/contracts/capability"
|
||||
"git.cin.su/m/Assistant/contracts/cognitive"
|
||||
)
|
||||
|
||||
func TestCompileContextOrdersDeduplicatesAndBudgets(t *testing.T) {
|
||||
bundle, err := cognitive.CompileContext(cognitive.ContextCompileInput{
|
||||
MaxBytes: 4,
|
||||
Candidates: []cognitive.ContextCandidate{
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "low", Sensitivity: cognitive.SensitivityPublic, Text: "low"}, Relevance: 1},
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "high", Sensitivity: cognitive.SensitivityPublic, Text: "éé"}, Relevance: 2},
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "high", Sensitivity: cognitive.SensitivityPublic, Text: "hi"}, Relevance: 3},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(bundle.Excerpts) != 1 || bundle.Excerpts[0].ID != "high" || bundle.Bytes != 2 {
|
||||
t.Fatalf("unexpected bundle: %#v", bundle)
|
||||
}
|
||||
if len(bundle.Omissions) != 2 || bundle.Omissions[0].Reason != "duplicate" || bundle.Omissions[1].Reason != "budget" {
|
||||
t.Fatalf("unexpected omissions: %#v", bundle.Omissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompileContextFiltersSensitivityByPolicy(t *testing.T) {
|
||||
bundle, err := cognitive.CompileContext(cognitive.ContextCompileInput{
|
||||
MaxBytes: 100,
|
||||
DataPolicy: capability.DataPolicyExternalMinimizedContext,
|
||||
Candidates: []cognitive.ContextCandidate{
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "public", Sensitivity: cognitive.SensitivityPublic, Text: "ok"}},
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "private", Sensitivity: cognitive.SensitivityPrivateAllowed, Text: "private"}},
|
||||
{Excerpt: cognitive.ContextExcerpt{ID: "secret", Sensitivity: cognitive.SensitivitySecret, Text: "secret"}},
|
||||
},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(bundle.Excerpts) != 1 || bundle.Excerpts[0].ID != "public" {
|
||||
t.Fatalf("unexpected excerpts: %#v", bundle.Excerpts)
|
||||
}
|
||||
if len(bundle.Omissions) != 2 {
|
||||
t.Fatalf("unexpected omissions: %#v", bundle.Omissions)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCompileContextAllowsEmptyResultAndRejectsInvalidBudget(t *testing.T) {
|
||||
if _, err := cognitive.CompileContext(cognitive.ContextCompileInput{}); err == nil {
|
||||
t.Fatal("expected invalid budget")
|
||||
}
|
||||
bundle, err := cognitive.CompileContext(cognitive.ContextCompileInput{MaxBytes: 10, Candidates: []cognitive.ContextCandidate{{Excerpt: cognitive.ContextExcerpt{ID: "secret", Sensitivity: cognitive.SensitivitySecret, Text: "x"}}}})
|
||||
if err != nil || len(bundle.Excerpts) != 0 || len(bundle.Omissions) != 1 {
|
||||
t.Fatalf("unexpected empty result: %#v %v", bundle, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user