This commit is contained in:
1 parent
74d21cb204
commit
7d3b6804af
5 files changed
+220
-15
No files matched your search
@@ -309,3 +309,84 @@ func TestXlsxTranslateToolMissingFile(t *testing.T) {
|
||||
t.Fatalf("missing file should fail")
|
||||
}
|
||||
}
|
||||
|
||||
// dropItemsChat simulates the deterministic 192 failure: for any batch larger
|
||||
// than one string the model "merges" two items and returns N-1 translations.
|
||||
type dropItemsChat struct{}
|
||||
|
||||
func (f *dropItemsChat) Chat(ctx context.Context, messages []chat.Message, opts *chat.ChatOptions) (*types.ChatResponse, error) {
|
||||
resp, err := f.ChatStream(ctx, messages, opts)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var content strings.Builder
|
||||
for chunk := range resp {
|
||||
content.WriteString(chunk.Content)
|
||||
}
|
||||
return &types.ChatResponse{Content: content.String()}, nil
|
||||
}
|
||||
|
||||
func (f *dropItemsChat) ChatStream(_ context.Context, messages []chat.Message, opts *chat.ChatOptions) (<-chan types.StreamResponse, error) {
|
||||
content := messages[len(messages)-1].Content
|
||||
start := strings.IndexByte(content, '[')
|
||||
end := strings.LastIndexByte(content, ']')
|
||||
var texts []string
|
||||
if err := json.Unmarshal([]byte(content[start:end+1]), &texts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(texts) > 1 {
|
||||
texts = texts[:len(texts)-1] // drop the last item: 19 of 20
|
||||
} else if len(texts) == 1 {
|
||||
texts = nil // single stubborn string: model refuses to translate it
|
||||
}
|
||||
out := make([]string, 0, len(texts))
|
||||
for _, t := range texts {
|
||||
out = append(out, replaceCJKRuns(t))
|
||||
}
|
||||
b, _ := json.Marshal(out)
|
||||
ch := make(chan types.StreamResponse, 2)
|
||||
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Content: string(b)}
|
||||
ch <- types.StreamResponse{ResponseType: types.ResponseTypeAnswer, Done: true}
|
||||
close(ch)
|
||||
return ch, nil
|
||||
}
|
||||
|
||||
func (f *dropItemsChat) GetModelName() string { return "dropping" }
|
||||
func (f *dropItemsChat) GetModelID() string { return "dropping-id" }
|
||||
|
||||
// TestXlsxTranslateBatchCountMismatchRecovers covers the field failure where
|
||||
// the model returns N-1 translations for N inputs: bisection must realign
|
||||
// instead of failing the run.
|
||||
func TestXlsxTranslateBatchCountMismatchRecovers(t *testing.T) {
|
||||
texts := make([]string, 20)
|
||||
for i := range texts {
|
||||
texts[i] = fmt.Sprintf("检验项目 %d", i+1)
|
||||
}
|
||||
tr := &chatBatchTranslator{model: &dropItemsChat{}}
|
||||
out, err := tr.TranslateBatch(context.Background(), texts)
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateBatch: %v", err)
|
||||
}
|
||||
if len(out) != len(texts) {
|
||||
t.Fatalf("recovered %d results for %d inputs", len(out), len(texts))
|
||||
}
|
||||
for i := range out {
|
||||
if strings.TrimSpace(out[i]) == "" {
|
||||
t.Fatalf("item %d empty after recovery", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestXlsxTranslateBatchSingleStubbornStringKeepsOriginal covers the terminal
|
||||
// bisection case: one string the model refuses to translate must degrade to
|
||||
// the original text, not fail the run.
|
||||
func TestXlsxTranslateBatchSingleStubbornStringKeepsOriginal(t *testing.T) {
|
||||
tr := &chatBatchTranslator{model: &dropItemsChat{}}
|
||||
out, err := tr.TranslateBatch(context.Background(), []string{"顽固文本"})
|
||||
if err != nil {
|
||||
t.Fatalf("TranslateBatch: %v", err)
|
||||
}
|
||||
if out[0] != "顽固文本" {
|
||||
t.Fatalf("single item altered: %q", out[0])
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user