mirror of
https://github.com/BaaRai/ARTEX.git
synced 2026-11-13 11:40:53 +08:00
A worker hitting a wall only a human can clear (captcha, email/SMS OTP, 2FA,
a decision) calls ask_operator with a precise question and an optional
screenshot. The question lands in the intent's transcript as a user-role turn
with an attachment card (the same {text, attachments} shape chat uploads use,
so the existing transcript UI renders it natively), and the intent pauses. The
operator replies through the existing message-to-worker flow; the reply resumes
the run with the answer as the next input. Images are whitelisted (png/jpg/
jpeg/webp/gif), capped at 10MB, and copied into the task uploads dir.
Closes #41
71 lines
2.3 KiB
Go
71 lines
2.3 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestStageAskOperatorImage(t *testing.T) {
|
|
dir := t.TempDir()
|
|
src := filepath.Join(dir, "shot.png")
|
|
if err := os.WriteFile(src, []byte("fakepng"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
|
|
// no image -> no attachment
|
|
att, err := stageAskOperatorImage(dir, "7", "")
|
|
if err != nil || att != nil {
|
|
t.Fatalf("empty src: att=%v err=%v", att, err)
|
|
}
|
|
|
|
// valid image -> copied under the task uploads, descriptor matches the chat format
|
|
att, err = stageAskOperatorImage(dir, "7", src)
|
|
if err != nil || len(att) != 1 {
|
|
t.Fatalf("valid src: att=%v err=%v", att, err)
|
|
}
|
|
if !strings.HasPrefix(att[0].Path, "uploads/ask-operator-") || !strings.HasSuffix(att[0].Path, ".png") {
|
|
t.Fatalf("unexpected attachment path %q", att[0].Path)
|
|
}
|
|
if _, err := os.Stat(filepath.Join(dir, "tasks", "7", "uploads", att[0].Name)); err != nil {
|
|
t.Fatalf("copy missing: %v", err)
|
|
}
|
|
|
|
// wrong extension rejected
|
|
bad := filepath.Join(dir, "payload.exe")
|
|
if err := os.WriteFile(bad, []byte("x"), 0o644); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := stageAskOperatorImage(dir, "7", bad); err == nil || !strings.Contains(err.Error(), "png/jpg") {
|
|
t.Fatalf("expected extension rejection, got %v", err)
|
|
}
|
|
|
|
// missing file rejected
|
|
if _, err := stageAskOperatorImage(dir, "7", filepath.Join(dir, "nope.png")); err == nil {
|
|
t.Fatal("expected missing-file rejection")
|
|
}
|
|
}
|
|
|
|
// The emitted activity must carry the {text, attachments} Detail shape the
|
|
// transcript parser expects, so the operator sees the question with the
|
|
// attachment card for the screenshot.
|
|
func TestAskOperatorActivityShape(t *testing.T) {
|
|
att := []chatAttachment{{Name: "ask-operator-1.png", Path: "uploads/ask-operator-1.png", Size: 7}}
|
|
a := userActivityWithAttachments("worker", "OPERATOR INPUT REQUESTED: solve the captcha", att)
|
|
if a.Kind != "user" || a.Worker != "worker" {
|
|
t.Fatalf("unexpected activity kind/worker: %+v", a)
|
|
}
|
|
var parsed struct {
|
|
Text string `json:"text"`
|
|
Attachments []chatAttachment `json:"attachments"`
|
|
}
|
|
if err := json.Unmarshal([]byte(a.Detail), &parsed); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.HasPrefix(parsed.Text, "OPERATOR INPUT REQUESTED") || len(parsed.Attachments) != 1 || parsed.Attachments[0].Path != att[0].Path {
|
|
t.Fatalf("unexpected detail payload: %+v", parsed)
|
|
}
|
|
}
|