Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

34.7 KB
Raw Blame
import { strict as assert } from "node:assert";
import { test } from "vitest";
import { createSidebarLabelMatcher, matchSidebarLabel, type SidebarSearchMatchMode } from "../../apps/desktop/src/lib/sidebar/sidebarSearch.ts";
import { compileSearchRegex, parseSlashDelimitedRegexQuery } from "../../apps/desktop/src/lib/common/searchPattern.ts";
import { filterSidebarTree } from "../../apps/desktop/src/lib/sidebar/sidebarSearchTree.ts";
import type { TreeNode } from "../../apps/desktop/src/types/database.ts";

test("matches exact and prefix labels first", () => {
  assert.equal(matchSidebarLabel("orders", "orders")?.kind, "exact");
  assert.equal(matchSidebarLabel("orders_archive", "ord")?.kind, "prefix");
});

test("matches word prefixes in underscored and dotted identifiers", () => {
  assert.equal(matchSidebarLabel("user_orders", "ord")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("sales.customer_profile", "cust")?.kind, "word-prefix");
});

test("matches DataGrip-style abbreviations by identifier word boundaries", () => {
  assert.equal(matchSidebarLabel("additional_country", "ac")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("sales.customer_profile", "scp")?.kind, "abbreviation");
  // Existing behavior: s/system + e/exception + l/log.
  const sel = matchSidebarLabel("system_exception_log", "sel");
  assert.equal(sel?.kind, "abbreviation");
  assert.equal(sel?.score, 60);
});

test("keeps one-character fuzzy matches disabled", () => {
  assert.equal(matchSidebarLabel("orders", "r")?.kind, "substring");
  assert.equal(matchSidebarLabel("orders", "x"), null);
});

test("matches separator-blind prefix when user omits the underscore between prefix and name", () => {
  // "delo" → "del_order": stripped "delorder" starts with "delo"
  assert.equal(matchSidebarLabel("del_order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del_order", "delo")?.score, 65);
  // "usrp" → "usr_profile": stripped "usrprofile" starts with "usrp"
  assert.equal(matchSidebarLabel("usr_profile", "usrp")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("usr_profile", "usrp")?.score, 65);
});

test("matches separator-blind substring across underscore boundaries", () => {
  // "delord" → "del_order": stripped "delorder" starts with "delord"
  // → separator-blind prefix, not substring
  assert.equal(matchSidebarLabel("del_order", "delord")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del_order", "delord")?.score, 65);
  // "elo" → "del_order": stripped "delorder" includes "elo" as a substring
  // (not at the start, so falls to separator-blind substring)
  assert.equal(matchSidebarLabel("del_order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del_order", "elo")?.score, 55);
  // "userpro" → "user_profile": stripped "userprofile" starts with "userpro"
  assert.equal(matchSidebarLabel("user_profile", "userpro")?.kind, "word-prefix");
});

test("does not match loose subsequences that object search would exclude", () => {
  // "roles" does NOT match "sys_role_data_scope" — the separator-blind
  // check strips to "sysroledatascope" which does not include "roles" as
  // a contiguous substring, and we intentionally exclude fuzzy/subsequence
  // matching on the stripped form to avoid false positives.
  assert.equal(matchSidebarLabel("sys_role_data_scope", "roles"), null);
  assert.equal(matchSidebarLabel("sys_role_data_scope", "role")?.kind, "word-prefix");
});

test("keeps fuzzy subsequence matching inside a single identifier word", () => {
  assert.equal(matchSidebarLabel("orders", "odr")?.kind, "fuzzy");
  assert.equal(matchSidebarLabel("user_profile", "up")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("user_profile", "urf"), null);
});

// ── Separator-blind extended coverage ──

test("separator-blind prefix works with hyphen, dot, space, and backslash separators", () => {
  // Hyphen separator
  assert.equal(matchSidebarLabel("del-order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del-order", "delo")?.score, 65);
  // Dot separator (common in schema-qualified names)
  assert.equal(matchSidebarLabel("del.order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del.order", "delo")?.score, 65);
  // Space separator
  assert.equal(matchSidebarLabel("del order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del order", "delo")?.score, 65);
  // Backslash separator (Windows / MSSQL linked-server paths)
  assert.equal(matchSidebarLabel("del\\order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del\\order", "delo")?.score, 65);
});

test("separator-blind substring works with hyphen, dot, space, and backslash separators", () => {
  // "elo" sits after the separator in all four forms → stripped includes it
  assert.equal(matchSidebarLabel("del-order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del-order", "elo")?.score, 55);
  assert.equal(matchSidebarLabel("del.order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del.order", "elo")?.score, 55);
  assert.equal(matchSidebarLabel("del order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del order", "elo")?.score, 55);
  assert.equal(matchSidebarLabel("del\\order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del\\order", "elo")?.score, 55);
});

test("separator-blind prefix works across three segments", () => {
  // "del_order_history" → stripped "delorderhistory"
  // "delorderhis" covers "del" + "order" + "his" → prefix
  assert.equal(matchSidebarLabel("del_order_history", "delorderhis")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del_order_history", "delorderhis")?.score, 65);
  // "sys_user_log" → stripped "sysuserlog", starts with "sysuser"
  assert.equal(matchSidebarLabel("sys_user_log", "sysuser")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("sys_user_log", "sysuser")?.score, 65);
});

test("separator-blind substring works across three segments", () => {
  // "del_order_history" → stripped "delorderhistory", includes "erhi" (from "order_history")
  const r1 = matchSidebarLabel("del_order_history", "erhi");
  assert.equal(r1?.kind, "substring");
  assert.equal(r1?.score, 55);
  // "sys_user_log" → stripped "sysuserlog", includes "userlo" (crosses "user" + "log")
  const r2 = matchSidebarLabel("sys_user_log", "userlo");
  assert.equal(r2?.kind, "substring");
  assert.equal(r2?.score, 55);
});

test("separator-blind works with consecutive separators", () => {
  // Double underscores
  assert.equal(matchSidebarLabel("del__order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del__order", "delo")?.score, 65);
  // Double hyphens
  assert.equal(matchSidebarLabel("del--order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del--order", "delo")?.score, 65);
  // Double dots
  assert.equal(matchSidebarLabel("del..order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del..order", "delo")?.score, 65);
});

test("separator-blind works with forward-slash separators", () => {
  // / is in both isWordBoundary and stripSeparators
  // prefix across /
  assert.equal(matchSidebarLabel("del/order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del/order", "delo")?.score, 65);
  // substring across /
  assert.equal(matchSidebarLabel("del/order", "elo")?.kind, "substring");
  assert.equal(matchSidebarLabel("del/order", "elo")?.score, 55);
  // abbreviation: d(0 boundary) o(4 boundary after /) → 60
  assert.equal(matchSidebarLabel("del/order", "do")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("del/order", "do")?.score, 60);
  // three-segment with /
  assert.equal(matchSidebarLabel("user/role/permission", "urp")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("user/role/permission", "urp")?.score, 60);
  // word-prefix at boundary: "role" starts after /
  assert.equal(matchSidebarLabel("user/role/permission", "role")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("user/role/permission", "role")?.score, 80);
  // separator-blind prefix across multiple /
  assert.equal(matchSidebarLabel("user/role/permission", "userrole")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("user/role/permission", "userrole")?.score, 65);
});

test("separator-blind works with mixed separators in the same label", () => {
  // Underscore then dot (common in MySQL schema.table notation)
  // "del_order.sub" → stripped "delordersub", "delord" starts at 0
  const r1 = matchSidebarLabel("del_order.sub", "delord");
  assert.equal(r1?.kind, "word-prefix");
  assert.equal(r1?.score, 65);
  // Dot then underscore
  // "schema.user_log" → stripped "schemauserlog", "schemau" starts at 0
  const r2 = matchSidebarLabel("schema.user_log", "schemau");
  assert.equal(r2?.kind, "word-prefix");
  assert.equal(r2?.score, 65);
  // All three: underscore, hyphen, dot
  // "del_order-hist.log" → stripped "delorderhistlog", "delorderh" starts at 0
  const r3 = matchSidebarLabel("del_order-hist.log", "delorderh");
  assert.equal(r3?.kind, "word-prefix");
  assert.equal(r3?.score, 65);
});

test("separator-blind substring in the middle of compound names", () => {
  // "rpro" from "user_profile" — direct includes("rpro") → false (r at 3, _ at 4)
  // Separator-blind: stripped "userprofile" → includes "rpro" → substring (55)
  assert.equal(matchSidebarLabel("user_profile", "rpro")?.kind, "substring");
  assert.equal(matchSidebarLabel("user_profile", "rpro")?.score, 55);
  // "rpr" from "user_profile" — direct includes false, stripped true
  assert.equal(matchSidebarLabel("user_profile", "rpr")?.kind, "substring");
  assert.equal(matchSidebarLabel("user_profile", "rpr")?.score, 55);
});

test("separator-blind works with dotted namespace patterns", () => {
  // "public.user_orders" → stripped "publicuserorders"
  // "publicu" crosses the first dot → separator-blind prefix
  assert.equal(matchSidebarLabel("public.user_orders", "publicu")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("public.user_orders", "publicu")?.score, 65);
  // "userord" crosses a dot + underscore → separator-blind substring
  assert.equal(matchSidebarLabel("public.user_orders", "userord")?.kind, "substring");
  assert.equal(matchSidebarLabel("public.user_orders", "userord")?.score, 55);
  // "cuser" from the middle (stripped form)
  assert.equal(matchSidebarLabel("public.user_orders", "cuser")?.kind, "substring");
  assert.equal(matchSidebarLabel("public.user_orders", "cuser")?.score, 55);
});

test("separator-blind is a no-op for labels without any separators", () => {
  // "orders" has no separators → stripped === label, separator-blind branch skipped
  assert.equal(matchSidebarLabel("orders", "elo"), null);
  assert.equal(matchSidebarLabel("customers", "cust")?.kind, "prefix");
  // Same behavior as before the change — direct matching applies
  assert.equal(matchSidebarLabel("orders", "odr")?.kind, "fuzzy");
  // A query that doesn't match via direct or separator-blind on a no-separator label
  assert.equal(matchSidebarLabel("products", "pdt")?.kind, "fuzzy");
});

test("separator-blind handles leading and trailing separators", () => {
  // Leading underscore: stripped "_del_order" = "delorder"
  assert.equal(matchSidebarLabel("_del_order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("_del_order", "delo")?.score, 65);
  // Trailing underscore: stripped "del_order_" = "delorder"
  assert.equal(matchSidebarLabel("del_order_", "delord")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del_order_", "delord")?.score, 65);
  // Leading hyphen
  assert.equal(matchSidebarLabel("-del_order", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("-del_order", "delo")?.score, 65);
  // Leading + trailing: stripped "_del_order_" = "delorder"
  assert.equal(matchSidebarLabel("_del_order_", "delo")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("_del_order_", "delo")?.score, 65);
  // Leading separator-only label: "___" → stripped "" → length 0 < query.length → no match
  assert.equal(matchSidebarLabel("___", "x"), null);
});

test("separator-blind with query equal to the fully stripped label", () => {
  // "a_b" search "ab" → abbreviation fires first: a(0 boundary) b(2 boundary) → 60
  assert.equal(matchSidebarLabel("a_b", "ab")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("a_b", "ab")?.score, 60);
  // "del_order" search "delorder" → stripped "delorder".startsWith("delorder") → 65
  // (abbreviation fails: d,e,l at 0/1/2 are NOT boundaries)
  assert.equal(matchSidebarLabel("del_order", "delorder")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("del_order", "delorder")?.score, 65);
  // "sys_user_log" search "sysuserlog" → 65 (abbreviation fails)
  assert.equal(matchSidebarLabel("sys_user_log", "sysuserlog")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("sys_user_log", "sysuserlog")?.score, 65);
  // "t_json" search "tjson" → 65 (abbreviation fails: t(0 ✓) j(2 ✓) but s,o,n not boundaries)
  assert.equal(matchSidebarLabel("t_json", "tjson")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("t_json", "tjson")?.score, 65);
  // "a.b" search "ab" → abbreviation: a(0 ✓) b(2 after . ✓) → 60, beats separator-blind
  assert.equal(matchSidebarLabel("a.b", "ab")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("a.b", "ab")?.score, 60);
});

test("direct matches always beat separator-blind counterparts", () => {
  // "del" is direct prefix (90), not separator-blind prefix (65)
  const r1 = matchSidebarLabel("del_order", "del");
  assert.equal(r1?.kind, "prefix");
  assert.equal(r1?.score, 90);
  // "ord" is direct word-prefix at the underscore boundary (80),
  // not separator-blind substring at 55
  const r2 = matchSidebarLabel("del_order", "ord");
  assert.equal(r2?.kind, "word-prefix");
  assert.equal(r2?.score, 80);
  // "_order" is a direct substring (70) because the label literally contains it
  const r3 = matchSidebarLabel("del_order", "_order");
  assert.equal(r3?.kind, "substring");
  assert.equal(r3?.score, 70);
  // "der" is a direct substring at indices 6-8 (70), not separator-blind (55)
  const r4 = matchSidebarLabel("del_order", "der");
  assert.equal(r4?.kind, "substring");
  assert.equal(r4?.score, 70);
});

test("abbreviation and word-prefix still outrank separator-blind", () => {
  // "do" for "del_order" matches as abbreviation (60) — d at start, o after _
  // separator-blind would NOT match anyway (stripped "delorder" does not contain "do"),
  // but even if it did, 60 > 55
  assert.equal(matchSidebarLabel("del_order", "do")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("del_order", "do")?.score, 60);
  // "up" for "user_profile" → abbreviation (60)
  assert.equal(matchSidebarLabel("user_profile", "up")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("user_profile", "up")?.score, 60);
  // "erp" for "user_profile" → direct fails (underscore breaks "er" + "p"),
  // stripped "userprofile" includes "erp" → separator-blind substring (55)
  assert.equal(matchSidebarLabel("user_profile", "erp")?.kind, "substring");
  assert.equal(matchSidebarLabel("user_profile", "erp")?.score, 55);
});

test("separator-blind handles edge cases correctly", () => {
  // Query longer than stripped label → no match
  assert.equal(matchSidebarLabel("del_order", "delorderextra"), null);
  // Query entirely composed of separator characters → only direct checks apply
  // "_" is a direct substring
  assert.equal(matchSidebarLabel("del_order", "_")?.kind, "substring");
  assert.equal(matchSidebarLabel("del_order", "_")?.score, 70);
  // ".__" → direct substring if present, null otherwise
  assert.equal(matchSidebarLabel("a.__b", ".__")?.kind, "substring");
  assert.equal(matchSidebarLabel("del_order", ".__"), null);
  // Empty query → null (early return in matchSidebarLabelWithRegex)
  assert.equal(matchSidebarLabel("del_order", ""), null);
});

// ── Single-character prefix patterns (e.g. "t_" prefix tables) ──

test("matches common t_xxx prefix tables via abbreviation", () => {
  // "t_json" — the single-letter prefix "t_" is a very common naming
  // convention.  "tj" picks the first char of each underscored segment.
  const r1 = matchSidebarLabel("t_json", "tj");
  assert.equal(r1?.kind, "abbreviation");
  assert.equal(r1?.score, 60);
  // "t_user" → "tu"
  const r2 = matchSidebarLabel("t_user", "tu");
  assert.equal(r2?.kind, "abbreviation");
  assert.equal(r2?.score, 60);
  // "t_order" → "to"
  const r3 = matchSidebarLabel("t_order", "to");
  assert.equal(r3?.kind, "abbreviation");
  assert.equal(r3?.score, 60);
});

test("matches t_xxx prefix tables with separator-blind substring deepening", () => {
  // "tjso" → "t_json": "t_json".startsWith("tjso") → false, includes → false.
  // Abbreviation: t at 0 ✓, j at 2 ✓, s at 3 NOT boundary → fails.
  // Fuzzy: "t_json" subsequence for "tjso" resets at _ → fails.
  // Separator-blind: stripped "tjson" starts with "tjso" → true!  The
  // stripped form is literally "tjson", and "tjso" IS a prefix of that.
  const r0 = matchSidebarLabel("t_json", "tjso");
  assert.equal(r0?.kind, "word-prefix");
  assert.equal(r0?.score, 65);
  // "tson" → "t_json": stripped "tjson".includes("tson")? NO — "tjson"
  // has 'j' between 't' and 's', so "tson" is not a contiguous substring.
  assert.equal(matchSidebarLabel("t_json", "tson"), null);
  // "tjs" → "t_json": stripped "tjson".startsWith("tjs") → true (65)
  const r2 = matchSidebarLabel("t_json", "tjs");
  assert.equal(r2?.kind, "word-prefix");
  assert.equal(r2?.score, 65);
  // "tus" → "t_user": stripped "tuser".startsWith("tus") → true (65)
  const r3 = matchSidebarLabel("t_user", "tus");
  assert.equal(r3?.kind, "word-prefix");
  assert.equal(r3?.score, 65);
});

test("t_xxx prefix tables: direct substring beats abbreviation", () => {
  // "t_j" → "t_json": "t_json" literally starts with "t_j" → prefix (90)
  const r1 = matchSidebarLabel("t_json", "t_j");
  assert.equal(r1?.kind, "prefix");
  assert.equal(r1?.score, 90);
  // "t_" → "t_json": direct prefix (90)
  const r2 = matchSidebarLabel("t_json", "t_");
  assert.equal(r2?.kind, "prefix");
  assert.equal(r2?.score, 90);
  // "json" → "t_json": word-prefix at the underscore boundary (80)
  const r3 = matchSidebarLabel("t_json", "json");
  assert.equal(r3?.kind, "word-prefix");
  assert.equal(r3?.score, 80);
  // "t_jso" — check: "t_json".startsWith("t_jso") → yes (prefix 90)
  // because "t_json" has t, _, j, s, o, n and "t_jso" is t, _, j, s, o
  assert.equal(matchSidebarLabel("t_json", "t_jso")?.kind, "prefix");
  assert.equal(matchSidebarLabel("t_json", "t_jso")?.score, 90);
});

test("matches t_xxx tables with multi-segment abbreviation", () => {
  // "t_my_json" → "tmj" picks t, m, j at boundaries → abbreviation (60)
  const r1 = matchSidebarLabel("t_my_json", "tmj");
  assert.equal(r1?.kind, "abbreviation");
  assert.equal(r1?.score, 60);
  // "t_json_item" → "tji" picks t, j, i at boundaries → abbreviation (60)
  const r2 = matchSidebarLabel("t_json_item", "tji");
  assert.equal(r2?.kind, "abbreviation");
  assert.equal(r2?.score, 60);
  // "tj" also matches "t_my_json" as abbreviation — t at 0, j at 5 (after second _)
  const r3 = matchSidebarLabel("t_my_json", "tj");
  assert.equal(r3?.kind, "abbreviation");
  assert.equal(r3?.score, 60);
});

test("common real-world table prefix patterns", () => {
  // v_ (view prefix): "v_order_detail"
  assert.equal(matchSidebarLabel("v_order_detail", "vod")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("v_order_detail", "vod")?.score, 60);
  // "vod" as separator-blind: stripped "vorderdetail".startsWith("vod") → true (65)
  // but abbreviation fires first (60).  Acceptable — both match.
  //
  // tmp_ prefix: "tmp_export_data"
  assert.equal(matchSidebarLabel("tmp_export_data", "ted")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("tmp_export_data", "ted")?.score, 60);
  // "tmpe" → stripped "tmpexportdata", startsWith "tmpe" → separator-blind prefix (65)
  // (abbreviation fails because 'p' is not at a boundary)
  assert.equal(matchSidebarLabel("tmp_export_data", "tmpe")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("tmp_export_data", "tmpe")?.score, 65);
  //
  // bak_ prefix: "bak_2024_orders"
  assert.equal(matchSidebarLabel("bak_2024_orders", "b2o")?.kind, "abbreviation");
  assert.equal(matchSidebarLabel("bak_2024_orders", "b2o")?.score, 60);
  // "bak2" → stripped "bak2024orders", startsWith "bak2" → true (65)
  // (abbreviation fails because 'a', 'k' are not at boundaries)
  assert.equal(matchSidebarLabel("bak_2024_orders", "bak2")?.kind, "word-prefix");
  assert.equal(matchSidebarLabel("bak_2024_orders", "bak2")?.score, 65);
});

test("matches slash-delimited regular expression queries case-insensitively by default", () => {
  assert.equal(matchSidebarLabel("SYS_USER_LOG", "/^sys_.*_log$/")?.kind, "regex");
  assert.equal(matchSidebarLabel("sys_user_archive", "/^sys_.*_log$/"), null);
});

test("keeps invalid regular expression queries from matching every label", () => {
  assert.equal(matchSidebarLabel("orders", "/["), null);
});

test("ordinary slash-delimited search keeps its implicit case-insensitive flags", () => {
  assert.equal(matchSidebarLabel("FOO", "/foo/m")?.kind, "regex");
});

test("explicit regex mode supports JavaScript syntax and defaults to case-insensitive", () => {
  const pattern = "^(foo|bar)[0-9]+\\w+$";
  assert.equal(matchSidebarLabel("BAR12_name", pattern, { regexMode: true })?.kind, "regex");
  assert.equal(matchSidebarLabel("baz12_name", pattern, { regexMode: true }), null);
  assert.equal(matchSidebarLabel("SYS_USER_LOG", "/^sys_.*_log/", { regexMode: true })?.kind, "regex");
});

test("explicit regex flags are respected and global or sticky tests are stable", () => {
  assert.equal(matchSidebarLabel("FOO", "/foo/m", { regexMode: true }), null);
  assert.equal(matchSidebarLabel("FOO", "/foo/i", { regexMode: true })?.kind, "regex");
  const globalMatcher = createSidebarLabelMatcher("/foo/g", { regexMode: true });
  assert.equal(globalMatcher("foo")?.kind, "regex");
  assert.equal(globalMatcher("foo")?.kind, "regex");
  const global = compileSearchRegex("/foo/g").regex!;
  assert.equal(global.lastIndex, 0);
  const stickyMatcher = createSidebarLabelMatcher("/foo/y", { regexMode: true });
  assert.equal(stickyMatcher("foo")?.kind, "regex");
  assert.equal(stickyMatcher("foo")?.kind, "regex");
});

test("invalid explicit regexes return invalid without matching", () => {
  assert.deepEqual(compileSearchRegex("/["), { regex: null, invalid: true });
  assert.equal(matchSidebarLabel("orders", "[", { regexMode: true }), null);
  assert.equal(parseSlashDelimitedRegexQuery("/[/"), null);
});

// Cross-word prefix concatenation (issue #5407)

test("matches cross-word prefix concatenation (issue #5407)", () => {
  const r1 = matchSidebarLabel("system_exception_log", "exclog");
  assert.equal(r1?.kind, "abbreviation");
  assert.equal(r1?.score, 50);
  const r2 = matchSidebarLabel("system_exception_log", "syslog");
  assert.equal(r2?.kind, "abbreviation");
  assert.equal(r2?.score, 50);
  const r3 = matchSidebarLabel("system_exception_log", "exlog");
  assert.equal(r3?.kind, "abbreviation");
  assert.equal(r3?.score, 50);
  const r4 = matchSidebarLabel("customer_order_detail", "custdet");
  assert.equal(r4?.kind, "abbreviation");
  assert.equal(r4?.score, 50);
  const r5 = matchSidebarLabel("order_payment_record", "payrec");
  assert.equal(r5?.kind, "abbreviation");
  assert.equal(r5?.score, 50);
});

test("rejects loose cross-word queries (issue #5407 negatives)", () => {
  assert.equal(matchSidebarLabel("system_exception_log", "slog"), null);
  assert.equal(matchSidebarLabel("system_exception_log", "selog"), null);
  assert.equal(matchSidebarLabel("sys_role_data_scope", "roles"), null);
  assert.equal(matchSidebarLabel("user_profile", "urf"), null);
  assert.equal(matchSidebarLabel("t_json", "tson"), null);
});

test("tokenizes camelCase identifiers for word-boundary matching", () => {
  const r1 = matchSidebarLabel("camelCaseTable", "caseTab");
  assert.equal(r1?.kind, "word-prefix");
  assert.equal(r1?.score, 80);
  const r2 = matchSidebarLabel("camelCaseTable", "camTab");
  assert.equal(r2?.kind, "abbreviation");
  assert.equal(r2?.score, 50);
  const r3 = matchSidebarLabel("camelCaseTable", "cct");
  assert.equal(r3?.kind, "abbreviation");
  assert.equal(r3?.score, 60);
  assert.equal(matchSidebarLabel("camelCaseTable", "aseTb"), null);
});

test("camelCase tokenization survives the real tree-search path (issue #5407)", () => {
  // filterSidebarTree must preserve the original label. Lowercasing
  // camelCaseTable first still finds camTab as fuzzy/40, so score ordering
  // against an existing fuzzy/40 candidate makes the boundary observable.
  const tree: TreeNode[] = [
    {
      id: "conn-1",
      label: "My Connection",
      type: "connection",
      connectionId: "conn-1",
      isExpanded: true,
      children: [
        {
          id: "db-1",
          label: "app_db",
          type: "database",
          connectionId: "conn-1",
          database: "app_db",
          isExpanded: true,
          children: [
            {
              id: "tbl-fuzzy",
              label: "camxxtab",
              type: "table",
              connectionId: "conn-1",
              database: "app_db",
              schema: "public",
            },
            {
              id: "tbl-camel",
              label: "camelCaseTable",
              type: "table",
              connectionId: "conn-1",
              database: "app_db",
              schema: "public",
            },
          ],
        },
      ],
    },
  ];

  const findTable = (nodes: TreeNode[]): TreeNode | undefined => {
    for (const node of nodes) {
      if (node.type === "table") return node;
      const found = node.children ? findTable(node.children) : undefined;
      if (found) return found;
    }
    return undefined;
  };

  // camel + Table skips Case, so it scores 50 and outranks camxxtab's fuzzy/40.
  // If callers lowercase first, both score 40 and the stable sort keeps camxxtab first.
  const camTabResults = filterSidebarTree(tree, "camTab", new Set());
  assert.deepEqual(
    camTabResults[0]?.children?.[0]?.children?.map((node) => node.label),
    ["camelCaseTable", "camxxtab"],
  );
  assert.equal(findTable(filterSidebarTree(tree, "caseTab", new Set()))?.label, "camelCaseTable");
  assert.equal(findTable(filterSidebarTree(tree, "aseTb", new Set())), undefined);

  const mixedCaseTree: TreeNode[] = [
    {
      id: "tbl-2",
      label: "UserOrderDetail",
      type: "table",
      connectionId: "conn-1",
      database: "app_db",
      schema: "public",
    },
  ];
  assert.equal(findTable(filterSidebarTree(mixedCaseTree, "userorderdetail", new Set()))?.label, "UserOrderDetail");
  assert.equal(findTable(filterSidebarTree(mixedCaseTree, "uod", new Set()))?.label, "UserOrderDetail");
});

// ── Foreground match modes (issue #13505) ──
//
// `fuzzy` is the default and must stay byte-for-byte identical to the ladder
// above. `contains` drops the subsequence/abbreviation/concatenation tiers so
// "101" no longer finds "1001". `edges` keeps only whole-label start/end
// matches so a mid-name word stops matching.

test("fuzzy remains the default match mode", () => {
  // No options at all (every pre-#13505 call site) and an explicit "fuzzy"
  // produce the same results.
  assert.equal(matchSidebarLabel("1001", "101")?.kind, "fuzzy");
  assert.equal(matchSidebarLabel("1001", "101")?.score, 40);
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: "fuzzy" })?.score, 40);
  assert.equal(matchSidebarLabel("orders", "odr")?.kind, "fuzzy");
  assert.equal(matchSidebarLabel("orders", "odr", { matchMode: "fuzzy" })?.score, 40);
  assert.equal(matchSidebarLabel("system_exception_log", "exclog")?.score, 50);
  assert.equal(matchSidebarLabel("del_order", "elo")?.score, 55);
  assert.equal(createSidebarLabelMatcher("101")("1001")?.kind, "fuzzy");

  // An out-of-union value (stale persisted state, plugin caller) must degrade to
  // the documented default, not to the stricter `contains` ladder.
  const unknownMode = "wildcard" as unknown as SidebarSearchMatchMode;
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: unknownMode })?.score, 40);
});

test("contains keeps literal containment and drops subsequence tiers", () => {
  const contains = (label: string, query: string, score?: number) => {
    const match = matchSidebarLabel(label, query, { matchMode: "contains" });
    assert.equal(match?.score, score, `${label} / ${query}`);
    return match;
  };

  // Dropped: in-order subsequences and cross-word/quasi tiers.
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: "contains" }), null);
  assert.equal(matchSidebarLabel("t_1001", "101", { matchMode: "contains" }), null);
  assert.equal(matchSidebarLabel("dwd_order_1001", "101", { matchMode: "contains" }), null);
  assert.equal(matchSidebarLabel("orders", "odr", { matchMode: "contains" }), null);
  assert.equal(matchSidebarLabel("system_exception_log", "sel", { matchMode: "contains" }), null);
  // Word-segment prefix concatenation is a fuzzy-only tier too ("exc"+skip+"log").
  assert.equal(matchSidebarLabel("system_exception_log", "exclog", { matchMode: "contains" }), null);

  // Kept: exact, prefix, word-prefix, substring — all literal contiguous text.
  assert.equal(contains("101", "101", 100)?.kind, "exact");
  assert.equal(contains("1010", "101", 90)?.kind, "prefix");
  assert.equal(contains("t_101", "101", 80)?.kind, "word-prefix");
  assert.equal(contains("dwd_order_101", "101", 80)?.kind, "word-prefix");
  assert.equal(contains("x101x", "101", 70)?.kind, "substring");
  // Strict modes are literal: separator-blind tiers belong to fuzzy only.
  assert.equal(matchSidebarLabel("del_order", "delo", { matchMode: "contains" }), null);
  assert.equal(matchSidebarLabel("del_order", "elo", { matchMode: "contains" }), null);
});

test("edges only matches whole-label start and end", () => {
  // Exact and prefix are unchanged.
  assert.equal(matchSidebarLabel("101", "101", { matchMode: "edges" })?.kind, "exact");
  assert.equal(matchSidebarLabel("101", "101", { matchMode: "edges" })?.score, 100);
  assert.equal(matchSidebarLabel("1010", "101", { matchMode: "edges" })?.kind, "prefix");

  // Direct suffix is a new tier; it cannot appear in fuzzy mode.
  assert.equal(matchSidebarLabel("t_101", "101", { matchMode: "edges" })?.kind, "suffix");
  assert.equal(matchSidebarLabel("t_101", "101", { matchMode: "edges" })?.score, 85);
  assert.equal(matchSidebarLabel("del_order", "order", { matchMode: "edges" })?.kind, "suffix");
  assert.equal(matchSidebarLabel("del_order", "order", { matchMode: "edges" })?.score, 85);

  // Mid-name words stop matching, including plain subsequences.
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("t_1001", "101", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("dwd_order_1001", "101", { matchMode: "edges" }), null);
  // `dwd_order_101` genuinely ends with the query, so `edges` keeps it as a
  // suffix: the mode anchors on the label's start/end, not on the word count.
  // Its `_detail` sibling is the mid-name case the mode rejects.
  assert.equal(matchSidebarLabel("dwd_order_101", "101", { matchMode: "edges" })?.kind, "suffix");
  assert.equal(matchSidebarLabel("dwd_order_101", "101", { matchMode: "edges" })?.score, 85);
  assert.equal(matchSidebarLabel("dwd_order_101_detail", "101", { matchMode: "edges" }), null);

  // Strict modes are literal: no separator-blind prefix/suffix variants.
  assert.equal(matchSidebarLabel("del_order", "delo", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("del_order", "elo", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("del_order", "delorder", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("del_order", "elorder", { matchMode: "edges" }), null);
  assert.equal(matchSidebarLabel("del_order", "order", { matchMode: "edges" })?.kind, "suffix");
  assert.equal(matchSidebarLabel("del_order", "order", { matchMode: "edges" })?.score, 85);
});

test("unknown match modes fall back to the fuzzy default", () => {
  // Runtime values are typed, but the matcher is a public helper; an unexpected
  // string must not silently degrade to the more restrictive `contains` ladder.
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: "bogus" as unknown as SidebarSearchMatchMode })?.kind, "fuzzy");
  assert.equal(matchSidebarLabel("1001", "101", { matchMode: "bogus" as unknown as SidebarSearchMatchMode })?.score, 40);
});

test("regex mode keeps its semantics regardless of the plain-text mode", () => {
  for (const matchMode of ["fuzzy", "contains", "edges"] as const) {
    assert.equal(matchSidebarLabel("SYS_USER_LOG", "/^sys_.*_log$/", { regexMode: true, matchMode })?.kind, "regex");
    assert.equal(matchSidebarLabel("sys_user_archive", "/^sys_.*_log$/", { regexMode: true, matchMode }), null);
    assert.equal(matchSidebarLabel("orders", "[", { regexMode: true, matchMode }), null);
  }
});

test("filterSidebarTree applies the match mode to labels, comments and aliases", () => {
  const tree: TreeNode[] = [
    {
      id: "conn-1",
      label: "My Connection",
      type: "connection",
      connectionId: "conn-1",
      isExpanded: true,
      children: [
        {
          id: "db-1",
          label: "app_db",
          type: "database",
          connectionId: "conn-1",
          database: "app_db",
          isExpanded: true,
          children: [
            { id: "tbl-1001", label: "t_1001", type: "table", connectionId: "conn-1", database: "app_db" },
            { id: "tbl-101", label: "t_101", type: "table", connectionId: "conn-1", database: "app_db" },
            { id: "tbl-comment", label: "del_order", type: "table", connectionId: "conn-1", database: "app_db", comment: "101 archive" },
            { id: "tbl-alias", label: "unrelated", type: "table", connectionId: "conn-1", database: "app_db", searchAliases: ["101"] },
          ],
        },
      ],
    },
  ];
  const labels = (mode: "fuzzy" | "contains" | "edges") => (filterSidebarTree(tree, "101", new Set(), undefined, { matchMode: mode })[0]?.children?.[0]?.children ?? []).map((node) => node.label).sort();

  // The matcher sees every label uniformly: node label, table name, comment
  // and search aliases. "del_order" only matches through its comment and
  // "unrelated" only through its alias, so they prove both paths.
  assert.deepEqual(labels("fuzzy"), ["del_order", "t_1001", "t_101", "unrelated"]);
  // `contains` drops the in-order subsequence hit on t_1001 but keeps the
  // comment and alias matches, which are literal containment.
  assert.deepEqual(labels("contains"), ["del_order", "t_101", "unrelated"]);
  // `edges` matches "101 archive" (comment prefix) and the exact alias, but
  // t_101 is a suffix match while t_1001 has no edge.
  assert.deepEqual(labels("edges"), ["del_order", "t_101", "unrelated"]);
});