Files
job-search/internal/cli/skiprules_test.go
T
bwilliams 0543762330 Added skip rules, which mark new opportunities Skipped when they're fetched
sources.skip_rules_file names a JSON file of filters, each with a
comment, an active flag, a logic mode ("or" by default, or "and") and
rules by buyer, title words or CPV codes. The fetcher applies them
before saving, so only new opportunities are marked; the marks of
stored ones are never touched. CPV rules match when the opportunity's
codes are all in the list, or any of them with exclusive false, and
by prefix with exact false.

The file is read again when its time or size changes: an invalid edit
is logged and the last good rules kept, and a deleted file means no
rules until it's back. fetcher service and serve refuse to start if it
is define but missing or invalid.

job-search skip-rules check validates a rules file, and skip-rules
apply marks the stored opportunities that match Skipped, or only lists
them with --dry-run.
2026-10-20 08:55:19 +01:00

44 lines
1.1 KiB
Go

package cli
import (
"os"
"path/filepath"
"strings"
"testing"
"git.avsdev.uk/Go/job-search/internal/config"
)
// SkipRulesOptions gives no option without a file, one with a valid file,
// and refuses a missing or invalid file, naming the setting.
func TestSkipRulesOptions(t *testing.T) {
dir := t.TempDir()
valid := filepath.Join(dir, "valid.json")
invalid := filepath.Join(dir, "invalid.json")
os.WriteFile(valid, []byte(`[]`), 0o600)
os.WriteFile(invalid, []byte(`[{}]`), 0o600)
tests := []struct {
file string
options int
err string
}{
{"", 0, ""},
{valid, 1, ""},
{filepath.Join(dir, "missing.json"), 0, "sources.skip_rules_file: "},
{invalid, 0, "sources.skip_rules_file: skip rules "},
}
for _, tt := range tests {
conf := &config.JobSearchConfig{}
conf.Sources.SkipRulesFile = tt.file
options, err := SkipRulesOptions(conf)
if len(options) != tt.options {
t.Errorf("%q: %d options; want %d", tt.file, len(options), tt.options)
}
if (err == nil) != (tt.err == "") ||
err != nil && !strings.Contains(err.Error(), tt.err) {
t.Errorf("%q: err %v; want %q", tt.file, err, tt.err)
}
}
}