This commit is contained in:
1 parent
db8472bf28
commit
c6088aeb4b
4 files changed
+334
-126
No files matched your search
@@ -13,6 +13,7 @@ import (
|
||||
"academy/internal/models/chat"
|
||||
"academy/internal/sandbox"
|
||||
"academy/internal/types"
|
||||
"academy/internal/types/interfaces"
|
||||
)
|
||||
|
||||
// fakeTranslateStore is an in-memory SessionFileSink + dir ensurer.
|
||||
@@ -44,6 +45,45 @@ func (s *fakeTranslateStore) WriteSessionWorkspaceFile(_ context.Context, _, pat
|
||||
return nil
|
||||
}
|
||||
|
||||
// fakeTranslateTempDoc is an in-memory TemporaryDocumentService covering the
|
||||
// surface xlsx_translate uses. Unimplemented methods panic via the embedded
|
||||
// nil interface — the tool must not call them.
|
||||
type fakeTranslateTempDoc struct {
|
||||
interfaces.TemporaryDocumentService
|
||||
docs []*types.TemporaryDocument
|
||||
created []*types.TemporaryDocument
|
||||
createdBytes map[string][]byte
|
||||
openBytes map[string][]byte
|
||||
}
|
||||
|
||||
func (s *fakeTranslateTempDoc) List(_ context.Context, _ uint64, _ string) ([]*types.TemporaryDocument, error) {
|
||||
return s.docs, nil
|
||||
}
|
||||
|
||||
func (s *fakeTranslateTempDoc) OpenFile(_ context.Context, _ uint64, _ string, documentID string) (io.ReadCloser, string, error) {
|
||||
if data, ok := s.openBytes[documentID]; ok {
|
||||
return io.NopCloser(bytes.NewReader(data)), "part.xlsx", nil
|
||||
}
|
||||
return nil, "", fmt.Errorf("no such document: %s", documentID)
|
||||
}
|
||||
|
||||
func (s *fakeTranslateTempDoc) Create(_ context.Context, _ uint64, _ string, fileName, _ string, fileSize int64, reader io.Reader, _ types.TemporaryDocumentCreateOptions) (*types.TemporaryDocument, error) {
|
||||
data, err := io.ReadAll(reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if int64(len(data)) != fileSize {
|
||||
return nil, fmt.Errorf("size mismatch")
|
||||
}
|
||||
doc := &types.TemporaryDocument{ID: fmt.Sprintf("doc-%d", len(s.created)+1), FileName: fileName, FileSize: fileSize}
|
||||
if s.createdBytes == nil {
|
||||
s.createdBytes = map[string][]byte{}
|
||||
}
|
||||
s.createdBytes[doc.ID] = data
|
||||
s.created = append(s.created, doc)
|
||||
return doc, nil
|
||||
}
|
||||
|
||||
// fakeTranslateChat answers every batch with the requested array translated
|
||||
// deterministically: CJK runs become " EN ".
|
||||
type fakeTranslateChat struct {
|
||||
@@ -52,10 +92,11 @@ type fakeTranslateChat struct {
|
||||
|
||||
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, ']')
|
||||
content := messages[len(messages)-1].Content
|
||||
start := strings.IndexByte(content, '[')
|
||||
end := strings.LastIndexByte(content, ']')
|
||||
var texts []string
|
||||
if err := json.Unmarshal([]byte(messages[len(messages)-1].Content[start:end+1]), &texts); err != nil {
|
||||
if err := json.Unmarshal([]byte(content[start:end+1]), &texts); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
out := make([]string, 0, len(texts))
|
||||
@@ -110,14 +151,36 @@ func buildToolFixture(t *testing.T) []byte {
|
||||
return buf.Bytes()
|
||||
}
|
||||
|
||||
func TestXlsxTranslateToolEndToEnd(t *testing.T) {
|
||||
func toolExecContext(sessionID string) context.Context {
|
||||
return WithToolExecContext(context.Background(), &ToolExecContext{SessionID: sessionID})
|
||||
}
|
||||
|
||||
func sharedStringsFrom(t *testing.T, data []byte) string {
|
||||
t.Helper()
|
||||
r, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
|
||||
if err != nil {
|
||||
t.Fatalf("output zip: %v", err)
|
||||
}
|
||||
for _, f := range r.File {
|
||||
if f.Name == "xl/sharedStrings.xml" {
|
||||
rc, _ := f.Open()
|
||||
raw, _ := io.ReadAll(rc)
|
||||
rc.Close()
|
||||
return string(raw)
|
||||
}
|
||||
}
|
||||
t.Fatalf("no sharedStrings.xml in output")
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestXlsxTranslateToolEndToEndSandbox(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{
|
||||
"/workspace/input/part.xlsx": buildToolFixture(t),
|
||||
}}
|
||||
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, nil, &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
|
||||
result, err := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
||||
result, err := tool.Execute(toolExecContext("sess-1"), args)
|
||||
if err != nil {
|
||||
t.Fatalf("Execute: %v", err)
|
||||
}
|
||||
@@ -134,20 +197,7 @@ func TestXlsxTranslateToolEndToEnd(t *testing.T) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
ss := sharedStringsFrom(t, store.files[store.writePath])
|
||||
if strings.ContainsAny(ss, "螺母") {
|
||||
t.Fatalf("output still contains Chinese: %s", ss)
|
||||
}
|
||||
@@ -156,14 +206,78 @@ func TestXlsxTranslateToolEndToEnd(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// TestXlsxTranslateToolWithoutSandbox covers the 192 deployment failure mode:
|
||||
// no workspace sandbox anywhere in the session, so input comes from the
|
||||
// uploaded session attachment and the deliverable is a preview URL.
|
||||
func TestXlsxTranslateToolWithoutSandbox(t *testing.T) {
|
||||
tempDoc := &fakeTranslateTempDoc{
|
||||
openBytes: map[string][]byte{"doc-1": buildToolFixture(t)},
|
||||
}
|
||||
tempDoc.docs = append(tempDoc.docs, &types.TemporaryDocument{
|
||||
ID: "doc-1", FileName: "D4-A08AR12000002-A3-SIP.xlsx", FileType: "xlsx",
|
||||
})
|
||||
tool := NewXlsxTranslateTool(nil, nil, tempDoc, &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{AttachmentName: "D4-A08AR12000002-A3-SIP.xlsx"})
|
||||
result, err := tool.Execute(toolExecContext("sess-1"), args)
|
||||
if err != nil {
|
||||
t.Fatalf("Execute: %v", err)
|
||||
}
|
||||
if !result.Success {
|
||||
t.Fatalf("tool failed: %s", result.Error)
|
||||
}
|
||||
if len(tempDoc.created) != 1 {
|
||||
t.Fatalf("expected one delivered attachment, got %d", len(tempDoc.created))
|
||||
}
|
||||
created := tempDoc.created[0]
|
||||
if created.FileName != "D4-A08AR12000002-A3-SIP_EN.xlsx" {
|
||||
t.Fatalf("unexpected deliverable name: %s", created.FileName)
|
||||
}
|
||||
wantURL := "/api/v1/sessions/sess-1/attachments/" + created.ID + "/preview"
|
||||
if !strings.Contains(result.Output, wantURL) {
|
||||
t.Fatalf("output missing preview URL %s: %s", wantURL, result.Output)
|
||||
}
|
||||
ss := sharedStringsFrom(t, tempDoc.createdBytes[created.ID])
|
||||
if strings.ContainsAny(ss, "螺母") {
|
||||
t.Fatalf("delivered workbook still contains Chinese: %s", ss)
|
||||
}
|
||||
}
|
||||
|
||||
// TestXlsxTranslateToolSandboxReadFallsBackToAttachment covers a mixed
|
||||
// session: a sandbox exists but the named file is not staged there, so the
|
||||
// upload is read from the attachment service while the deliverable still
|
||||
// goes to the sandbox output directory.
|
||||
func TestXlsxTranslateToolSandboxReadFallsBackToAttachment(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{}}
|
||||
tempDoc := &fakeTranslateTempDoc{
|
||||
openBytes: map[string][]byte{"doc-1": buildToolFixture(t)},
|
||||
}
|
||||
tempDoc.docs = append(tempDoc.docs, &types.TemporaryDocument{
|
||||
ID: "doc-1", FileName: "part.xlsx", FileType: "xlsx",
|
||||
})
|
||||
tool := NewXlsxTranslateTool(store, store, tempDoc, &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xlsx"})
|
||||
result, err := tool.Execute(toolExecContext("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)
|
||||
}
|
||||
}
|
||||
|
||||
func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{
|
||||
"/workspace/input/part.xls": {0xD0, 0xCF, 0x11, 0xE0},
|
||||
}}
|
||||
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
||||
tool := NewXlsxTranslateTool(store, store, nil, &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/part.xls"})
|
||||
result, _ := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
||||
result, _ := tool.Execute(toolExecContext("sess-1"), args)
|
||||
if result.Success {
|
||||
t.Fatalf("legacy .xls should be rejected")
|
||||
}
|
||||
@@ -174,10 +288,11 @@ func TestXlsxTranslateToolRejectsNonXlsx(t *testing.T) {
|
||||
|
||||
func TestXlsxTranslateToolMissingFile(t *testing.T) {
|
||||
store := &fakeTranslateStore{files: map[string][]byte{}}
|
||||
tool := NewXlsxTranslateTool(store, store, &fakeTranslateChat{})
|
||||
tempDoc := &fakeTranslateTempDoc{}
|
||||
tool := NewXlsxTranslateTool(store, store, tempDoc, &fakeTranslateChat{})
|
||||
|
||||
args, _ := json.Marshal(XlsxTranslateInput{FilePath: "/workspace/input/missing.xlsx"})
|
||||
result, _ := tool.Execute(WithToolExecContext(context.Background(), &ToolExecContext{SessionID: "sess-1"}), args)
|
||||
result, _ := tool.Execute(toolExecContext("sess-1"), args)
|
||||
if result.Success {
|
||||
t.Fatalf("missing file should fail")
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user