返回 DeepSeek-Reasonix
preview_test.go
根目录 / internal / tool / preview_test.go
1 package tool
2
3 import (
4 "context"
5 "encoding/json"
6 "errors"
7 "testing"
8
9 "reasonix/internal/diff"
10 )
11
12 type fakeWriter struct {
13 readOnly bool
14 change diff.Change
15 err error
16 }
17
18 func (f fakeWriter) Name() string { return "fake" }
19 func (f fakeWriter) Description() string { return "fake" }
20 func (f fakeWriter) Schema() json.RawMessage { return json.RawMessage(`{}`) }
21 func (f fakeWriter) Execute(context.Context, json.RawMessage) (string, error) { return "", nil }
22 func (f fakeWriter) ReadOnly() bool { return f.readOnly }
23 func (f fakeWriter) Preview(context.Context, json.RawMessage) (diff.Change, error) {
24 return f.change, f.err
25 }
26
27 type plainWriter struct{}
28
29 func (plainWriter) Name() string { return "plain" }
30 func (plainWriter) Description() string { return "plain" }
31 func (plainWriter) Schema() json.RawMessage { return json.RawMessage(`{}`) }
32 func (plainWriter) Execute(context.Context, json.RawMessage) (string, error) { return "", nil }
33 func (plainWriter) ReadOnly() bool { return false }
34
35 func TestPreviewChange(t *testing.T) {
36 good := diff.Change{Diff: "@@\n+a\n", Added: 1}
37 cases := []struct {
38 name string
39 tool Tool
40 want bool
41 }{
42 {"nil tool", nil, false},
43 {"read-only skipped", fakeWriter{readOnly: true, change: good}, false},
44 {"writer without previewer", plainWriter{}, false},
45 {"preview error", fakeWriter{err: errors.New("boom")}, false},
46 {"binary skipped", fakeWriter{change: diff.Change{Binary: true}}, false},
47 {"textual change", fakeWriter{change: good}, true},
48 }
49 for _, c := range cases {
50 t.Run(c.name, func(t *testing.T) {
51 ch, ok := PreviewChange(context.Background(), c.tool, json.RawMessage(`{}`))
52 if ok != c.want {
53 t.Fatalf("ok = %v, want %v", ok, c.want)
54 }
55 if ok && ch.Diff == "" {
56 t.Fatal("expected a non-empty diff on success")
57 }
58 })
59 }
60 }
61
61 lines GO