beads/internal/storage/metadata_jsonpath_test.go
Isis Anisoptera 40b7f75c26
fix(query): make slash and mixed-case metadata keys queryable (#4896)
* fix(metadata): allow / in metadata keys

ValidateMetadataKey rejected slashes even though keys are always passed
as bind parameters and JSONMetadataPath quotes non-identifier keys, so
the restriction had no safety purpose. Allow / in the key charset and
quote slash keys in JSON paths (unquoted, / is a path syntax error).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019jrTawz2CRUiJLCt2UTQ2B

* fix(query): make slash metadata keys queryable; always quote JSON paths

Code-review follow-ups to the / metadata-key change:

- Allow / in query-language identifiers so keys like jira/sprint are
  reachable from 'metadata.jira/sprint=42' expressions, not just the
  --metadata-field flag.
- JSONMetadataPath now quotes every key unconditionally (valid for plain
  keys too), removing the separator char list that had to stay in sync
  with validMetadataKeyRe.
- Derive the ValidateMetadataKey error message from the regex itself.
- Collapse the five JSONMetadataPath clone tests into one table-driven
  test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019jrTawz2CRUiJLCt2UTQ2B

* test(query): enforce that all valid metadata keys are queryable

Sweep every printable ASCII character and assert each key accepted by
storage.ValidateMetadataKey parses as metadata.<key>=<value>, so the
lexer's identifier alphabet and the storage key regex can't drift apart
and silently recreate the unqueryable-key bug class.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019jrTawz2CRUiJLCt2UTQ2B

* fix(query): preserve metadata key case; escape JSON path keys

Review follow-ups on this branch:

- The parser lowercased the whole field token, so mixed-case metadata
  keys (valid per ValidateMetadataKey, stored case-sensitively) were
  silently unqueryable via metadata.<key>=<value> — and inconsistent
  with has_metadata_key and the --metadata flag, which preserve case.
  Keep field-name matching case-insensitive but restore the key
  suffix's original case.

- JSONMetadataPath spliced the key into the quoted path unescaped,
  relying on every caller validating first. Escape \ and " locally so
  the function is correct for any input.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_019jrTawz2CRUiJLCt2UTQ2B

---------

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
Co-authored-by: matt wilkie <maphew@gmail.com>
2026-07-24 17:52:22 -07:00

25 lines
570 B
Go

package storage
import "testing"
func TestJSONMetadataPath(t *testing.T) {
tests := []struct {
key string
want string
}{
{"status", `$."status"`},
{"my_field", `$."my_field"`},
{"gc.routed_to", `$."gc.routed_to"`},
{"gc.scope.ref", `$."gc.scope.ref"`},
{"jira/sprint", `$."jira/sprint"`},
{`ke"y`, `$."ke\"y"`},
{`ke\y`, `$."ke\\y"`},
}
for _, tt := range tests {
t.Run(tt.key, func(t *testing.T) {
if got := JSONMetadataPath(tt.key); got != tt.want {
t.Errorf("JSONMetadataPath(%q) = %q, want %q", tt.key, got, tt.want)
}
})
}
}