// SPDX-License-Identifier: GPL-3.0-or-later package plan import ( "path/filepath" "strings" "testing" "time" "krino/internal/config" "krino/internal/scan" ) // FuzzExpand: expanding any template against any file name never panics, // gives the same answer twice, and never succeeds with a {N} beyond the // capture groups there are - MaxIndex and Expand read placeholders alike. // Used as a destination - relative, in the home directory, or bare - a // template never plans a move outside the directory its text names before // the first placeholder (review M1). func FuzzExpand(f *testing.F) { for _, s := range []string{"{name}", "{stem}{ext}", "{mtime:%Y/%m}", "{1}_{2}", "{{literal}}", "{", "}", "{now:%", "{0}", "{9}", "{99999999999999999999}"} { f.Add(s, "a.b.pdf") } f.Fuzz(func(t *testing.T, tmpl, name string) { facts := Facts{ Name: name, Captures: []string{name, "x", ".."}, ModTime: time.Date(2026, 8, 15, 12, 0, 0, 0, time.UTC), Now: time.Date(2026, 9, 14, 0, 0, 0, 0, time.UTC), } a, errA := Expand(tmpl, facts) b, errB := Expand(tmpl, facts) if a != b || (errA == nil) != (errB == nil) { t.Fatalf("Expand(%q) differs between two calls: %q/%v, %q/%v", tmpl, a, errA, b, errB) } if n, err := MaxIndex(tmpl); err == nil && n > 2 && errA == nil { t.Fatalf("Expand(%q) = %q, though it uses {%d} and only 2 groups exist", tmpl, a, n) } for _, dest := range []string{tmpl, "Out/" + tmpl, "~/docs/" + tmpl} { in := []Input{{ File: scan.File{Path: "/r/x.pdf", Rel: "x.pdf", Name: "x.pdf", ModTime: facts.ModTime}, Rules: []RuleMatch{{Name: "a", Captures: facts.Captures, Actions: []config.Action{{Kind: config.Move, Arg: dest}}}}, }} s := Build("/r", in, facts.Now, NoDisk{}, NewClaims())[0].Steps[0] if strings.ContainsRune(dest, '{') && s.Skip == "" && !within(filepath.Dir(s.Dst), staticDir(dest, "/r")) { t.Fatalf("destination %q planned %q, outside %q", dest, s.Dst, staticDir(dest, "/r")) } } }) }