| 1 | package control |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "reasonix/internal/agent" |
| 8 | "reasonix/internal/event" |
| 9 | "reasonix/internal/provider" |
| 10 | ) |
| 11 | |
| 12 | type visionSummaryTestProvider struct{} |
| 13 | |
| 14 | func (visionSummaryTestProvider) Name() string { return "vision-test" } |
| 15 | |
| 16 | func (visionSummaryTestProvider) Stream(ctx context.Context, _ provider.Request) (<-chan provider.Chunk, error) { |
| 17 | out := make(chan provider.Chunk, 2) |
| 18 | go func() { |
| 19 | defer close(out) |
| 20 | select { |
| 21 | case out <- provider.Chunk{Type: provider.ChunkText, Text: "a chart with OCR: Revenue 42"}: |
| 22 | case <-ctx.Done(): |
| 23 | return |
| 24 | } |
| 25 | out <- provider.Chunk{Type: provider.ChunkDone} |
| 26 | }() |
| 27 | return out, nil |
| 28 | } |
| 29 | |
| 30 | func TestPrepareVisionTurnSummarizesTextOnlyInputAndKeepsRawPrompt(t *testing.T) { |
| 31 | c := &Controller{ |
| 32 | selection: modelSelection{ref: "text/text-model"}, |
| 33 | visionModel: "vision/vision-model", |
| 34 | visionProviderResolver: func(string) (provider.Provider, error) { |
| 35 | return visionSummaryTestProvider{}, nil |
| 36 | }, |
| 37 | sink: event.Discard, |
| 38 | } |
| 39 | ctx := context.Background() |
| 40 | got, ctx, err := c.prepareVisionTurn(ctx, "请看这张图", []string{"data:image/png;base64,AA=="}) |
| 41 | if err != nil { |
| 42 | t.Fatalf("prepareVisionTurn: %v", err) |
| 43 | } |
| 44 | if got == "请看这张图" || !contains(got, "<reasonix-image-context") || !contains(got, "Revenue 42") { |
| 45 | t.Fatalf("prepared input = %q", got) |
| 46 | } |
| 47 | if summary := agent.VisionSummaryFromContext(ctx); summary == nil || summary.ModelRef != "vision/vision-model" { |
| 48 | t.Fatalf("summary context = %+v", summary) |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func TestCachedVisionSummaryIsReused(t *testing.T) { |
| 53 | session := agent.NewSession("system") |
| 54 | calls := 0 |
| 55 | c := &Controller{ |
| 56 | selection: modelSelection{ref: "text/text-model"}, |
| 57 | visionModel: "vision/vision-model", |
| 58 | executor: agent.New(nil, nil, session, agent.Options{}, event.Discard), |
| 59 | visionProviderResolver: func(string) (provider.Provider, error) { |
| 60 | calls++ |
| 61 | return visionSummaryTestProvider{}, nil |
| 62 | }, |
| 63 | sink: event.Discard, |
| 64 | } |
| 65 | images := []string{"data:image/png;base64,AA=="} |
| 66 | first, ctx, err := c.prepareVisionTurn(context.Background(), "first", images) |
| 67 | if err != nil { |
| 68 | t.Fatalf("first prepare: %v", err) |
| 69 | } |
| 70 | summary := agent.VisionSummaryFromContext(ctx) |
| 71 | session.Add(provider.Message{Role: provider.RoleUser, Content: first, RawContent: "first", VisionSummary: summary}) |
| 72 | second, _, err := c.prepareVisionTurn(context.Background(), "follow-up", images) |
| 73 | if err != nil { |
| 74 | t.Fatalf("second prepare: %v", err) |
| 75 | } |
| 76 | if calls != 1 || !contains(second, "Revenue 42") { |
| 77 | t.Fatalf("calls=%d second=%q", calls, second) |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | func contains(s, sub string) bool { |
| 82 | for i := 0; i+len(sub) <= len(s); i++ { |
| 83 | if s[i:i+len(sub)] == sub { |
| 84 | return true |
| 85 | } |
| 86 | } |
| 87 | return false |
| 88 | } |
| 89 |