* fix(timeparsing): add unambiguous "min" minute unit to compact durations --defer/--due use the compact duration parser, whose smallest unit was one hour. "+30m" silently parsed as 30 months (rc 0, no warning), so a row meant to return in half an hour became ready again years later, while "+30min" was rejected outright. Add "min" as a new minute spelling alongside h/d/w/m/y. Bare "m" keeps meaning months, so existing callers are unchanged; redefining or refusing bare "m" is a semantics change left for maintainer sign-off. Fixes #6609 Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> * fix(query): resolve the "min" minutes unit as "N ago" in bd query The widened compact-duration grammar reached the `bd query` plane through the evaluator's ungated fallback arm: `isDurationSuffix` classifies a single rune, so `30min` lexed as TokenIdent, skipped `parseDurationAgo`, and resolved through `ParseRelativeTime` as 30 minutes in the *future* -- the opposite direction from `created > 30m` (30 months ago) and from the command's own documented contract at cmd/bd/query.go:66. Recognize multi-rune duration suffixes in `readNumberOrDuration` so `30min` lexes as TokenDuration and resolves to 30 minutes ago. Plural, uppercase, and identifier-continuation spellings (`30mins`, `30MIN`, `30min-window`) keep their existing identifier classification. Maintainer review fixups for PR #6629, review iteration 1: - pin the query-plane semantics with EvaluateAt tests against a fixed now - make the months assertion in TestParseTimeFlag_MinutesVsMonths two-sided, so it can no longer stay green if "m" regressed to years - mention +30min in the ParseRelativeTime error examples Maintainer review fixups for PR #6629, review iteration 2 (non-gating): - bind the query lexer's unit set to the shared timeparsing grammar with a cross-package test. ParseCompactDuration is the oracle, so a unit added to that grammar but missed in durationWords/isDurationSuffix now fails loudly instead of silently resolving as a future offset -- the class this fixup previously closed only for "min". Verified by adding "sec" to the shared pattern alone: the test discovers and fails it with no test edit. - pin that a duration token on a non-time field (label=1min) is still filtered as a literal string, since parseTimeValue is the only consumer of ValueType. - list min/m/y in the bd query duration help, so "m" reads as months right next to "min". The generated CLI docs are release-pinned (docs/cli-docs.pin v1.2.2, built from the tag rather than the working tree), so they are deliberately not regenerated here; ./scripts/generate-cli-docs.sh --check reports them fresh. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com> Co-authored-by: Julian Knutsen <julianknutsen@users.noreply.github.com>
1090 lines
34 KiB
Go
1090 lines
34 KiB
Go
package query
|
|
|
|
import (
|
|
"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", ""},
|
|
},
|
|
}
|
|
|
|
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))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestCompactDurationGrammarReachesQueryPlane binds this package's duration
|
|
// grammar to the shared one in internal/timeparsing, which owns it. The lexer
|
|
// keeps a second copy of the unit set — isDurationSuffix for single runes,
|
|
// durationWords for whole words — and nothing outside this test holds the two
|
|
// in sync. A unit added to the shared grammar but missed here lexes as
|
|
// TokenIdent, skips the evaluator's TokenDuration arm, and resolves through
|
|
// ParseRelativeTime as a *future* offset: the "30min meant 30 minutes from now"
|
|
// inversion this PR fixed, reproduced silently for the next unit.
|
|
//
|
|
// The shared pattern is unexported, so ParseCompactDuration is the oracle:
|
|
// whatever suffix it accepts must lex as a duration here. Candidates are
|
|
// exhaustive over lowercase ASCII up to three runes, so plausible future
|
|
// spellings ("sec", "hr", "mo", "yr") are covered alongside today's six.
|
|
//
|
|
// Only this direction is asserted. The reverse — everything the lexer accepts
|
|
// being valid in the shared grammar — is already false at base: isDurationSuffix
|
|
// also takes "H D W M Y", which the case-sensitive pattern rejects, so
|
|
// "created>30M" lexes as a duration and then fails in ParseCompactDuration.
|
|
// That asymmetry predates this PR and is not what this test is for.
|
|
func TestCompactDurationGrammarReachesQueryPlane(t *testing.T) {
|
|
now := time.Date(2025, 2, 4, 12, 0, 0, 0, time.UTC)
|
|
|
|
const letters = "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 _, c := range letters {
|
|
candidates = append(candidates, string(a)+string(b)+string(c))
|
|
}
|
|
}
|
|
}
|
|
|
|
var accepted []string
|
|
isAccepted := make(map[string]bool)
|
|
for _, unit := range candidates {
|
|
if _, err := timeparsing.ParseCompactDuration("1"+unit, now); err != nil {
|
|
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); "+
|
|
"add the unit to durationWords/isDurationSuffix or it 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)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
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")
|
|
}
|
|
}
|