1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
|
// SPDX-License-Identifier: GPL-3.0-or-later
package cond
import (
"errors"
"math/rand"
"reflect"
"strings"
"testing"
"time"
"git.labunix.xyz/krino/internal/norm"
)
var now = time.Date(2026, 9, 11, 12, 0, 0, 0, time.UTC)
type fake struct {
name, rel, raw string
rawErr error
size int64
age time.Duration
matched bool
matchedUnknown bool
dupOrig string
dupOK bool
dupErr error
contentCalls int
}
func (f *fake) Name() string { return f.name }
func (f *fake) Rel() string {
if f.rel != "" {
return f.rel
}
return f.name
}
func (f *fake) Size() int64 { return f.size }
func (f *fake) ModTime() time.Time { return now.Add(-f.age) }
func (f *fake) Now() time.Time { return now }
func (f *fake) Matched() (bool, bool) { return f.matched, f.matchedUnknown }
func (f *fake) ContentContains(opt Options, keywords []string) (int, error) {
f.contentCalls++
if f.rawErr != nil {
return -1, f.rawErr
}
text := norm.Text(f.raw, opt.IgnoreCase, opt.Fold)
for i, kw := range keywords {
if strings.Contains(text, kw) {
return i, nil
}
}
return -1, nil
}
func (f *fake) Duplicate(dirs []string) (string, bool, error) { return f.dupOrig, f.dupOK, f.dupErr }
func eval(t *testing.T, src string, opt Options, f Facts) Result {
t.Helper()
c, errs := Compile("d.conf", nodes(t, src), opt)
if len(errs) > 0 {
t.Fatalf("%s: %v", src, errs)
}
return c.Eval(f)
}
func TestTruth(t *testing.T) {
ic := Options{IgnoreCase: true}
pdf := &fake{name: "Scan001.PDF", raw: "Invoice from ACME LTD", size: 20 << 20, age: 40 * 24 * time.Hour}
tests := []struct {
src string
want bool
}{
{``, true},
{`(type pdf)`, true},
{`(type document)`, true},
{`(type jpg)`, false},
{`(type pdf) (size > 10M)`, true},
{`(type pdf) (size < 10M)`, false},
{`(or (type jpg) (content "acme ltd"))`, true},
{`(not (content "acme ltd"))`, false},
{`(age > 30d) (age <= 41d)`, true},
{`(name "^scan\d+")`, true},
{`(name "(?-i)^scan")`, false},
{`(matched)`, false},
{`(and (type pdf) (or (name "^x") (not (name "^y"))))`, true},
}
for _, tt := range tests {
if got := eval(t, tt.src, ic, pdf); got.Match != tt.want {
t.Errorf("%s = %v, want %v (%+v)", tt.src, got.Match, tt.want, got)
}
}
}
func TestCaseAndFold(t *testing.T) {
f := &fake{name: "SPOLKA-umowa.pdf", raw: "Umowa: spółka z o.o."}
if !eval(t, `(name "spółka")`, Options{IgnoreCase: true, Fold: true}, f).Match {
t.Error("folded, case-ignoring name did not match")
}
if eval(t, `(name "spółka")`, Options{IgnoreCase: true, Fold: false}, f).Match {
t.Error("matched without folding")
}
if !eval(t, `(content "SPOLKA Z O.O.")`, Options{IgnoreCase: true, Fold: true}, f).Match {
t.Error("folded content did not match")
}
img := &fake{name: "IMG_0001.jpg"}
if eval(t, `(name "^img")`, Options{IgnoreCase: false}, img).Match {
t.Error("strict case matched")
}
if !eval(t, `(name "(?i)^img")`, Options{IgnoreCase: false}, img).Match {
t.Error("inline (?i) did not override strict case")
}
}
func TestReasonsAndCaptures(t *testing.T) {
f := &fake{name: "Screenshot_20260911.png", raw: "acme ltd"}
r := eval(t, `(type image) (name "^Screenshot_(\d{4})(\d{2})") (not (name "^x(y)"))`, Options{IgnoreCase: true}, f)
if !r.Match {
t.Fatal("no match")
}
if want := []string{"Screenshot_202609", "2026", "09"}; !reflect.DeepEqual(r.Captures, want) {
t.Errorf("captures = %q, want %q", r.Captures, want)
}
if want := []string{`type png`, `name "^Screenshot_(\d{4})(\d{2})"`, `not name "^x(y)"`}; !reflect.DeepEqual(r.Reasons, want) {
t.Errorf("reasons = %q, want %q", r.Reasons, want)
}
r = eval(t, `(or (content "nope" "acme ltd") (type png))`, Options{IgnoreCase: true}, f)
if want := []string{`type png`}; !reflect.DeepEqual(r.Reasons, want) {
t.Errorf("or reasons = %q, want %q (cheapest true child)", r.Reasons, want)
}
r = eval(t, `(content "nope" "acme ltd")`, Options{IgnoreCase: true}, f)
if want := []string{`content "acme ltd"`}; !reflect.DeepEqual(r.Reasons, want) {
t.Errorf("content reasons = %q, want %q", r.Reasons, want)
}
if r := eval(t, ``, Options{}, f); !reflect.DeepEqual(r.Reasons, []string{"no condition"}) {
t.Errorf("empty reasons = %q", r.Reasons)
}
d := &fake{name: "report (1).pdf", dupOrig: "report.pdf", dupOK: true}
if r := eval(t, `(duplicate)`, Options{}, d); !r.Match || r.Reasons[0] != "duplicate of report.pdf" {
t.Errorf("duplicate = %+v", r)
}
}
func TestCheapFirstAvoidsContent(t *testing.T) {
f := &fake{name: "a.txt", raw: "x"}
eval(t, `(and (content "x") (type pdf))`, Options{}, f)
if f.contentCalls != 0 {
t.Errorf("content read although type was false (%d calls)", f.contentCalls)
}
g := &fake{name: "a.pdf", raw: "x"}
eval(t, `(or (content "x") (type pdf))`, Options{}, g)
if g.contentCalls != 0 {
t.Errorf("content read although type was true (%d calls)", g.contentCalls)
}
}
func TestContentErrorWarns(t *testing.T) {
f := &fake{name: "a.pdf", rawErr: errors.New("needs pdftotext, not installed")}
r := eval(t, `(or (content "acme") (content "other"))`, Options{}, f)
if r.Match {
t.Fatal("matched unreadable content")
}
if want := []string{"content unreadable: needs pdftotext, not installed"}; !reflect.DeepEqual(r.Warnings, want) {
t.Errorf("warnings = %q, want %q (de-duplicated)", r.Warnings, want)
}
}
func TestExplainFormat(t *testing.T) {
f := &fake{name: "scan.pdf", rawErr: errors.New("needs pdftotext, not installed")}
c, _ := Compile("d.conf", nodes(t, `(type pdf) (or (content "acme ltd") (name "\bacme\b"))`), Options{IgnoreCase: true})
var b strings.Builder
c.Explain(f).Format(&b)
want := "? and\n" +
"yes type pdf\n" +
"? or\n" +
"no name \"\\bacme\\b\"\n" +
"? content \"acme ltd\" (content unreadable: needs pdftotext, not installed)\n"
if b.String() != want {
t.Fatalf("got\n%s\nwant\n%s", b.String(), want)
}
}
// TestReorderKeepsMeaning: random trees give the same answer with and without
// cost reordering.
func TestReorderKeepsMeaning(t *testing.T) {
leaves := []string{`(type pdf)`, `(type txt)`, `(name "^a")`, `(name "b$")`, `(size > 10)`, `(size < 5)`,
`(age > 1d)`, `(content "x")`, `(content "y")`, `(matched)`}
rng := rand.New(rand.NewSource(1))
var gen func(depth int) string
gen = func(depth int) string {
if depth == 0 || rng.Intn(3) == 0 {
return leaves[rng.Intn(len(leaves))]
}
switch rng.Intn(3) {
case 0:
return "(not " + gen(depth-1) + ")"
case 1:
return "(and " + gen(depth-1) + " " + gen(depth-1) + ")"
default:
return "(or " + gen(depth-1) + " " + gen(depth-1) + " " + gen(depth-1) + ")"
}
}
names := []string{"a.pdf", "b.txt", "ab.pdf", "c.jpg"}
for i := 0; i < 500; i++ {
src := gen(4)
f := &fake{name: names[rng.Intn(len(names))], raw: []string{"x", "y", "xy", ""}[rng.Intn(4)],
size: int64(rng.Intn(20)), age: time.Duration(rng.Intn(72)) * time.Hour, matched: rng.Intn(2) == 0}
a, _ := compile("d.conf", nodes(t, src), Options{}, true)
b, _ := compile("d.conf", nodes(t, src), Options{}, false)
if ra, rb := a.Eval(f), b.Eval(f); ra.Match != rb.Match {
t.Fatalf("%s on %+v: reordered %v, original %v", src, f, ra.Match, rb.Match)
}
}
}
// TestNegatedCombinatorReason: E4. Negating a combinator must read as
// something a person would write ("not (and ...)" / "not (or ...)"), not
// the bare "not and" / "not or"; a negated leaf keeps its own label
// unchanged ("not matched").
func TestNegatedCombinatorReason(t *testing.T) {
f := &fake{name: "a.pdf"}
if r := eval(t, `(not (and (type pdf) (matched)))`, Options{}, f); !r.Match || r.Reasons[0] != "not (and ...)" {
t.Errorf("negated and = %+v, want reason %q", r, "not (and ...)")
}
if r := eval(t, `(not (or (matched) (name "^zzz")))`, Options{}, f); !r.Match || r.Reasons[0] != "not (or ...)" {
t.Errorf("negated or = %+v, want reason %q", r, "not (or ...)")
}
if r := eval(t, `(not (matched))`, Options{}, f); !r.Match || r.Reasons[0] != "not matched" {
t.Errorf("negated leaf = %+v, want its own label unchanged: %q", r, "not matched")
}
}
// TestEvalReportsUnreadableContent: Result.Unreadable says a content test
// was reached and could not read the file - what lets an exclude fail
// closed - and stays false when evaluation never reached the content
// test.
func TestEvalReportsUnreadableContent(t *testing.T) {
f := &fake{name: "a.pdf", rawErr: errors.New("larger than max-read")}
if r := eval(t, `(and (type pdf) (content "x"))`, Options{}, f); r.Match || !r.Unreadable {
t.Errorf("reached: Match %v Unreadable %v; want false, true", r.Match, r.Unreadable)
}
if r := eval(t, `(or (name "^a") (content "x"))`, Options{}, f); !r.Match || r.Unreadable {
t.Errorf("not reached: Match %v Unreadable %v; want true, false", r.Match, r.Unreadable)
}
}
// TestCapturesKeepDiacritics: a name test matches the folded name, but its
// captures are the original name's characters, so {1} keeps "Łódź".
func TestCapturesKeepDiacritics(t *testing.T) {
f := &fake{name: "Łódź-Faktura.pdf"}
r := eval(t, `(name "^(.+)-faktura")`, Options{IgnoreCase: true, Fold: true}, f)
if !r.Match {
t.Fatal("no match")
}
if want := []string{"Łódź-Faktura", "Łódź"}; !reflect.DeepEqual(r.Captures, want) {
t.Errorf("captures = %q, want %q", r.Captures, want)
}
r = eval(t, `(name "^(lodz)?(x)?-")`, Options{IgnoreCase: true, Fold: true}, f)
if want := []string{"Łódź-", "Łódź", ""}; !reflect.DeepEqual(r.Captures, want) {
t.Errorf("captures with a group that did not take part = %q, want %q", r.Captures, want)
}
}
// TestUnreadableContentIsUnknown: a content test that cannot read the file
// is unknown, not false, and and/or/not combine unknowns the way Kleene's
// three-valued logic does: a condition certainly false (or true) whatever
// the text holds is decided, and only one that depends on the text is
// unknown. A rule matches only a true condition; an exclude holds on true
// or unknown.
func TestUnreadableContentIsUnknown(t *testing.T) {
f := &fake{name: "a.pdf", rawErr: errors.New("larger than max-read")}
cases := []struct {
src string
match, unreadable bool
}{
{`(and (content "x") (type txt))`, false, false},
{`(or (content "x") (type pdf))`, true, false},
{`(not (content "x"))`, false, true},
{`(and (type pdf) (content "x"))`, false, true},
{`(or (type txt) (content "x"))`, false, true},
{`(content "secret") (and (content "other") (type txt))`, false, false},
{`(not (and (content "x") (type txt)))`, true, false},
}
for _, c := range cases {
if r := eval(t, c.src, Options{}, f); r.Match != c.match || r.Unreadable != c.unreadable {
t.Errorf("%s: Match %v Unreadable %v; want %v, %v", c.src, r.Match, r.Unreadable, c.match, c.unreadable)
}
}
}
// TestMatchedIsUnknownAfterAnUnknownRule: when no earlier rule matched but
// one could not be decided, (matched) is unknown, so a later
// (not (matched)) does not act on a file krino could not read.
func TestMatchedIsUnknownAfterAnUnknownRule(t *testing.T) {
f := &fake{name: "a.docx", matchedUnknown: true}
if r := eval(t, `(not (matched))`, Options{}, f); r.Match || !r.Unreadable {
t.Errorf("(not (matched)): Match %v Unreadable %v; want false, true", r.Match, r.Unreadable)
}
f = &fake{name: "a.docx", matched: true, matchedUnknown: true}
if r := eval(t, `(matched)`, Options{}, f); !r.Match {
t.Errorf("(matched) after a rule that did match: %+v, want true", r)
}
}
// TestFailedDuplicateLookupIsUnknown: a duplicate test whose lookup fails is
// unknown, like unreadable content, and says why.
func TestFailedDuplicateLookupIsUnknown(t *testing.T) {
f := &fake{name: "a.pdf", dupErr: errors.New("~/dl/a.pdf: open: permission denied")}
for _, c := range []struct {
src string
match, unreadable bool
}{
{`(duplicate)`, false, true},
{`(not (duplicate))`, false, true},
{`(and (duplicate) (type txt))`, false, false},
} {
r := eval(t, c.src, Options{}, f)
if r.Match != c.match || r.Unreadable != c.unreadable {
t.Errorf("%s: Match %v Unreadable %v; want %v, %v", c.src, r.Match, r.Unreadable, c.match, c.unreadable)
}
if c.unreadable && r.Undecided != "duplicate check failed" {
t.Errorf("%s: Undecided %q", c.src, r.Undecided)
}
}
g := &fake{name: "a.pdf", rawErr: errors.New("larger than max-read"), dupErr: errors.New("x")}
if r := eval(t, `(or (content "x") (duplicate))`, Options{}, g); r.Undecided != "content unreadable, duplicate check failed" {
t.Errorf("both unknown: Undecided %q", r.Undecided)
}
}
|