185 lines
5.6 KiB
Go
185 lines
5.6 KiB
Go
package tools
|
|
|
|
import (
|
|
"archive/zip"
|
|
"bytes"
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
"academy/internal/models/chat"
|
|
"academy/internal/sandbox"
|
|
"academy/internal/types"
|
|
)
|
|
|
|
// fakeTranslateStore is an in-memory SessionFileSink + dir ensurer.
|
|
type fakeTranslateStore struct {
|
|
files map[string][]byte
|
|
ensured []string
|
|
writePath string
|
|
}
|
|
|
|
func (s *fakeTranslateStore) EnsureSessionDir(_ context.Context, _, dir string) error {
|
|
s.ensured = append(s.ensured, dir)
|
|
return nil
|
|
}
|
|
|
|
func (s *fakeTranslateStore) StatSessionFile(_ context.Context, _, _ string) (*sandbox.RemoteStatEntry, error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (s *fakeTranslateStore) ReadSessionFile(_ context.Context, _, path string) ([]byte, error) {
|
|
if data, ok := s.files[path]; ok {
|
|
return data, nil
|
|
}
|
|
return nil, fmt.Errorf("no such file: %s", path)
|
|
}
|
|
|
|
func (s *fakeTranslateStore) WriteSessionWorkspaceFile(_ context.Context, _, path string, content []byte) error {
|
|
s.files[path] = content
|
|
s.writePath = path
|
|
return nil
|
|
}
|
|
|
|
// fakeTranslateChat answers every batch with the requested array translated
|
|
// deterministically: CJK runs become " EN ".
|
|
type fakeTranslateChat struct {
|
|
prompts []string
|
|
}
|
|
|
|
func (f *fakeTranslateChat) Chat(_ context.Context, messages []chat.Message, _ *chat.ChatOptions) (*types.ChatResponse, error) {
|
|
f.prompts = append(f.prompts, messages[len(messages)-1].Content)
|
|
start := strings.IndexByte(messages[len(messages)-1].Content, '[')
|
|
end := strings.LastIndexByte(messages[len(messages)-1].Content, ']')
|
|
var texts []string
|
|
if err := json.Unmarshal([]byte(messages[len(messages)-1].Content[start:end+1]), &texts); err != nil {
|
|
return nil, err
|
|
}
|
|
out := make([]string, 0, len(texts))
|
|
for _, t := range texts {
|
|
out = append(out, replaceCJKRuns(t))
|
|
}
|
|
b, _ := json.Marshal(out)
|
|
return &types.ChatResponse{Content: string(b)}, nil
|
|
}
|
|
|
|
func (f *fakeTranslateChat) ChatStream(_ context.Context, _ []chat.Message, _ *chat.ChatOptions) (<-chan types.StreamResponse, error) {
|
|
return nil, fmt.Errorf("not implemented")
|
|
}
|
|
|
|
func (f *fakeTranslateChat) GetModelName() string { return "fake" }
|
|
func (f *fakeTranslateChat) GetModelID() string { return "fake-id" }
|
|
|
|
// replaceCJKRuns mirrors the engine-test stub: CJK runs become a single space.
|
|
func replaceCJKRuns(s string) string {
|
|
var b strings.Builder
|
|
inCJK := false
|
|
for _, r := range s {
|
|
if (r >= 0x3400 && r <= 0x9FFF) || (r >= 0xF900 && r <= 0xFAFF) ||
|
|
(r >= 0x3000 && r <= 0x303F) || (r >= 0xFF01 && r <= 0xFF5E) {
|
|
inCJK = true
|
|
continue
|
|
}
|
|
if inCJK {
|
|
b.WriteByte(' ')
|
|
inCJK = false
|
|
}
|
|
b.WriteRune(r)
|
|
}
|
|
if inCJK {
|
|
b.WriteByte(' ')
|
|
}
|
|
return b.String()
|
|
}
|
|
|
|
func buildToolFixture(t *testing.T) []byte {
|
|
t.Helper()
|
|
var buf bytes.Buffer
|
|
zw := zip.NewWriter(&buf)
|
|
for _, f := range []struct{ name, body string }{
|
|
{"[Content_Types].xml", "<Types/>"},
|
|
{"xl/sharedStrings.xml", `<sst xmlns="x"><si><t>螺母 M8</t></si><si><t>Assembly Process</t></si></sst>`},
|
|
} {
|
|
w, _ := zw.Create(f.name)
|
|
w.Write([]byte(f.body))
|
|
}
|
|
zw.Close()
|
|
return buf.Bytes()
|
|
}
|
|
|
|
func TestXlsxTranslateToolEndToEnd(t *testing.T) {
|
|
store := &fakeTranslateStore{files: map[string][]byte{
|
|
"/workspace/input/part.xlsx": buildToolFixture(t),
|
|
}}
|
|
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
|
|
|
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
|
|
result, err := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
|
if err != nil {
|
|
t.Fatalf("Execute: %v", err)
|
|
}
|
|
if !result.Success {
|
|
t.Fatalf("tool failed: %s", result.Error)
|
|
}
|
|
if store.writePath != "/workspace/output/part_EN.xlsx" {
|
|
t.Fatalf("unexpected output path: %s", store.writePath)
|
|
}
|
|
if len(result.OutputFiles) != 1 || !strings.Contains(result.OutputFiles[0], "part_EN.xlsx") {
|
|
t.Fatalf("missing output link: %v", result.OutputFiles)
|
|
}
|
|
if !strings.Contains(result.Output, "part_EN.xlsx") {
|
|
t.Fatalf("output does not name the deliverable: %s", result.Output)
|
|
}
|
|
|
|
// The translated workbook must contain no CJK and keep the EN string.
|
|
r, err := zip.NewReader(bytes.NewReader(store.files[store.writePath]), int64(len(store.files[store.writePath])))
|
|
if err != nil {
|
|
t.Fatalf("output zip: %v", err)
|
|
}
|
|
var ss string
|
|
for _, f := range r.File {
|
|
if f.Name == "xl/sharedStrings.xml" {
|
|
rc, _ := f.Open()
|
|
raw, _ := io.ReadAll(rc)
|
|
rc.Close()
|
|
ss = string(raw)
|
|
}
|
|
}
|
|
if strings.ContainsAny(ss, "螺母") {
|
|
t.Fatalf("output still contains Chinese: %s", ss)
|
|
}
|
|
if !strings.Contains(ss, "Assembly Process") {
|
|
t.Fatalf("English string lost: %s", ss)
|
|
}
|
|
}
|
|
|
|
func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
|
|
store := &fakeTranslateStore{files: map[string][]byte{
|
|
"/workspace/input/part.xls": {0xD0, 0xCF, 0x11, 0xE0},
|
|
}}
|
|
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
|
|
|
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xls"})
|
|
result, _ := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
|
if result.Success {
|
|
t.Fatalf("legacy .xls should be rejected")
|
|
}
|
|
if !strings.Contains(result.Error, ".xlsx") {
|
|
t.Fatalf("unhelpful error: %s", result.Error)
|
|
}
|
|
}
|
|
|
|
func TestXlsxTranslateToolMissingFile(t *testing.T) {
|
|
store := &fakeTranslateStore{files: map[string][]byte{}}
|
|
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
|
|
|
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/missing.xlsx"})
|
|
result, _ := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
|
if result.Success {
|
|
t.Fatalf("missing file should fail")
|
|
}
|
|
}
|