* refactor(timeparsing): export IsCompactDuration The query lexer keeps its own copy of the compact-duration unit set, and that copy has drifted from the grammar: it accepts the uppercase units H D W M Y, which ParseCompactDuration rejects. Export the classifier so the lexer can ask timeparsing instead of keeping a copy, and pin that it agrees with ParseCompactDuration on every row, uppercase spellings included. AC: AC-1 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(query): classify durations with timeparsing.IsCompactDuration The query lexer kept its own copy of the compact-duration unit set: isDurationSuffix for single runes and durationWords for "min". The copy had drifted. It accepted H D W M Y, which the case-sensitive grammar rejects, so "created>30M" reached the evaluator as a duration and failed with the internal error `not a compact duration: "-30M"`, quoting a value the user never typed. Classify the whole digit-led lexeme with timeparsing.IsCompactDuration and delete the copy (isDurationSuffix, durationWords, acceptDurationWord). Uppercase units now lex as identifiers and fail as an unparseable time; every lowercase input lexes as before. A unit added to compactDurationRe now reaches the lexer with no edit here: with "sec" added to the regex alone, TestCompactDurationGrammarReachesQueryPlane fails on the old lexer and passes on this one. Retire that test's text naming the deleted copy. AC: AC-2 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(query): pin that uppercase duration units lex as identifiers "updated>30M", "updated>30H" and "label=30D" lex the value as one TokenIdent with the text as typed; "updated>30m" and "updated>+30d" still lex as TokenDuration. On the base lexer the three uppercase rows fail (typed DURATION). AC: AC-3 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(query): pin the error for an uppercase duration unit created>, updated<, closed>= and started<= compared to 30M, 30D, 30W, 30Y, 30H or 1H, alone or in "... OR status=open", fail with `cannot parse time expression: "<value as typed>"`, never with the internal `not a compact duration` error and never quoting the negated "-<value>". 30MIN and 30Min, which always took this path, are the controls. On the base lexer 30 of the 40 subtests fail; the 10 controls pass. AC: AC-4 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(query): bind the lexer's duration grammar to timeparsing both ways TestCompactDurationGrammarReachesQueryPlane asserted only that every suffix ParseCompactDuration accepts lexes as a duration. Assert the reverse too: a suffix it rejects must not lex as TokenDuration, or the evaluator fails on it with ParseCompactDuration's internal error about a negated value. Widen the candidates to every one- and two-letter ASCII suffix in either case, every three-letter lowercase one, and every case spelling of an accepted three-letter unit. On the base lexer the reverse sweep fails on exactly [D H M W Y]. A mutant that case-folds before classifying fails it on those plus the seven other spellings of "min". AC: AC-5 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(query): pin that a signed uppercase unit stops at the parser "created>+30M" and "created>-30M" fail Parse and EvaluateAt with `unexpected token "M" at position 11 (expected end of query)`, as "created>+30x" already does: a signed digit run has no identifier fallback, so "+30" is a number and "M" a stray token. On the base lexer both parsed as a duration and failed in the evaluator with `not a compact duration: "-30M"` and `"--30M"`; the +30x control passes on both. AC: AC-6 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * test(query): pin the values rejecting uppercase units leaves alone At a fixed now, created>+30d is still 30 days ago, updated>5pm still 17:00 that day, and label=30M / title=30M still filter the literal "30M". TestQueryMinutesUnitResolvesAgo already pins created>30m (30 months ago) and created>30min (30 minutes ago). These rows pass on the base lexer as well, which is the point; a lexer mutant that stops typing signed lexemes as durations fails the +30d row. AC: AC-7 Agent-Signature: claude-code-claude-opus-5-5-unknown-reasoning on behalf of Julian Knutsen Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Eddie the Engineer <julianknutsen@users.noreply.github.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1312 lines
42 KiB
Go
1312 lines
42 KiB
Go
package query
|
|
|
|
import (
|
|
"fmt"
|
|
"strconv"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/steveyegge/beads/internal/storage"
|
|
"github.com/steveyegge/beads/internal/timeparsing"
|
|
"github.com/steveyegge/beads/internal/types"
|
|
)
|
|
|
|
func TestLexer(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected []TokenType
|
|
values []string
|
|
}{
|
|
{
|
|
name: "simple equality",
|
|
input: "status=open",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"status", "=", "open", ""},
|
|
},
|
|
{
|
|
name: "not equals",
|
|
input: "status!=closed",
|
|
expected: []TokenType{TokenIdent, TokenNotEquals, TokenIdent, TokenEOF},
|
|
values: []string{"status", "!=", "closed", ""},
|
|
},
|
|
{
|
|
name: "greater than",
|
|
input: "priority>1",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenNumber, TokenEOF},
|
|
values: []string{"priority", ">", "1", ""},
|
|
},
|
|
{
|
|
name: "less than or equal",
|
|
input: "priority<=3",
|
|
expected: []TokenType{TokenIdent, TokenLessEq, TokenNumber, TokenEOF},
|
|
values: []string{"priority", "<=", "3", ""},
|
|
},
|
|
{
|
|
name: "duration value",
|
|
input: "updated>7d",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenEOF},
|
|
values: []string{"updated", ">", "7d", ""},
|
|
},
|
|
{
|
|
name: "AND expression",
|
|
input: "status=open AND priority>1",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenAnd, TokenIdent, TokenGreater, TokenNumber, TokenEOF},
|
|
values: []string{"status", "=", "open", "AND", "priority", ">", "1", ""},
|
|
},
|
|
{
|
|
name: "OR expression",
|
|
input: "status=open OR status=blocked",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenOr, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"status", "=", "open", "OR", "status", "=", "blocked", ""},
|
|
},
|
|
{
|
|
name: "NOT expression",
|
|
input: "NOT status=closed",
|
|
expected: []TokenType{TokenNot, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"NOT", "status", "=", "closed", ""},
|
|
},
|
|
{
|
|
name: "parentheses",
|
|
input: "(status=open)",
|
|
expected: []TokenType{TokenLParen, TokenIdent, TokenEquals, TokenIdent, TokenRParen, TokenEOF},
|
|
values: []string{"(", "status", "=", "open", ")", ""},
|
|
},
|
|
{
|
|
name: "quoted string",
|
|
input: `title="hello world"`,
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenString, TokenEOF},
|
|
values: []string{"title", "=", "hello world", ""},
|
|
},
|
|
{
|
|
name: "compound expression with colon in label value",
|
|
input: "ephemeral=true AND label=gt:merge-request AND status=open",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenAnd, TokenIdent, TokenEquals, TokenIdent, TokenAnd, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"ephemeral", "=", "true", "AND", "label", "=", "gt:merge-request", "AND", "status", "=", "open", ""},
|
|
},
|
|
{
|
|
name: "case insensitive keywords",
|
|
input: "status=open and priority>1 or type=bug",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenAnd, TokenIdent, TokenGreater, TokenNumber, TokenOr, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
},
|
|
{
|
|
name: "negative number",
|
|
input: "priority>-1",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenNumber, TokenEOF},
|
|
values: []string{"priority", ">", "-1", ""},
|
|
},
|
|
{
|
|
name: "identifier with hyphen",
|
|
input: "id=bd-abc123",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"id", "=", "bd-abc123", ""},
|
|
},
|
|
{
|
|
name: "identifier with underscore",
|
|
input: "mol_type=swarm",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"mol_type", "=", "swarm", ""},
|
|
},
|
|
{
|
|
name: "quoted string with colon",
|
|
input: `label="gt:merge-request"`,
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenString, TokenEOF},
|
|
values: []string{"label", "=", "gt:merge-request", ""},
|
|
},
|
|
{
|
|
name: "unquoted identifier with colon",
|
|
input: "label=gt:merge-request",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "gt:merge-request", ""},
|
|
},
|
|
{
|
|
name: "digit-led identifier on the value side",
|
|
input: "assignee=1-alpha",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"assignee", "=", "1-alpha", ""},
|
|
},
|
|
{
|
|
name: "digit-led identifier in a compound expression",
|
|
input: "assignee=2-bravo AND status=open",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenAnd, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"assignee", "=", "2-bravo", "AND", "status", "=", "open", ""},
|
|
},
|
|
{
|
|
name: "digit-led identifier with duration-suffix prefix",
|
|
input: "label=7day-window",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "7day-window", ""},
|
|
},
|
|
{
|
|
name: "duration still wins when the suffix stands alone",
|
|
input: "updated>30d",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenEOF},
|
|
values: []string{"updated", ">", "30d", ""},
|
|
},
|
|
{
|
|
name: "duration with trailing space stays a duration",
|
|
input: "updated>30d AND status=open",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenAnd, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"updated", ">", "30d", "AND", "status", "=", "open", ""},
|
|
},
|
|
{
|
|
name: "multi-rune minutes suffix is a duration",
|
|
input: "updated>30min",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenEOF},
|
|
values: []string{"updated", ">", "30min", ""},
|
|
},
|
|
{
|
|
name: "multi-rune minutes suffix in a compound expression",
|
|
input: "updated>30min AND status=open",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenAnd, TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"updated", ">", "30min", "AND", "status", "=", "open", ""},
|
|
},
|
|
{
|
|
name: "plural minutes spelling stays an identifier",
|
|
input: "label=30mins",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "30mins", ""},
|
|
},
|
|
{
|
|
name: "minutes suffix continuing into an identifier stays an identifier",
|
|
input: "label=30min-window",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "30min-window", ""},
|
|
},
|
|
{
|
|
name: "uppercase minutes spelling stays an identifier",
|
|
input: "label=30MIN",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "30MIN", ""},
|
|
},
|
|
// Units are lowercase in the shared grammar, so an uppercase one is
|
|
// not a duration: it reaches the evaluator as typed and fails there as
|
|
// an unparseable time instead of as a negated compact duration.
|
|
{
|
|
name: "uppercase months unit stays an identifier",
|
|
input: "updated>30M",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenIdent, TokenEOF},
|
|
values: []string{"updated", ">", "30M", ""},
|
|
},
|
|
{
|
|
name: "uppercase hours unit stays an identifier",
|
|
input: "updated>30H",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenIdent, TokenEOF},
|
|
values: []string{"updated", ">", "30H", ""},
|
|
},
|
|
{
|
|
name: "uppercase days unit on a non-time field stays an identifier",
|
|
input: "label=30D",
|
|
expected: []TokenType{TokenIdent, TokenEquals, TokenIdent, TokenEOF},
|
|
values: []string{"label", "=", "30D", ""},
|
|
},
|
|
{
|
|
name: "lowercase months unit is a duration",
|
|
input: "updated>30m",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenEOF},
|
|
values: []string{"updated", ">", "30m", ""},
|
|
},
|
|
{
|
|
name: "signed lowercase days unit is a duration",
|
|
input: "updated>+30d",
|
|
expected: []TokenType{TokenIdent, TokenGreater, TokenDuration, TokenEOF},
|
|
values: []string{"updated", ">", "+30d", ""},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
lexer := NewLexer(tt.input)
|
|
tokens, err := lexer.Tokenize()
|
|
if err != nil {
|
|
t.Fatalf("Tokenize() error = %v", err)
|
|
}
|
|
|
|
if len(tokens) != len(tt.expected) {
|
|
t.Fatalf("got %d tokens, want %d", len(tokens), len(tt.expected))
|
|
}
|
|
|
|
for i, tok := range tokens {
|
|
if tok.Type != tt.expected[i] {
|
|
t.Errorf("token %d: got type %v, want %v", i, tok.Type, tt.expected[i])
|
|
}
|
|
if tt.values != nil && tok.Value != tt.values[i] {
|
|
t.Errorf("token %d: got value %q, want %q", i, tok.Value, tt.values[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestLexerErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
}{
|
|
{"unterminated string", `title="hello`},
|
|
{"invalid character", "status@open"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
lexer := NewLexer(tt.input)
|
|
_, err := lexer.Tokenize()
|
|
if err == nil {
|
|
t.Error("expected error, got nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParser(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
expected string
|
|
}{
|
|
{
|
|
name: "simple comparison",
|
|
input: "status=open",
|
|
expected: "status=open",
|
|
},
|
|
{
|
|
name: "AND expression",
|
|
input: "status=open AND priority>1",
|
|
expected: "(status=open AND priority>1)",
|
|
},
|
|
{
|
|
name: "OR expression",
|
|
input: "status=open OR status=blocked",
|
|
expected: "(status=open OR status=blocked)",
|
|
},
|
|
{
|
|
name: "NOT expression",
|
|
input: "NOT status=closed",
|
|
expected: "NOT status=closed",
|
|
},
|
|
{
|
|
name: "parentheses",
|
|
input: "(status=open OR status=blocked) AND priority<2",
|
|
expected: "((status=open OR status=blocked) AND priority<2)",
|
|
},
|
|
{
|
|
name: "chained AND",
|
|
input: "status=open AND priority>1 AND type=bug",
|
|
expected: "((status=open AND priority>1) AND type=bug)",
|
|
},
|
|
{
|
|
name: "AND has higher precedence than OR",
|
|
input: "status=open OR priority>1 AND type=bug",
|
|
expected: "(status=open OR (priority>1 AND type=bug))",
|
|
},
|
|
{
|
|
name: "NOT with parentheses",
|
|
input: "NOT (status=closed OR status=deferred)",
|
|
expected: "NOT (status=closed OR status=deferred)",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
node, err := Parse(tt.input)
|
|
if err != nil {
|
|
t.Fatalf("Parse() error = %v", err)
|
|
}
|
|
|
|
got := node.String()
|
|
if got != tt.expected {
|
|
t.Errorf("got %q, want %q", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParserErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
input string
|
|
}{
|
|
{"empty query", ""},
|
|
{"missing value", "status="},
|
|
{"missing operator", "status open"},
|
|
{"unclosed paren", "(status=open"},
|
|
{"extra paren", "status=open)"},
|
|
{"missing operand after AND", "status=open AND"},
|
|
{"invalid operator", "status~open"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := Parse(tt.input)
|
|
if err == nil {
|
|
t.Error("expected error, got nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvaluatorSimpleQueries(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
expectFilter func(*types.IssueFilter) bool
|
|
requiresPredicate bool
|
|
}{
|
|
{
|
|
name: "status equals",
|
|
query: "status=open",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Status != nil && *f.Status == types.StatusOpen
|
|
},
|
|
},
|
|
{
|
|
name: "status not equals",
|
|
query: "status!=closed",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.ExcludeStatus) == 1 && f.ExcludeStatus[0] == types.StatusClosed
|
|
},
|
|
},
|
|
{
|
|
name: "priority equals",
|
|
query: "priority=2",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Priority != nil && *f.Priority == 2
|
|
},
|
|
},
|
|
{
|
|
name: "priority greater than",
|
|
query: "priority>1",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.PriorityMin != nil && *f.PriorityMin == 2
|
|
},
|
|
},
|
|
{
|
|
name: "priority less than",
|
|
query: "priority<3",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.PriorityMax != nil && *f.PriorityMax == 2
|
|
},
|
|
},
|
|
{
|
|
name: "type equals",
|
|
query: "type=bug",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.IssueType != nil && *f.IssueType == types.TypeBug
|
|
},
|
|
},
|
|
{
|
|
name: "assignee equals",
|
|
query: "assignee=alice",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Assignee != nil && *f.Assignee == "alice"
|
|
},
|
|
},
|
|
{
|
|
name: "assignee none",
|
|
query: "assignee=none",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.NoAssignee
|
|
},
|
|
},
|
|
{
|
|
name: "label equals",
|
|
query: "label=urgent",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.Labels) == 1 && f.Labels[0] == "urgent"
|
|
},
|
|
},
|
|
{
|
|
name: "label with colon (quoted)",
|
|
query: `label="gt:merge-request"`,
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.Labels) == 1 && f.Labels[0] == "gt:merge-request"
|
|
},
|
|
},
|
|
{
|
|
name: "label with colon (unquoted)",
|
|
query: "label=gt:merge-request",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.Labels) == 1 && f.Labels[0] == "gt:merge-request"
|
|
},
|
|
},
|
|
{
|
|
name: "label none",
|
|
query: "label=none",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.NoLabels
|
|
},
|
|
},
|
|
{
|
|
name: "title contains",
|
|
query: "title=authentication",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.TitleContains == "authentication"
|
|
},
|
|
},
|
|
{
|
|
name: "description empty",
|
|
query: "description=none",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.EmptyDescription
|
|
},
|
|
},
|
|
{
|
|
name: "pinned equals true",
|
|
query: "pinned=true",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Pinned != nil && *f.Pinned == true
|
|
},
|
|
},
|
|
{
|
|
name: "updated greater than duration",
|
|
query: "updated>7d",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
// 7d ago from now
|
|
expected := now.AddDate(0, 0, -7)
|
|
return f.UpdatedAfter != nil && f.UpdatedAfter.Year() == expected.Year() &&
|
|
f.UpdatedAfter.Month() == expected.Month() && f.UpdatedAfter.Day() == expected.Day()
|
|
},
|
|
},
|
|
{
|
|
name: "created less than duration",
|
|
query: "created<30d",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
expected := now.AddDate(0, 0, -30)
|
|
return f.CreatedBefore != nil && f.CreatedBefore.Year() == expected.Year() &&
|
|
f.CreatedBefore.Month() == expected.Month() && f.CreatedBefore.Day() == expected.Day()
|
|
},
|
|
},
|
|
{
|
|
name: "AND expression",
|
|
query: "status=open AND priority>1",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Status != nil && *f.Status == types.StatusOpen &&
|
|
f.PriorityMin != nil && *f.PriorityMin == 2
|
|
},
|
|
},
|
|
{
|
|
name: "multiple labels AND",
|
|
query: "label=frontend AND label=urgent",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.Labels) == 2 &&
|
|
(f.Labels[0] == "frontend" || f.Labels[0] == "urgent") &&
|
|
(f.Labels[1] == "frontend" || f.Labels[1] == "urgent")
|
|
},
|
|
},
|
|
{
|
|
name: "NOT status",
|
|
query: "NOT status=closed",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.ExcludeStatus) == 1 && f.ExcludeStatus[0] == types.StatusClosed
|
|
},
|
|
},
|
|
{
|
|
name: "NOT type",
|
|
query: "NOT type=epic",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.ExcludeTypes) == 1 && f.ExcludeTypes[0] == types.TypeEpic
|
|
},
|
|
},
|
|
{
|
|
name: "labels OR uses LabelsAny",
|
|
query: "label=frontend OR label=backend",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return len(f.LabelsAny) == 2
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt() error = %v", err)
|
|
}
|
|
|
|
if tt.expectFilter != nil && !tt.expectFilter(&result.Filter) {
|
|
t.Errorf("filter check failed for %q", tt.query)
|
|
}
|
|
|
|
if result.RequiresPredicate != tt.requiresPredicate {
|
|
t.Errorf("RequiresPredicate = %v, want %v", result.RequiresPredicate, tt.requiresPredicate)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvaluatorComplexQueries(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
requiresPredicate bool
|
|
}{
|
|
{
|
|
name: "OR with different fields requires predicate",
|
|
query: "status=open OR priority>1",
|
|
requiresPredicate: true,
|
|
},
|
|
{
|
|
name: "nested OR requires predicate",
|
|
query: "(status=open OR status=blocked) AND priority<2",
|
|
requiresPredicate: true,
|
|
},
|
|
{
|
|
name: "NOT with complex expression requires predicate",
|
|
query: "NOT (status=closed AND type=bug)",
|
|
requiresPredicate: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt() error = %v", err)
|
|
}
|
|
|
|
if result.RequiresPredicate != tt.requiresPredicate {
|
|
t.Errorf("RequiresPredicate = %v, want %v", result.RequiresPredicate, tt.requiresPredicate)
|
|
}
|
|
|
|
if tt.requiresPredicate && result.Predicate == nil {
|
|
t.Error("expected Predicate to be set")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPredicateEvaluation(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
openBug := &types.Issue{
|
|
ID: "bd-1",
|
|
Status: types.StatusOpen,
|
|
Priority: 1,
|
|
IssueType: types.TypeBug,
|
|
Labels: []string{"urgent", "frontend"},
|
|
CreatedAt: now.AddDate(0, 0, -5),
|
|
UpdatedAt: now.AddDate(0, 0, -1),
|
|
}
|
|
|
|
closedTask := &types.Issue{
|
|
ID: "bd-2",
|
|
Status: types.StatusClosed,
|
|
Priority: 2,
|
|
IssueType: types.TypeTask,
|
|
Labels: []string{"backend"},
|
|
CreatedAt: now.AddDate(0, 0, -30),
|
|
UpdatedAt: now.AddDate(0, 0, -10),
|
|
}
|
|
|
|
blockedFeature := &types.Issue{
|
|
ID: "bd-3",
|
|
Status: types.StatusBlocked,
|
|
Priority: 0,
|
|
IssueType: types.TypeFeature,
|
|
Labels: []string{},
|
|
CreatedAt: now.AddDate(0, 0, -2),
|
|
UpdatedAt: now,
|
|
}
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
issue *types.Issue
|
|
matches bool
|
|
}{
|
|
// Status tests
|
|
{"status=open matches open bug", "status=open", openBug, true},
|
|
{"status=open doesn't match closed task", "status=open", closedTask, false},
|
|
{"status!=closed matches open bug", "status!=closed", openBug, true},
|
|
{"status!=closed doesn't match closed task", "status!=closed", closedTask, false},
|
|
|
|
// Priority tests
|
|
{"priority>0 matches P1 bug", "priority>0", openBug, true},
|
|
{"priority>0 doesn't match P0 feature", "priority>0", blockedFeature, false},
|
|
{"priority<=1 matches P1 bug", "priority<=1", openBug, true},
|
|
{"priority<=1 doesn't match P2 task", "priority<=1", closedTask, false},
|
|
|
|
// Type tests
|
|
{"type=bug matches bug", "type=bug", openBug, true},
|
|
{"type=bug doesn't match task", "type=bug", closedTask, false},
|
|
|
|
// Label tests
|
|
{"label=urgent matches issue with urgent", "label=urgent", openBug, true},
|
|
{"label=urgent doesn't match issue without", "label=urgent", closedTask, false},
|
|
{"label=none matches unlabeled", "label=none", blockedFeature, true},
|
|
{"label=none doesn't match labeled", "label=none", openBug, false},
|
|
|
|
// OR tests
|
|
{"status=open OR status=blocked matches open", "status=open OR status=blocked", openBug, true},
|
|
{"status=open OR status=blocked matches blocked", "status=open OR status=blocked", blockedFeature, true},
|
|
{"status=open OR status=blocked doesn't match closed", "status=open OR status=blocked", closedTask, false},
|
|
|
|
// AND tests
|
|
{"status=open AND type=bug matches", "status=open AND type=bug", openBug, true},
|
|
{"status=open AND type=bug doesn't match blocked", "status=open AND type=bug", blockedFeature, false},
|
|
|
|
// NOT tests
|
|
{"NOT status=closed matches open", "NOT status=closed", openBug, true},
|
|
{"NOT status=closed doesn't match closed", "NOT status=closed", closedTask, false},
|
|
|
|
// Complex tests
|
|
{"(status=open OR status=blocked) AND priority<2 matches P1 open", "(status=open OR status=blocked) AND priority<2", openBug, true},
|
|
{"(status=open OR status=blocked) AND priority<2 matches P0 blocked", "(status=open OR status=blocked) AND priority<2", blockedFeature, true},
|
|
{"(status=open OR status=blocked) AND priority<2 doesn't match P2 closed", "(status=open OR status=blocked) AND priority<2", closedTask, false},
|
|
|
|
// Label OR tests
|
|
{"label=urgent OR label=backend matches urgent", "label=urgent OR label=backend", openBug, true},
|
|
{"label=urgent OR label=backend matches backend", "label=urgent OR label=backend", closedTask, true},
|
|
{"label=urgent OR label=backend doesn't match unlabeled", "label=urgent OR label=backend", blockedFeature, false},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt() error = %v", err)
|
|
}
|
|
|
|
// For simple queries, check filter
|
|
if !result.RequiresPredicate {
|
|
// Build predicate anyway for testing
|
|
eval := NewEvaluator(now)
|
|
node, _ := Parse(tt.query)
|
|
pred, err := eval.buildPredicate(node)
|
|
if err != nil {
|
|
t.Fatalf("buildPredicate() error = %v", err)
|
|
}
|
|
got := pred(tt.issue)
|
|
if got != tt.matches {
|
|
t.Errorf("predicate(%s) = %v, want %v", tt.issue.ID, got, tt.matches)
|
|
}
|
|
} else {
|
|
// Use the predicate from result
|
|
got := result.Predicate(tt.issue)
|
|
if got != tt.matches {
|
|
t.Errorf("predicate(%s) = %v, want %v", tt.issue.ID, got, tt.matches)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestEvaluatorErrors(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
}{
|
|
{"invalid status", "status=invalid"},
|
|
{"invalid priority", "priority=abc"},
|
|
{"priority out of range", "priority=5"},
|
|
{"invalid boolean", "pinned=maybe"},
|
|
{"unknown field", "unknown=value"},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
_, err := Evaluate(tt.query)
|
|
if err == nil {
|
|
t.Error("expected error, got nil")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestDurationParsing(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
eval := NewEvaluator(now)
|
|
|
|
tests := []struct {
|
|
duration string
|
|
expected time.Time
|
|
}{
|
|
{"7d", now.AddDate(0, 0, -7)},
|
|
{"24h", now.Add(-24 * time.Hour)},
|
|
{"2w", now.AddDate(0, 0, -14)},
|
|
{"1m", now.AddDate(0, -1, 0)},
|
|
{"1y", now.AddDate(-1, 0, 0)},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.duration, func(t *testing.T) {
|
|
got, err := eval.parseDurationAgo(tt.duration)
|
|
if err != nil {
|
|
t.Fatalf("parseDurationAgo() error = %v", err)
|
|
}
|
|
|
|
// Compare dates (ignore time precision)
|
|
if got.Year() != tt.expected.Year() || got.Month() != tt.expected.Month() || got.Day() != tt.expected.Day() {
|
|
t.Errorf("got %v, want %v", got, tt.expected)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestQueryMinutesUnitResolvesAgo pins the query-plane semantics of the "min"
|
|
// minutes unit in the shared compact-duration grammar: like every other duration
|
|
// token here it resolves to N *ago*, per the documented contract in
|
|
// `bd query --help` ("7d (7 days ago)"). The fixed now makes the exact instant
|
|
// assertable, so classifying "30min" as an identifier again — which routes it to
|
|
// the future-relative ParseRelativeTime fallback — fails this test.
|
|
func TestQueryMinutesUnitResolvesAgo(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
want time.Time
|
|
bound func(*types.IssueFilter) *time.Time
|
|
}{
|
|
{
|
|
name: "created>30min is 30 minutes ago",
|
|
query: "created>30min",
|
|
want: now.Add(-30 * time.Minute),
|
|
bound: func(f *types.IssueFilter) *time.Time { return f.CreatedAfter },
|
|
},
|
|
{
|
|
name: "updated<90min is 90 minutes ago",
|
|
query: "updated<90min",
|
|
want: now.Add(-90 * time.Minute),
|
|
bound: func(f *types.IssueFilter) *time.Time { return f.UpdatedBefore },
|
|
},
|
|
{
|
|
name: "bare m still means months, not minutes",
|
|
query: "created>30m",
|
|
want: now.AddDate(0, -30, 0),
|
|
bound: func(f *types.IssueFilter) *time.Time { return f.CreatedAfter },
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(%q) error = %v", tt.query, err)
|
|
}
|
|
got := tt.bound(&result.Filter)
|
|
if got == nil {
|
|
t.Fatalf("EvaluateAt(%q) left the time bound unset, filter=%+v", tt.query, result.Filter)
|
|
}
|
|
if !got.Equal(tt.want) {
|
|
t.Errorf("EvaluateAt(%q) = %s, want %s", tt.query, got.Format(time.RFC3339), tt.want.Format(time.RFC3339))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestQueryRejectsUppercaseDurationUnits pins how an uppercase duration unit
|
|
// fails on a time field. Units are lowercase in the shared grammar, so "30M"
|
|
// is not a duration: it reaches ParseRelativeTime as typed, fails there, and
|
|
// the error quotes what the user wrote. It must never be the internal
|
|
// "not a compact duration" error, which quoted the negated "-30M" the
|
|
// evaluator builds for a duration token. "30MIN" and "30Min" always took the
|
|
// ParseRelativeTime path; they are controls for the expected error.
|
|
func TestQueryRejectsUppercaseDurationUnits(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
values := []string{"30M", "30D", "30W", "30Y", "30H", "1H", "30MIN", "30Min"}
|
|
shapes := []string{
|
|
"created>%s",
|
|
"updated<%s",
|
|
"closed>=%s",
|
|
"started<=%s",
|
|
"created>%s OR status=open",
|
|
}
|
|
|
|
for _, value := range values {
|
|
for _, shape := range shapes {
|
|
query := fmt.Sprintf(shape, value)
|
|
t.Run(query, func(t *testing.T) {
|
|
_, err := EvaluateAt(query, now)
|
|
if err == nil {
|
|
t.Fatalf("EvaluateAt(%q) succeeded, want an unparseable-time error", query)
|
|
}
|
|
msg := err.Error()
|
|
if want := "cannot parse time expression: " + strconv.Quote(value); !strings.Contains(msg, want) {
|
|
t.Errorf("EvaluateAt(%q) error = %q, want it to contain %q", query, msg, want)
|
|
}
|
|
if strings.Contains(msg, "not a compact duration") {
|
|
t.Errorf("EvaluateAt(%q) error = %q, want no internal compact-duration error", query, msg)
|
|
}
|
|
if negated := strconv.Quote("-" + value); strings.Contains(msg, negated) {
|
|
t.Errorf("EvaluateAt(%q) error = %q quotes %s, a value the user never typed", query, msg, negated)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
}
|
|
|
|
// TestQuerySignedUppercaseDurationUnitStopsAtTheParser pins how a signed
|
|
// uppercase unit fails. A signed digit run has no identifier fallback (the
|
|
// user has to quote "-3-foo"), so with "M" not a unit, "+30M" lexes as the
|
|
// number "+30" and then the identifier "M", and the parser rejects that stray
|
|
// token as it already rejects the "x" of "+30x". It must not reach the
|
|
// evaluator as a duration and fail there with the internal
|
|
// "not a compact duration" error.
|
|
func TestQuerySignedUppercaseDurationUnitStopsAtTheParser(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
query string
|
|
wantErr string
|
|
}{
|
|
{"created>+30M", `unexpected token "M" at position 11 (expected end of query)`},
|
|
{"created>-30M", `unexpected token "M" at position 11 (expected end of query)`},
|
|
// Control: an unknown lowercase unit already fails this way.
|
|
{"created>+30x", `unexpected token "x" at position 11 (expected end of query)`},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.query, func(t *testing.T) {
|
|
if _, err := Parse(tt.query); err == nil || err.Error() != tt.wantErr {
|
|
t.Errorf("Parse(%q) error = %v, want %q", tt.query, err, tt.wantErr)
|
|
}
|
|
if _, err := EvaluateAt(tt.query, now); err == nil || err.Error() != tt.wantErr {
|
|
t.Errorf("EvaluateAt(%q) error = %v, want %q", tt.query, err, tt.wantErr)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestQueryKeepsLowercaseUnitsAndLiteralValues pins, at a fixed now, what
|
|
// rejecting uppercase units leaves alone: a signed lowercase unit still
|
|
// resolves as N ago, a natural-language time still resolves through
|
|
// ParseRelativeTime, and on a non-time field "30M" is still the literal string
|
|
// it always was. TestQueryMinutesUnitResolvesAgo pins created>30m (30 months
|
|
// ago) and created>30min (30 minutes ago).
|
|
func TestQueryKeepsLowercaseUnitsAndLiteralValues(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
bounds := []struct {
|
|
query string
|
|
want time.Time
|
|
bound func(*types.IssueFilter) *time.Time
|
|
}{
|
|
{
|
|
query: "created>+30d",
|
|
want: now.AddDate(0, 0, -30),
|
|
bound: func(f *types.IssueFilter) *time.Time { return f.CreatedAfter },
|
|
},
|
|
{
|
|
query: "updated>5pm",
|
|
want: time.Date(2025, 2, 4, 17, 0, 0, 0, time.UTC),
|
|
bound: func(f *types.IssueFilter) *time.Time { return f.UpdatedAfter },
|
|
},
|
|
}
|
|
for _, tt := range bounds {
|
|
t.Run(tt.query, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(%q) error = %v", tt.query, err)
|
|
}
|
|
got := tt.bound(&result.Filter)
|
|
if got == nil {
|
|
t.Fatalf("EvaluateAt(%q) left the time bound unset, filter=%+v", tt.query, result.Filter)
|
|
}
|
|
if !got.Equal(tt.want) {
|
|
t.Errorf("EvaluateAt(%q) = %s, want %s", tt.query, got.Format(time.RFC3339), tt.want.Format(time.RFC3339))
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("label=30M", func(t *testing.T) {
|
|
result, err := EvaluateAt("label=30M", now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(label=30M) error = %v", err)
|
|
}
|
|
if len(result.Filter.Labels) != 1 || result.Filter.Labels[0] != "30M" {
|
|
t.Errorf("EvaluateAt(label=30M) Labels = %v, want [%q]", result.Filter.Labels, "30M")
|
|
}
|
|
})
|
|
t.Run("title=30M", func(t *testing.T) {
|
|
result, err := EvaluateAt("title=30M", now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(title=30M) error = %v", err)
|
|
}
|
|
if result.Filter.TitleContains != "30M" {
|
|
t.Errorf("EvaluateAt(title=30M) TitleContains = %q, want %q", result.Filter.TitleContains, "30M")
|
|
}
|
|
})
|
|
}
|
|
|
|
// TestCompactDurationGrammarReachesQueryPlane binds this package's duration
|
|
// grammar to the shared one in internal/timeparsing, which owns it, in both
|
|
// directions. The lexer classifies a duration with
|
|
// timeparsing.IsCompactDuration instead of keeping its own copy of the unit
|
|
// set; this test holds it to that. A unit the shared grammar accepts but the
|
|
// lexer types as TokenIdent skips the evaluator's TokenDuration arm and
|
|
// resolves through ParseRelativeTime as a *future* offset: the "30min meant 30
|
|
// minutes from now" inversion, reproduced silently for the next unit. A suffix
|
|
// the shared grammar rejects but the lexer types as TokenDuration reaches
|
|
// ParseCompactDuration in the evaluator and fails with that parser's internal
|
|
// error about a negated value the user never typed: while the lexer took
|
|
// "H D W M Y", "created>30M" failed with `not a compact duration: "-30M"`.
|
|
//
|
|
// ParseCompactDuration is the oracle: a suffix must lex as a duration here if
|
|
// and only if it accepts it. Candidates are exhaustive over one- and
|
|
// two-letter ASCII in either case and three-letter lowercase ASCII, plus every
|
|
// case spelling of an accepted three-letter unit, so plausible future
|
|
// spellings ("sec", "hr", "mo", "yr") are covered alongside today's six and
|
|
// their uppercase forms.
|
|
func TestCompactDurationGrammarReachesQueryPlane(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
accepts := func(unit string) bool {
|
|
_, err := timeparsing.ParseCompactDuration("1"+unit, now)
|
|
return err == nil
|
|
}
|
|
|
|
const lower = "abcdefghijklmnopqrstuvwxyz"
|
|
const letters = lower + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
var candidates []string
|
|
for _, a := range letters {
|
|
candidates = append(candidates, string(a))
|
|
for _, b := range letters {
|
|
candidates = append(candidates, string(a)+string(b))
|
|
}
|
|
}
|
|
for _, a := range lower {
|
|
for _, b := range lower {
|
|
for _, c := range lower {
|
|
unit := string(a) + string(b) + string(c)
|
|
candidates = append(candidates, unit)
|
|
if !accepts(unit) {
|
|
continue
|
|
}
|
|
// The lowercase sweep reaches only this spelling of an
|
|
// accepted three-letter unit; add the others ("MIN", "Min", ...).
|
|
for mask := 1; mask < 1<<len(unit); mask++ {
|
|
var spelling strings.Builder
|
|
for i := 0; i < len(unit); i++ {
|
|
if mask&(1<<i) != 0 {
|
|
spelling.WriteString(strings.ToUpper(unit[i : i+1]))
|
|
} else {
|
|
spelling.WriteByte(unit[i])
|
|
}
|
|
}
|
|
candidates = append(candidates, spelling.String())
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
var accepted, rejected []string
|
|
isAccepted := make(map[string]bool)
|
|
for _, unit := range candidates {
|
|
if !accepts(unit) {
|
|
rejected = append(rejected, unit)
|
|
continue
|
|
}
|
|
accepted = append(accepted, unit)
|
|
isAccepted[unit] = true
|
|
}
|
|
|
|
// Guard the oracle itself: if ParseCompactDuration stopped accepting the
|
|
// units the grammar documents, every assertion below would pass vacuously.
|
|
for _, unit := range []string{"min", "h", "d", "w", "m", "y"} {
|
|
if !isAccepted[unit] {
|
|
t.Fatalf("oracle lost unit %q: timeparsing no longer accepts %q", unit, "1"+unit)
|
|
}
|
|
}
|
|
|
|
for _, unit := range accepted {
|
|
t.Run(unit, func(t *testing.T) {
|
|
value := "1" + unit
|
|
|
|
tokens, err := NewLexer("updated>" + value).Tokenize()
|
|
if err != nil {
|
|
t.Fatalf("Tokenize(updated>%s) error = %v", value, err)
|
|
}
|
|
if len(tokens) < 3 {
|
|
t.Fatalf("Tokenize(updated>%s) produced %d tokens, want at least 3", value, len(tokens))
|
|
}
|
|
if tokens[2].Type != TokenDuration || tokens[2].Value != value {
|
|
t.Errorf("timeparsing accepts %q but the query lexer typed it %v(%q), want %v(%q); "+
|
|
"classify durations with timeparsing.IsCompactDuration or the unit resolves as a future offset",
|
|
value, tokens[2].Type, tokens[2].Value, TokenDuration, value)
|
|
}
|
|
|
|
// Widening the duration grammar must not change non-time fields:
|
|
// parseTimeValue is the only consumer of ValueType, so a duration
|
|
// token on label= is still filtered as the literal string.
|
|
result, err := EvaluateAt("label="+value, now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(label=%s) error = %v", value, err)
|
|
}
|
|
if len(result.Filter.Labels) != 1 || result.Filter.Labels[0] != value {
|
|
t.Errorf("EvaluateAt(label=%s) Labels = %v, want [%q]", value, result.Filter.Labels, value)
|
|
}
|
|
})
|
|
}
|
|
|
|
// The reverse direction is one sweep with one report rather than a
|
|
// subtest per suffix: there are over 20,000 of them.
|
|
var typedDuration []string
|
|
for _, unit := range rejected {
|
|
value := "1" + unit
|
|
tokens, err := NewLexer("updated>" + value).Tokenize()
|
|
if err != nil {
|
|
t.Errorf("Tokenize(updated>%s) error = %v", value, err)
|
|
continue
|
|
}
|
|
for _, tok := range tokens {
|
|
if tok.Type == TokenDuration {
|
|
typedDuration = append(typedDuration, unit)
|
|
break
|
|
}
|
|
}
|
|
}
|
|
if len(typedDuration) > 0 {
|
|
t.Errorf("timeparsing rejects these suffixes but the query lexer typed them %v: %v; "+
|
|
"classify durations with timeparsing.IsCompactDuration or the evaluator fails on them "+
|
|
"with an internal error", TokenDuration, typedDuration)
|
|
}
|
|
}
|
|
|
|
func TestEvaluatorMetadataQueries(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
expectFilter func(*types.IssueFilter) bool
|
|
requiresPredicate bool
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "metadata.team=platform",
|
|
query: "metadata.team=platform",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.MetadataFields != nil && f.MetadataFields["team"] == "platform"
|
|
},
|
|
},
|
|
{
|
|
name: "metadata.jira.sprint=Q1",
|
|
query: "metadata.jira.sprint=Q1",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.MetadataFields != nil && f.MetadataFields["jira.sprint"] == "Q1"
|
|
},
|
|
},
|
|
{
|
|
name: "metadata.jira/sprint=Q1",
|
|
query: "metadata.jira/sprint=Q1",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.MetadataFields != nil && f.MetadataFields["jira/sprint"] == "Q1"
|
|
},
|
|
},
|
|
{
|
|
name: "metadata.Sprint=Q1 preserves key case",
|
|
query: "metadata.Sprint=Q1",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.MetadataFields != nil && f.MetadataFields["Sprint"] == "Q1"
|
|
},
|
|
},
|
|
{
|
|
name: "metadata combined with status",
|
|
query: "status=open AND metadata.team=platform",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.Status != nil && *f.Status == types.StatusOpen &&
|
|
f.MetadataFields != nil && f.MetadataFields["team"] == "platform"
|
|
},
|
|
},
|
|
{
|
|
name: "metadata in OR triggers predicate",
|
|
query: "metadata.team=platform OR status=open",
|
|
requiresPredicate: true,
|
|
},
|
|
{
|
|
name: "metadata with unsupported operator",
|
|
query: "metadata.team>platform",
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if tt.expectError {
|
|
if err == nil {
|
|
t.Fatalf("expected error for %q, got nil", tt.query)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(%q) error = %v", tt.query, err)
|
|
}
|
|
if tt.expectFilter != nil && !tt.expectFilter(&result.Filter) {
|
|
t.Errorf("filter check failed for %q, filter=%+v", tt.query, result.Filter)
|
|
}
|
|
if result.RequiresPredicate != tt.requiresPredicate {
|
|
t.Errorf("RequiresPredicate = %v, want %v for %q", result.RequiresPredicate, tt.requiresPredicate, tt.query)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestMetadataKeysAreQueryable enforces the cross-package invariant that
|
|
// every key accepted by storage.ValidateMetadataKey can be queried as
|
|
// "metadata.<key>=<value>". The lexer's identifier alphabet and the storage
|
|
// key regex are maintained independently; a character admitted to the regex
|
|
// but not to isIdentChar would produce keys that store fine but are
|
|
// unqueryable (the bug this test guards against). Metadata keys are
|
|
// case-sensitive JSON keys, so the filter key must preserve the query key's
|
|
// case even though field names are otherwise case-insensitive.
|
|
func TestMetadataKeysAreQueryable(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
for c := rune(' '); c <= '~'; c++ {
|
|
key := "a" + string(c) + "b"
|
|
if storage.ValidateMetadataKey(key) != nil {
|
|
continue
|
|
}
|
|
result, err := EvaluateAt("metadata."+key+"=v", now)
|
|
if err != nil {
|
|
t.Errorf("key %q is valid per ValidateMetadataKey but not queryable: %v", key, err)
|
|
continue
|
|
}
|
|
if result.Filter.MetadataFields[key] != "v" {
|
|
t.Errorf("key %q is valid per ValidateMetadataKey but query parsed to filter %+v", key, result.Filter)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEvaluatorHasMetadataKeyQueries(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
tests := []struct {
|
|
name string
|
|
query string
|
|
expectFilter func(*types.IssueFilter) bool
|
|
requiresPredicate bool
|
|
expectError bool
|
|
}{
|
|
{
|
|
name: "has_metadata_key=team",
|
|
query: "has_metadata_key=team",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.HasMetadataKey == "team"
|
|
},
|
|
},
|
|
{
|
|
name: "has_metadata_key combined with status",
|
|
query: "has_metadata_key=sprint AND status=open",
|
|
expectFilter: func(f *types.IssueFilter) bool {
|
|
return f.HasMetadataKey == "sprint" &&
|
|
f.Status != nil && *f.Status == types.StatusOpen
|
|
},
|
|
},
|
|
{
|
|
name: "has_metadata_key in OR triggers predicate",
|
|
query: "has_metadata_key=team OR status=open",
|
|
requiresPredicate: true,
|
|
},
|
|
{
|
|
name: "has_metadata_key with invalid key",
|
|
query: `has_metadata_key="bad key"`,
|
|
expectError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
result, err := EvaluateAt(tt.query, now)
|
|
if tt.expectError {
|
|
if err == nil {
|
|
t.Fatalf("expected error for %q, got nil", tt.query)
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt(%q) error = %v", tt.query, err)
|
|
}
|
|
if tt.expectFilter != nil && !tt.expectFilter(&result.Filter) {
|
|
t.Errorf("filter check failed for %q, filter=%+v", tt.query, result.Filter)
|
|
}
|
|
if result.RequiresPredicate != tt.requiresPredicate {
|
|
t.Errorf("RequiresPredicate = %v, want %v for %q", result.RequiresPredicate, tt.requiresPredicate, tt.query)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestHasMetadataKeyPredicateEvaluation(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
result, err := EvaluateAt("has_metadata_key=team OR status=closed", now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt error: %v", err)
|
|
}
|
|
if result.Predicate == nil {
|
|
t.Fatal("expected predicate for OR query")
|
|
}
|
|
|
|
// Issue with the key present
|
|
issueMatch := &types.Issue{
|
|
Status: types.StatusOpen,
|
|
Metadata: []byte(`{"team":"platform"}`),
|
|
}
|
|
if !result.Predicate(issueMatch) {
|
|
t.Error("predicate should match issue with team key present")
|
|
}
|
|
|
|
// Issue without the key
|
|
issueNoKey := &types.Issue{
|
|
Status: types.StatusOpen,
|
|
Metadata: []byte(`{"sprint":"Q1"}`),
|
|
}
|
|
if result.Predicate(issueNoKey) {
|
|
t.Error("predicate should not match issue without team key")
|
|
}
|
|
|
|
// Issue with no metadata but closed status (matches second branch)
|
|
issueClosed := &types.Issue{
|
|
Status: types.StatusClosed,
|
|
}
|
|
if !result.Predicate(issueClosed) {
|
|
t.Error("predicate should match closed issue via OR")
|
|
}
|
|
}
|
|
|
|
func TestMetadataPredicateEvaluation(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
result, err := EvaluateAt("metadata.team=platform OR status=closed", now)
|
|
if err != nil {
|
|
t.Fatalf("EvaluateAt error: %v", err)
|
|
}
|
|
if result.Predicate == nil {
|
|
t.Fatal("expected predicate for OR query")
|
|
}
|
|
|
|
// Issue with matching metadata
|
|
issueMatch := &types.Issue{
|
|
Status: types.StatusOpen,
|
|
Metadata: []byte(`{"team":"platform"}`),
|
|
}
|
|
if !result.Predicate(issueMatch) {
|
|
t.Error("predicate should match issue with team=platform")
|
|
}
|
|
|
|
// Issue with non-matching metadata
|
|
issueNoMatch := &types.Issue{
|
|
Status: types.StatusOpen,
|
|
Metadata: []byte(`{"team":"frontend"}`),
|
|
}
|
|
if result.Predicate(issueNoMatch) {
|
|
t.Error("predicate should not match issue with team=frontend")
|
|
}
|
|
|
|
// Issue with no metadata but closed status (matches second branch)
|
|
issueClosed := &types.Issue{
|
|
Status: types.StatusClosed,
|
|
}
|
|
if !result.Predicate(issueClosed) {
|
|
t.Error("predicate should match closed issue via OR")
|
|
}
|
|
}
|