diff --git a/AGENTS.md b/AGENTS.md index dc4e6adc..ba3ec800 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -112,6 +112,52 @@ behaviors are active: Full Godot static-analysis scope, static-vs-runtime boundary, and honesty signals: [`docs/godot.md`](docs/godot.md). +## Tauri IPC resolver (`codegraph-resolve`) + +The `TauriResolver` is the second concrete `FrameworkResolver` impl. It bridges the +IPC boundary a `#[tauri::command]` opens: `extract()` emits one +`tauri:invoke:` `Calls` reference per literal-argument `invoke('name')` +call site in a JS-family file, and `resolve()` binds it to the command function. +Registered **LAST** in `detect_frameworks` so the existing four resolvers' Strategy-1 +iteration order is unchanged. `confidence` is 0.9 at its single return site, so it +short-circuits before the name matcher. File: +`crates/codegraph-resolve/src/frameworks/tauri.rs`. + +- **Determinism rule.** An edge is built only when EXACTLY ONE command claims the + wire name. The roster is a `BTreeMap>` keyed by both the + snake name and its `tauri-specta` camelCase spelling, and a key claimed by a + second, DIFFERENT target is permanently poisoned (`None`) — 0 or ≥2 candidates + produce no edge. The "different target id" clause is what keeps a single-word + command, whose camel spelling equals its wire name, from poisoning itself. This + rule is the ONLY protection that exists: `Language::Rust` has no language family, + so the cross-family gates are inert for a Rust target, and `Calls` bypasses them + regardless. +- **Both text scans are lexically masked, and dropping either one is a defect.** + `rust_code_mask` gates the roster — BOTH the `#[tauri::command]` token and the + `fn ` that follows it, skipping masked bytes and further attributes in + between (a whole-span gate fails closed on the legal attribute / doc-comment / + attribute / `fn` interleaving) — plus a token boundary after `command`, because + `#[tauri::commandant]` is entirely ordinary code and the mask cannot reject it. + Without those, a commented-out or raw-string copy of the attribute promotes the + real UNATTRIBUTED same-named function: measured on the adversarial fixture, six + fabricated edges where the correct answer is one, since every masked copy mints + no node and the same-file/same-name join therefore has exactly one candidate and + it is the wrong one. `js_code_mask` plus a no-receiver guard gates the call side; + without its regex state, `/invoke('save_config')/` — a legal regex literal the + base extractor records nothing for — becomes a fabricated edge to a real command. + The same Rust mask also gates detection probe 4, without which THIS repository + self-detects as a Tauri project off the literal in its own source. +- **Known limitations, deliberate.** References are FILE-granular + (`from_node_id = file:`), so `codegraph callers get_mcp_port` answers + with `src/app.ts` rather than the enclosing function; reconstructing that id is + unsafe here because TypeScript has many declaration forms and an object-literal + call site has no enclosing function node at all. `invoke(cmdName)`, + ``invoke(`cmd_${id}`)`` and `client.invoke('x')` emit nothing, with no dynamic + sentinel — Tauri has no `codegraph audit`-style consumer that would read one. + `generate_handler![…]` is NOT parsed, so an attributed-but-unregistered command + still gets an edge (asserted deliberately). Events (`listen`/`once`) and the typed + `commands.getMcpPort()` binding path are out of scope. + ## MCP protocol surface (`codegraph-mcp`, rmcp 3.0.1) `codegraph-mcp` builds on **rmcp 3.0.1** (`crates/codegraph-mcp/Cargo.toml`, dep and dev-dep; diff --git a/crates/codegraph-resolve/src/frameworks/mod.rs b/crates/codegraph-resolve/src/frameworks/mod.rs index 4e71be08..5dff73db 100644 --- a/crates/codegraph-resolve/src/frameworks/mod.rs +++ b/crates/codegraph-resolve/src/frameworks/mod.rs @@ -19,6 +19,7 @@ pub(crate) mod godot_scene; pub mod godot_script; pub mod nestjs; pub mod react; +pub mod tauri; pub mod vue; use crate::framework::FrameworkResolver; @@ -36,6 +37,11 @@ pub fn detect_frameworks(context: &dyn ResolutionContext) -> Vec= 0.9` for any + // PRE-EXISTING reference must not change. A new candidate at the front + // could pre-empt one of the four above. + Box::new(tauri::TauriResolver::default()), ]; candidates .into_iter() diff --git a/crates/codegraph-resolve/src/frameworks/tauri.rs b/crates/codegraph-resolve/src/frameworks/tauri.rs new file mode 100644 index 00000000..6c3d3b6b --- /dev/null +++ b/crates/codegraph-resolve/src/frameworks/tauri.rs @@ -0,0 +1,2084 @@ +//! Tauri [`FrameworkResolver`] — the cross-language IPC bridge (Tier 4 item 10, +//! upstream issue #1543). +//! +//! Binds a Rust command function to the TypeScript/JavaScript call sites that +//! reach it over Tauri's IPC boundary, an edge tree-sitter cannot produce because +//! the two halves live in different languages and are joined only by a string +//! literal on the wire. +//! +//! Neither half of that join survives in the store, so both are recovered by +//! reading source (measured: the call literal is nowhere in `unresolved_refs`, +//! whose rows carry `reference_name = "invoke"` and no argument text; and +//! `nodes.decorators` is EMPTY for an attributed Rust fn, so nothing +//! distinguishes a command from any other function in the graph). Re-reading +//! source inside a resolver has direct precedent — +//! [`crate::frameworks::godot::GodotResolver`] recovers its autoload bindings by +//! re-reading every `project.godot` in `resolve()`. +//! +//! # Both text scans are lexically masked, and neither mask is optional +//! +//! [`rust_code_mask`] gates the registration side and [`js_code_mask`] the call +//! side, for DIFFERENT reasons that are worth keeping straight because a future +//! edit could drop either. +//! +//! On the RUST side the mask is the only defence that exists. A commented-out or +//! raw-string copy of the attribute mints no node, while a real UNATTRIBUTED +//! `fn` of the same name does — so a text hit inside a comment has exactly one +//! same-named candidate to bind to, the wrong one, and an `invoke('fake')` then +//! reaches a function the developer never exposed over IPC. Measured over the six +//! raw attribute hits of the adversarial fixture below: an unmasked scan yields +//! six roster keys and six fabricated edges where the correct answer is one. +//! +//! On the JS side the base extractor already discriminates every non-call shape +//! (comments, strings, templates, both regex spellings, division, JSX text and +//! attributes) and keeps the receiver in `client.invoke`. The mask is therefore +//! redundant-but-required: its obligation is to AGREE with the extractor shape by +//! shape, because a resolver at this tier cannot read the extractor's refs +//! (`FrameworkExtractionContext` carries two fields, and `ResolutionContext` has +//! no ref-facing method at all). Unmasked, a legal regex literal spelling the +//! call verbatim — `/invoke('save_config')/` — becomes a fabricated edge to a +//! real command. + +use std::collections::BTreeMap; +use std::sync::OnceLock; + +use codegraph_core::types::{EdgeKind, Language, Node, NodeKind}; + +use crate::framework::FrameworkResolver; +use crate::types::{RefView, ResolutionContext, ResolvedBy, ResolvedRef}; + +/// The Rust attribute that marks a function as an IPC command. Held as a `&str` +/// (not spelled inline) so this file's own occurrences of the literal stay inside +/// masked lexical states — see [`command_attr_code_hits`]. +const COMMAND_ATTR: &str = "tauri::command"; + +/// Namespace prefix on the call-side reference name, mirroring +/// `godot_script::AUTOLOAD_CALL_PREFIX`. +/// +/// A ref named plainly `get_mcp_port` would be indistinguishable from a genuine +/// reference to a same-named symbol, and the generic pass would then compete for +/// it: `apply_language_gate` filters cross-language candidates only for +/// `References` and `Imports`, so a bare-named TS→Rust `Calls` ref falls through +/// to the name matcher and is bound by its scoring rather than by our uniqueness +/// rule. No real symbol name contains this prefix. +pub const NAMESPACE_PREFIX: &str = "tauri:invoke:"; + +/// Config-file markers probed by [`TauriResolver::detect`], in order. Both the +/// `src-tauri/` layout `create-tauri-app` generates and the root layout, each in +/// its `.json` and `.json5` spelling. +const TAURI_CONF_MARKERS: &[&str] = &[ + "src-tauri/tauri.conf.json", + "src-tauri/tauri.conf.json5", + "tauri.conf.json", + "tauri.conf.json5", +]; + +/// The frontend package whose presence in `package.json` marks a Tauri app. +const TAURI_API_PACKAGE: &str = "@tauri-apps/api"; + +/// The call-side identifier this resolver recognises. +const INVOKE: &str = "invoke"; + +/// Path prefixes holding OTHER projects' checked-in source. Excluded from the +/// `.rs` attribute probe before the file is even read: a vendored corpus must +/// never decide the host project's framework, and unlike the lexical mask this +/// also stops a genuine (unmasked) attribute inside a fixture — the case a future +/// Tauri golden corpus would create. +const FIXTURE_PREFIXES: &[&str] = &[ + "crates/codegraph-bench/fixtures/", + "crates/codegraph-resolve/tests/fixtures/", + "reference/", +]; + +/// Wire name → its unique command node, or `None` for a POISONED key (two +/// different commands claimed the same spelling, so no edge may be built). +type Roster = BTreeMap>; + +/// Tauri IPC resolver. +/// +/// The command roster is built once per resolver instance — i.e. once per +/// `ReferenceResolver::initialize`, which is once per resolve pass — because a +/// per-reference rebuild would re-scan every `.rs` file in the project for every +/// `invoke` call site. +#[derive(Default)] +pub struct TauriResolver { + roster: OnceLock, +} + +impl FrameworkResolver for TauriResolver { + fn name(&self) -> &str { + "tauri" + } + + /// `None` = applies to ALL languages, for the same reason + /// [`crate::frameworks::godot::GodotResolver`] does: `resolve()` must read + /// `.rs` files and `extract()` must see JS/TS files, and + /// `applies_to_language` gates `extract()` by this list. [`Self::detect`] is + /// the real activation guard. + fn languages(&self) -> Option<&[Language]> { + None + } + + /// Ordered cheapest-first, returning `true` on the first hit. + /// + /// Probes 1-2 use `file_exists` rather than a `get_all_files` basename scan + /// because `tauri.conf.json` is NOT an indexed file: `get_all_files` is backed + /// by the `files` table, while `file_exists` reaches the filesystem, so it is + /// the only probe that can see the marker at all. + /// + /// Probe 4 is last because it is the expensive one — it reads every `.rs` + /// file — and it exists because it is the only probe that survives a monorepo + /// (`apps/desktop/src-tauri/tauri.conf.json`). + fn detect(&self, context: &dyn ResolutionContext) -> bool { + for marker in TAURI_CONF_MARKERS { + if context.file_exists(marker) { + return true; + } + } + if let Some(pkg) = context.read_file("package.json") { + if pkg.contains(TAURI_API_PACKAGE) { + return true; + } + } + context.get_all_files().iter().any(|f| { + is_rust_source(f) + && context + .read_file(f) + .is_some_and(|src| !command_attr_code_hits(&src).is_empty()) + }) + } + + /// Bind a `tauri:invoke:` reference to its command function. + /// + /// Confidence is 0.9 so it short-circuits Strategy 1 in `resolve_one_pure` + /// before the name-matcher: this resolver has already applied its own + /// uniqueness rule, and nothing downstream may add to or override it. That + /// matters more here than for any other resolver — `Language::Rust` has no + /// language family, so the cross-family gates are inert for a Rust target and + /// `Calls` bypasses them regardless. The roster's exactly-one-match rule is + /// the ONLY guard that exists. + fn resolve(&self, reference: &RefView, context: &dyn ResolutionContext) -> Option { + let wire = reference.reference_name.strip_prefix(NAMESPACE_PREFIX)?; + if reference.reference_kind != EdgeKind::Calls { + return None; + } + let target = self.roster(context).get(wire)?.as_ref()?; + Some(ResolvedRef { + original: reference.clone(), + target_node_id: target.id.clone(), + confidence: 0.9, + resolved_by: ResolvedBy::Framework, + }) + } + + /// Opt the namespaced call-side references through the name-exists + /// pre-filter. + /// + /// REQUIRED, and a deliberate divergence from `GodotResolver` (which returns + /// `false`). Measured: `has_any_possible_match` tests the whole name, then + /// splits on the first `.` and on `::`. `tauri:invoke:get_mcp_port` has a + /// single colon pair and no `.`, so every lookup misses and the ref is dropped + /// by the pre-filter before Strategy 1 runs. Godot's prefixed refs survive + /// only because their payload contains a `.` — an accident of the Godot name + /// shape, not a general rule. + /// + /// Prefix-gated, so it claims nothing a real symbol could be named and no + /// pre-existing reference name's pre-filter verdict changes. + fn claims_reference(&self, name: &str) -> bool { + name.starts_with(NAMESPACE_PREFIX) + } + + /// Emit one namespaced reference per literal-argument `invoke` call site in a + /// JS-family file. + fn extract( + &self, + file_path: &str, + content: &str, + _context: &crate::framework::FrameworkExtractionContext, + ) -> Option { + if !is_js_family(file_path) { + return None; + } + Some(crate::types::FrameworkResolverExtractionResult { + nodes: Vec::new(), + references: invoke_call_refs(file_path, content), + }) + } +} + +impl TauriResolver { + fn roster(&self, context: &dyn ResolutionContext) -> &Roster { + self.roster.get_or_init(|| build_roster(context)) + } +} + +// --------------------------------------------------------------------------- +// Registration side (D2): the command roster +// --------------------------------------------------------------------------- + +/// Scan every `.rs` file for `#[…tauri::command…]`-attributed functions and key +/// them by BOTH their wire name and its camelCase spelling (D4). +/// +/// `generate_handler![…]` is deliberately NOT parsed. The two failure directions +/// are not symmetric: missing the macro parse silently kills the feature for that +/// command, and the ways to miss it are numerous and realistic (a wrapped list, a +/// module-qualified entry, the bare `generate_handler!` spelling after a `use`, or +/// `collect_commands!` for tauri-specta users, where it never appears at all). +/// An over-broad roster costs at most one edge to a command that is attributed but +/// not registered — i.e. to the exact function the developer wrote and named. +/// +/// `BTreeMap` (not `HashMap`) so iteration is ordered, and the file list is sorted +/// so the outcome cannot depend on scan order. +fn build_roster(context: &dyn ResolutionContext) -> Roster { + let mut roster: Roster = BTreeMap::new(); + let mut files = context.get_all_files(); + files.sort(); + for file in files { + if !file.ends_with(".rs") { + continue; + } + let Some(src) = context.read_file(&file) else { + continue; + }; + for name in command_fn_names(&src) { + admit_command(&mut roster, context, &file, &name); + } + } + roster +} + +/// Join one attributed name to a real stored node and insert its two keys. +/// +/// The join is what turns a text hit into a genuine node id. Its three outcomes +/// are all deliberate: exactly one same-named `Function`/`Method` in that file is +/// the command; ZERO means the file is unindexed or unparsed, which contributes +/// nothing rather than a dangling target; and TWO OR MORE means the file itself +/// cannot say which function the attribute belongs to, so the key is poisoned +/// rather than guessed. +fn admit_command(roster: &mut Roster, context: &dyn ResolutionContext, file: &str, name: &str) { + let mut candidates: Vec = context + .get_nodes_in_file(file) + .into_iter() + .filter(|n| n.name == name && matches!(n.kind, NodeKind::Function | NodeKind::Method)) + .collect(); + let camel = snake_to_camel(name); + match candidates.len() { + 1 => { + let node = candidates.pop().expect("checked len"); + insert_key(roster, name.to_string(), &node); + insert_key(roster, camel, &node); + } + 0 => {} + _ => { + roster.insert(name.to_string(), None); + roster.insert(camel, None); + } + } +} + +/// Insert `key → node`, POISONING the key when it already names a DIFFERENT +/// target. +/// +/// The "different target id" clause is what keeps a single-word command +/// (`fn refresh`, whose camel spelling equals its wire name) from poisoning +/// itself: both inserts carry the same node id, so the second is a no-op. +fn insert_key(roster: &mut Roster, key: String, node: &Node) { + match roster.get(&key) { + None => { + roster.insert(key, Some(node.clone())); + } + Some(Some(existing)) if existing.id == node.id => {} + Some(Some(_)) => { + roster.insert(key, None); + } + Some(None) => {} + } +} + +/// `foo_bar` → `fooBar`, the `tauri-specta` wire rename. Leading underscores are +/// preserved so a `_private` command keeps a distinguishable key. +/// +/// Safe here in a way it would not be elsewhere: the lookup key is never an +/// arbitrary identifier from source, only ever the string literal of an `invoke` +/// call site, which by construction names a command or nothing. +fn snake_to_camel(name: &str) -> String { + let lead = name.len() - name.trim_start_matches('_').len(); + let mut out = String::with_capacity(name.len()); + out.push_str(&name[..lead]); + let mut upper_next = false; + for ch in name[lead..].chars() { + if ch == '_' { + upper_next = true; + continue; + } + if upper_next { + out.extend(ch.to_uppercase()); + upper_next = false; + } else { + out.push(ch); + } + } + out +} + +/// Byte offsets of every [`COMMAND_ATTR`] occurrence in `src` that is ordinary +/// Rust code AND ends at a token boundary. +/// +/// The boundary is not something the mask can supply: `tauri::commandant` is +/// ordinary code, so every byte of its `tauri::command` prefix masks `true` and a +/// byte-complete gate alone accepts it — measured, `#[tauri::commandant]` is one +/// of the adversarial fixture's six raw hits and it promotes a real unattributed +/// `fn`. +fn command_attr_code_hits(src: &str) -> Vec { + let mask = rust_code_mask(src); + hits_with_mask(src, &mask) +} + +fn hits_with_mask(src: &str, mask: &[bool]) -> Vec { + let bytes = src.as_bytes(); + src.match_indices(COMMAND_ATTR) + .filter(|(i, _)| { + let end = i + COMMAND_ATTR.len(); + (*i..end).all(|k| mask[k]) && ends_attribute_token(bytes.get(end)) + }) + .map(|(i, _)| i) + .collect() +} + +/// The byte after `tauri::command` in a genuine attribute: `]` closes it, `(` +/// opens its arguments (`#[tauri::command(rename_all = "snake_case")]`), `,` +/// separates it inside a list, and whitespace precedes any of those. +fn ends_attribute_token(next: Option<&u8>) -> bool { + match next { + None => false, + Some(b) => matches!(b, b']' | b'(' | b',') || b.is_ascii_whitespace(), + } +} + +/// Every command function name declared in `src`. +fn command_fn_names(src: &str) -> Vec { + let mask = rust_code_mask(src); + let bytes = src.as_bytes(); + hits_with_mask(src, &mask) + .into_iter() + .filter_map(|hit| command_fn_name_after(bytes, &mask, hit + COMMAND_ATTR.len())) + .collect() +} + +/// From the end of an accepted attribute token, walk to the `fn ` it +/// applies to. +/// +/// TWO tokens are gated, not one span: the attribute (already checked by the +/// caller) and the `fn ` that follows. Between them the walk SKIPS masked +/// bytes, further attribute tokens, `pub`/`pub(…)` and `async` — requiring the +/// whole span to be code would fail closed on legal Rust, because an +/// attribute / doc-comment / attribute / `fn` interleaving compiles. Skipping the +/// masked bytes is also what stops a commented-out `fn fake()` between the +/// attribute and the real declaration from keying the roster on `fake`. +fn command_fn_name_after(bytes: &[u8], mask: &[bool], attr_end: usize) -> Option { + let mut i = skip_trivia(bytes, mask, attr_end)?; + if bytes[i] == b'(' { + i = skip_balanced(bytes, mask, i, b'(', b')')?; + i = skip_trivia(bytes, mask, i)?; + } + if bytes[i] != b']' { + return None; + } + i += 1; + loop { + i = skip_trivia(bytes, mask, i)?; + if bytes[i] == b'#' { + if bytes.get(i + 1) != Some(&b'[') { + return None; + } + i = skip_balanced(bytes, mask, i + 1, b'[', b']')?; + continue; + } + let (word, after) = read_ident(bytes, i)?; + match word { + "pub" => { + i = after; + if let Some(j) = skip_trivia(bytes, mask, i) { + if bytes[j] == b'(' { + i = skip_balanced(bytes, mask, j, b'(', b')')?; + } + } + } + "async" => i = after, + "fn" => { + if !(i..after).all(|k| mask[k]) { + return None; + } + let n = skip_trivia(bytes, mask, after)?; + let (name, name_end) = read_ident(bytes, n)?; + if !(n..name_end).all(|k| mask[k]) { + return None; + } + return Some(name.to_string()); + } + _ => return None, + } + } +} + +/// Next index at or after `i` holding a non-whitespace CODE byte, skipping the +/// masked runs (comments and literals) in between. +fn skip_trivia(bytes: &[u8], mask: &[bool], mut i: usize) -> Option { + while i < bytes.len() && (bytes[i].is_ascii_whitespace() || !mask[i]) { + i += 1; + } + (i < bytes.len()).then_some(i) +} + +/// Index just past the `close` matching the `open` at `i`, counting only CODE +/// bytes so a bracket inside a comment or string cannot unbalance the span. +fn skip_balanced(bytes: &[u8], mask: &[bool], i: usize, open: u8, close: u8) -> Option { + let mut depth = 0usize; + let mut k = i; + while k < bytes.len() { + if mask[k] { + if bytes[k] == open { + depth += 1; + } else if bytes[k] == close { + depth -= 1; + if depth == 0 { + return Some(k + 1); + } + } + } + k += 1; + } + None +} + +fn read_ident(bytes: &[u8], i: usize) -> Option<(&str, usize)> { + if i >= bytes.len() || !is_ident_start(bytes[i]) { + return None; + } + let mut j = i; + while j < bytes.len() && is_ident_byte(bytes[j]) { + j += 1; + } + std::str::from_utf8(&bytes[i..j]).ok().map(|s| (s, j)) +} + +fn is_ident_start(b: u8) -> bool { + b.is_ascii_alphabetic() || b == b'_' || b >= 0x80 +} + +fn is_ident_byte(b: u8) -> bool { + b.is_ascii_alphanumeric() || b == b'_' || b >= 0x80 +} + +fn is_rust_source(path: &str) -> bool { + path.ends_with(".rs") && !FIXTURE_PREFIXES.iter().any(|p| path.starts_with(p)) +} + +fn is_js_family(path: &str) -> bool { + [".ts", ".tsx", ".js", ".jsx", ".mts", ".cts", ".mjs", ".cjs"] + .iter() + .any(|ext| path.ends_with(ext)) +} + +// --------------------------------------------------------------------------- +// The Rust lexical mask +// --------------------------------------------------------------------------- + +/// Byte mask over `src`: `true` where the byte is ordinary Rust code. +/// +/// One forward scan tracking the states a textual attribute match can hide in, +/// each of which was measured to fabricate an edge (or, for the last one, to lose +/// a real command) when unhandled: +/// +/// * line comments, including `///` and `//!`; +/// * block comments, **nestable** — Rust nests, unlike C++, so a C++-shaped mask +/// closes at the first `*/` and reads the trailing bytes as code; +/// * raw strings `r"…"` / `r#"…"#` and their byte forms `br##"…"##`, with the +/// HASH COUNT captured so a custom delimiter still closes correctly; +/// * normal and byte strings, `"…"` / `b"…"`, with `\` escapes; +/// * char literals with `\` escapes (`'\''`) that DECLINE to open on a lifetime +/// `'a` — a mask that treats every `'` as a delimiter masks from the lifetime +/// forward and can swallow a genuine attribute, which is a silent feature death +/// rather than a fabrication. +fn rust_code_mask(src: &str) -> Vec { + let bytes = src.as_bytes(); + let mut mask = vec![true; bytes.len()]; + let mut i = 0usize; + while i < bytes.len() { + let b = bytes[i]; + if b == b'/' && bytes.get(i + 1) == Some(&b'/') { + while i < bytes.len() && bytes[i] != b'\n' { + mask[i] = false; + i += 1; + } + continue; + } + if b == b'/' && bytes.get(i + 1) == Some(&b'*') { + i = mask_nested_block_comment(bytes, &mut mask, i); + continue; + } + if is_ident_start(b) { + if let Some((quote, hashes)) = raw_string_prefix(bytes, i) { + mask[i..quote].fill(false); + i = mask_raw_string(bytes, &mut mask, quote, hashes); + continue; + } + let (_, after) = read_ident(bytes, i).expect("ident start"); + i = after; + continue; + } + if b == b'"' { + i = mask_quoted(bytes, &mut mask, i, b'"', false); + continue; + } + if b == b'\'' { + if char_literal_end(bytes, i).is_some() { + i = mask_quoted(bytes, &mut mask, i, b'\'', true); + } else { + i += 1; + } + continue; + } + i += 1; + } + mask +} + +/// Mask a nestable block comment starting at `i` (`/*`). Returns the index just +/// past the outermost `*/`. +fn mask_nested_block_comment(bytes: &[u8], mask: &mut [bool], i: usize) -> usize { + let mut depth = 0usize; + let mut k = i; + while k < bytes.len() { + if bytes[k] == b'/' && bytes.get(k + 1) == Some(&b'*') { + depth += 1; + mask[k] = false; + mask[k + 1] = false; + k += 2; + continue; + } + if bytes[k] == b'*' && bytes.get(k + 1) == Some(&b'/') { + mask[k] = false; + mask[k + 1] = false; + k += 2; + depth -= 1; + if depth == 0 { + return k; + } + continue; + } + mask[k] = false; + k += 1; + } + bytes.len() +} + +/// If a raw-string prefix (`r"`, `r##"`, `br"`, `br##"`) starts at `i`, return +/// the index of its opening quote and its hash count. +fn raw_string_prefix(bytes: &[u8], i: usize) -> Option<(usize, usize)> { + let mut j = i; + if bytes.get(j) == Some(&b'b') { + j += 1; + } + if bytes.get(j) != Some(&b'r') { + return None; + } + j += 1; + let hash_start = j; + while bytes.get(j) == Some(&b'#') { + j += 1; + } + if bytes.get(j) != Some(&b'"') { + return None; + } + Some((j, j - hash_start)) +} + +/// Mask a raw string whose opening quote is at `quote` and which closes on a `"` +/// followed by exactly `hashes` `#`. +fn mask_raw_string(bytes: &[u8], mask: &mut [bool], quote: usize, hashes: usize) -> usize { + mask[quote] = false; + let mut k = quote + 1; + while k < bytes.len() { + if bytes[k] == b'"' && closes_raw_string(bytes, k, hashes) { + let end = (k + hashes).min(bytes.len() - 1); + mask[k..=end].fill(false); + return (k + hashes + 1).min(bytes.len()); + } + mask[k] = false; + k += 1; + } + bytes.len() +} + +fn closes_raw_string(bytes: &[u8], quote: usize, hashes: usize) -> bool { + (1..=hashes).all(|n| bytes.get(quote + n) == Some(&b'#')) +} + +/// Mask a `delim`-quoted literal starting at `start`, honouring `\` escapes. +/// +/// `reset_at_newline` is `true` only for char literals: a Rust string may legally +/// span lines, while an unclosed `'` means the disambiguation misjudged a +/// lifetime, and failing open there is safer than masking the rest of the file. +fn mask_quoted( + bytes: &[u8], + mask: &mut [bool], + start: usize, + delim: u8, + reset_at_newline: bool, +) -> usize { + mask[start] = false; + let mut i = start + 1; + while i < bytes.len() { + let b = bytes[i]; + if b == b'\n' && reset_at_newline { + return i; + } + mask[i] = false; + if b == b'\\' { + if i + 1 < bytes.len() { + mask[i + 1] = false; + } + i += 2; + continue; + } + i += 1; + if b == delim { + return i; + } + } + bytes.len() +} + +/// Index just past the closing `'` when `i` opens a genuine char literal, or +/// `None` when it opens a LIFETIME or label. +/// +/// `'\''` takes the escape branch; `'a'` is a literal because the byte after the +/// single character is the closing quote; `'a` in `&'a str` and `'static` are +/// lifetimes, so they open no state at all. +fn char_literal_end(bytes: &[u8], i: usize) -> Option { + if bytes.get(i + 1) == Some(&b'\\') { + let mut k = i + 2; + while k < bytes.len() && bytes[k] != b'\'' && bytes[k] != b'\n' { + k += 1; + } + return (bytes.get(k) == Some(&b'\'')).then_some(k + 1); + } + let rest = std::str::from_utf8(&bytes[i + 1..]).ok()?; + let ch = rest.chars().next()?; + let after = i + 1 + ch.len_utf8(); + (bytes.get(after) == Some(&b'\'')).then_some(after + 1) +} + +// --------------------------------------------------------------------------- +// Call side (D3): the `invoke('name')` call sites +// --------------------------------------------------------------------------- + +/// One `invoke(…)` call site the mask RECOGNISED, whether or not a wire name +/// could be recovered from it. +/// +/// The two levels are kept apart because "no reference emitted" conflates two +/// different verdicts: the mask saying "not a call site at all" (a comment, a +/// regex, JSX text) versus gate 4 saying "a call site with no literal to +/// recover" (`invoke(c)`, `` invoke(`cmd_${id}`) ``). Only the first is a +/// mask-agreement question against the extractor, so only the first may be +/// compared against its `invoke | calls` rows. +#[derive(Debug, Clone, PartialEq, Eq)] +struct CallSite { + line: i64, + column: i64, + wire_name: Option, +} + +/// Every recognised `invoke(…)` call site in `content`, in source order. +fn invoke_call_sites(file_path: &str, content: &str) -> Vec { + let lex = js_code_mask(content, is_jsx_capable(file_path)); + let bytes = content.as_bytes(); + let line_starts = line_starts(bytes); + let mut out = Vec::new(); + for (start, _) in content.match_indices(INVOKE) { + let end = start + INVOKE.len(); + if start > 0 && is_js_ident_byte(bytes[start - 1]) { + continue; + } + if bytes.get(end).is_some_and(|b| is_js_ident_byte(*b)) { + continue; + } + // Gate 1 — every byte of the identifier is ordinary code. Kills the line + // and block comments, the string, the template, BOTH regex spellings, and + // the two JSX shapes. + if !(start..end).all(|k| lex.mask[k]) { + continue; + } + // Gate 2 — no receiver. `client.invoke` is a call to SOME object's method + // and this resolver has no way to know the object is a Tauri façade. + if let Some(p) = prev_code_byte(bytes, &lex.mask, start) { + if bytes[p] == b'.' { + continue; + } + } + // Gate 3 — after an optional generic argument list, a call must follow. + // An import specifier never is, which is what keeps `import { invoke }` + // out. + let Some(mut j) = next_significant(bytes, &lex, end) else { + continue; + }; + if bytes[j] == b'<' { + let Some(after) = skip_balanced(bytes, &lex.mask, j, b'<', b'>') else { + continue; + }; + let Some(k) = next_significant(bytes, &lex, after) else { + continue; + }; + j = k; + } + if bytes[j] != b'(' { + continue; + } + // Gate 4 — the first argument must be a literal token the lexer CLOSED, + // so `invoke("a" + b)` and `invoke('a', {x: 'b'})` cannot smuggle a second + // literal into the wire name. + let wire_name = next_significant(bytes, &lex, j + 1) + .and_then(|arg| lex.literals.get(&arg)) + .map(|span| &content[span.0..span.1]) + .filter(|text| !text.contains("${") && is_wire_name(text)) + .map(str::to_string); + let (line, column) = line_col(&line_starts, start); + out.push(CallSite { + line, + column, + wire_name, + }); + } + out +} + +/// The namespaced references for the call sites that yielded a wire name. +/// +/// `from_node_id` is the FILE node (D6). Reconstructing the enclosing function's +/// id — the shape `GodotResolver` uses — loses here because GDScript has one +/// function form and TypeScript has many: measured, an object-literal method and +/// an object-literal arrow property both produce NO extractor reference at all and +/// their enclosing node is a `constant`, so a reconstructing scanner would have to +/// special-case a call site the extractor never modelled. And the failure mode is +/// silent — `insert_unresolved_refs` drops a ref whose source node does not exist. +fn invoke_call_refs(file_path: &str, content: &str) -> Vec { + let from_node_id = format!("file:{file_path}"); + let language = super::js_language_for(file_path); + invoke_call_sites(file_path, content) + .into_iter() + .filter_map(|site| { + let wire = site.wire_name?; + Some(RefView { + row_id: None, + from_node_id: from_node_id.clone(), + reference_name: format!("{NAMESPACE_PREFIX}{wire}"), + reference_kind: EdgeKind::Calls, + line: site.line, + column: site.column, + file_path: file_path.to_string(), + language, + is_function_ref: false, + reference_subkind: None, + }) + }) + .collect() +} + +/// A Tauri wire name: an identifier head then identifier bytes plus the +/// separators a command name may carry. +fn is_wire_name(text: &str) -> bool { + let mut chars = text.chars(); + let Some(head) = chars.next() else { + return false; + }; + if !(head.is_ascii_alphabetic() || head == '_') { + return false; + } + chars.all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | '.' | ':' | '-')) +} + +fn line_starts(bytes: &[u8]) -> Vec { + let mut starts = vec![0usize]; + for (i, b) in bytes.iter().enumerate() { + if *b == b'\n' { + starts.push(i + 1); + } + } + starts +} + +/// 1-based line and 0-based column of `offset`, matching the extractor's stored +/// convention (measured: `line` is the reference expression's line, `col` the +/// 0-based offset of its FIRST byte — which is why `client.invoke` is recorded at +/// the `c` of `client`, not at `invoke`). +fn line_col(line_starts: &[usize], offset: usize) -> (i64, i64) { + let idx = match line_starts.binary_search(&offset) { + Ok(i) => i, + Err(i) => i - 1, + }; + ((idx + 1) as i64, (offset - line_starts[idx]) as i64) +} + +/// Nearest index before `i` holding a non-whitespace CODE byte. +fn prev_code_byte(bytes: &[u8], mask: &[bool], i: usize) -> Option { + let mut k = i; + while k > 0 { + k -= 1; + if !bytes[k].is_ascii_whitespace() && mask[k] { + return Some(k); + } + } + None +} + +/// Next index at or after `i` that is either a recorded literal start or a +/// non-whitespace CODE byte. +/// +/// Skipping masked bytes is a requirement, not a convenience: without it +/// `invoke /* x */ ('save_config')` is rejected — a MISSED edge where the +/// extractor records a call, which is an S2 violation. Stopping AT a literal +/// start is what lets gate 4 see the argument at all, since a literal's own bytes +/// are masked. +fn next_significant(bytes: &[u8], lex: &JsLex, mut i: usize) -> Option { + while i < bytes.len() { + if lex.literals.contains_key(&i) { + return Some(i); + } + if !bytes[i].is_ascii_whitespace() && lex.mask[i] { + return Some(i); + } + i += 1; + } + None +} + +// --------------------------------------------------------------------------- +// The JS/TS lexical mask +// --------------------------------------------------------------------------- + +/// A JS/TS byte mask plus the span of every complete string-literal token. +struct JsLex { + mask: Vec, + /// Token start offset → (content start, content end) of a CLOSED literal. + literals: BTreeMap, +} + +/// Lex `src` into a code mask and its closed literal tokens. +/// +/// FIVE states, each required by a shape the base extractor was measured to +/// record nothing for — and this mask's obligation is to agree with it: +/// +/// 1. `//` to end of line, and `/* … */` **not** nestable (unlike Rust); +/// 2. `'…'` / `"…"` with `\` escapes, RESET at a newline so an unterminated quote +/// cannot mask the rest of the file; +/// 3. `` `…` `` spanning lines, with `${…}` interpolations scanned as CODE; +/// 4. a REGEX-literal state — `/invoke('save_config')/` is a legal regex whose +/// bytes spell the call verbatim, and without this state it becomes a +/// fabricated edge to a real command; +/// 5. JSX text runs, which are neither a string nor a comment to a naive mask. +fn js_code_mask(src: &str, jsx: bool) -> JsLex { + let bytes = src.as_bytes(); + let mut lex = JsLex { + mask: vec![true; bytes.len()], + literals: BTreeMap::new(), + }; + scan_js(bytes, &mut lex, 0, false, jsx); + lex +} + +/// Scan from `from` in code state. With `stop_at_brace`, return at the `}` that +/// closes the enclosing template interpolation or JSX expression container. +fn scan_js(bytes: &[u8], lex: &mut JsLex, from: usize, stop_at_brace: bool, jsx: bool) -> usize { + let mut i = from; + let mut brace_depth = 0usize; + let mut jsx_depth = 0usize; + while i < bytes.len() { + let b = bytes[i]; + if b == b'/' && bytes.get(i + 1) == Some(&b'/') { + while i < bytes.len() && bytes[i] != b'\n' { + lex.mask[i] = false; + i += 1; + } + continue; + } + if b == b'/' && bytes.get(i + 1) == Some(&b'*') { + i = mask_js_block_comment(bytes, lex, i); + continue; + } + if b == b'/' { + if regex_opens(bytes, &lex.mask, i) { + i = mask_js_regex(bytes, lex, i); + } else { + i += 1; + } + continue; + } + if b == b'"' || b == b'\'' { + i = mask_js_quoted(bytes, lex, i, b); + continue; + } + if b == b'`' { + i = mask_js_template(bytes, lex, i, jsx); + continue; + } + if jsx && b == b'<' && jsx_tag_opens(bytes, &lex.mask, i) { + i = scan_jsx_element(bytes, lex, i, &mut jsx_depth); + continue; + } + if b == b'{' { + brace_depth += 1; + i += 1; + continue; + } + if b == b'}' { + if stop_at_brace && brace_depth == 0 { + return i; + } + brace_depth = brace_depth.saturating_sub(1); + i += 1; + continue; + } + i += 1; + } + bytes.len() +} + +fn mask_js_block_comment(bytes: &[u8], lex: &mut JsLex, i: usize) -> usize { + let mut k = i; + lex.mask[k] = false; + lex.mask[k + 1] = false; + k += 2; + while k < bytes.len() { + let closing = bytes[k] == b'*' && bytes.get(k + 1) == Some(&b'/'); + lex.mask[k] = false; + k += 1; + if closing { + lex.mask[k] = false; + return k + 1; + } + } + bytes.len() +} + +/// Mask a `'`/`"` literal, recording it as a closed token only when it really +/// closes on its own line. +fn mask_js_quoted(bytes: &[u8], lex: &mut JsLex, start: usize, delim: u8) -> usize { + lex.mask[start] = false; + let mut i = start + 1; + while i < bytes.len() { + let b = bytes[i]; + if b == b'\n' { + return i; + } + lex.mask[i] = false; + if b == b'\\' { + if i + 1 < bytes.len() { + lex.mask[i + 1] = false; + } + i += 2; + continue; + } + i += 1; + if b == delim { + lex.literals.insert(start, (start + 1, i - 1)); + return i; + } + } + bytes.len() +} + +/// Mask a template literal, scanning each `${…}` interpolation as CODE. +/// +/// The recorded content span covers the whole raw text between the backticks, so +/// gate 4's `${` check sees an interpolation and declines the call site rather +/// than inventing a name from a fragment. +fn mask_js_template(bytes: &[u8], lex: &mut JsLex, start: usize, jsx: bool) -> usize { + lex.mask[start] = false; + let mut i = start + 1; + while i < bytes.len() { + let b = bytes[i]; + if b == b'\\' { + lex.mask[i] = false; + if i + 1 < bytes.len() { + lex.mask[i + 1] = false; + } + i += 2; + continue; + } + if b == b'$' && bytes.get(i + 1) == Some(&b'{') { + lex.mask[i] = false; + lex.mask[i + 1] = false; + let close = scan_js(bytes, lex, i + 2, true, jsx); + if close >= bytes.len() { + return bytes.len(); + } + lex.mask[close] = false; + i = close + 1; + continue; + } + lex.mask[i] = false; + i += 1; + if b == b'`' { + lex.literals.insert(start, (start + 1, i - 1)); + return i; + } + } + bytes.len() +} + +/// Does the `/` at `i` open a regex literal rather than a division? +/// +/// Division after `)`, `]`, `}`, an identifier or number byte, and after a `++` / +/// `--`; a regex otherwise. The KEYWORD exception matters: `return /re/` is a +/// regex even though `return` ends in an identifier byte, and treating it as +/// division would leave the regex body unmasked — the fabrication this state +/// exists to prevent. Where the shape stays undecidable the `/` is treated as +/// opening a regex, which masks real code and loses a call site (visible as an +/// S2 failure) instead of inventing one (invisible). +fn regex_opens(bytes: &[u8], mask: &[bool], i: usize) -> bool { + let Some(p) = prev_code_byte(bytes, mask, i) else { + return true; + }; + let b = bytes[p]; + if is_js_ident_byte(b) { + let mut s = p; + while s > 0 && is_js_ident_byte(bytes[s - 1]) { + s -= 1; + } + return matches!( + std::str::from_utf8(&bytes[s..=p]).unwrap_or_default(), + "return" + | "typeof" + | "case" + | "in" + | "of" + | "do" + | "else" + | "yield" + | "await" + | "delete" + | "void" + | "instanceof" + | "new" + | "throw" + ); + } + if matches!(b, b')' | b']' | b'}') { + return false; + } + if matches!(b, b'+' | b'-') && p > 0 && bytes[p - 1] == b { + return false; + } + true +} + +/// Mask a regex literal, honouring `\` escapes and character classes (a `/` +/// inside `[…]` does not close it). A regex cannot span lines, so a newline ends +/// the state and leaves the remaining bytes as code. +fn mask_js_regex(bytes: &[u8], lex: &mut JsLex, start: usize) -> usize { + lex.mask[start] = false; + let mut i = start + 1; + let mut in_class = false; + while i < bytes.len() { + let b = bytes[i]; + if b == b'\n' { + return i; + } + lex.mask[i] = false; + if b == b'\\' { + if i + 1 < bytes.len() { + lex.mask[i + 1] = false; + } + i += 2; + continue; + } + i += 1; + match b { + b'[' => in_class = true, + b']' => in_class = false, + b'/' if !in_class => return i, + _ => {} + } + } + bytes.len() +} + +/// Does the `<` at `i` open a JSX element rather than a comparison or a generic +/// argument list? +/// +/// The distinction is load-bearing in both directions: reading `Promise` +/// as JSX would mask the real code that follows it (a lost edge), while reading +/// `

` as a comparison leaves JSX text unmasked (a fabricated edge, which the +/// measured fixture contains). An identifier, `)` or `]` before the `<` means a +/// comparison or a type argument list — except after the keywords that can only +/// be followed by an expression. +fn jsx_tag_opens(bytes: &[u8], mask: &[bool], i: usize) -> bool { + let after = match bytes.get(i + 1) { + Some(b) => *b, + None => return false, + }; + if !(after.is_ascii_alphabetic() || after == b'_' || after == b'/' || after == b'>') { + return false; + } + let Some(p) = prev_code_byte(bytes, mask, i) else { + return true; + }; + let b = bytes[p]; + if is_js_ident_byte(b) { + let mut s = p; + while s > 0 && is_js_ident_byte(bytes[s - 1]) { + s -= 1; + } + return matches!( + std::str::from_utf8(&bytes[s..=p]).unwrap_or_default(), + "return" | "case" | "default" | "yield" | "await" | "do" | "else" | "in" | "of" + ); + } + matches!( + b, + b'(' | b'{' | b'}' | b'[' | b',' | b';' | b'=' | b':' | b'?' | b'&' | b'|' | b'!' | b'>' + ) +} + +/// Scan one JSX tag from its `<`, then mask the text run that follows it when the +/// scan is still inside an element. +/// +/// `jsx_depth` is what makes nesting correct: text after a nested `` is still +/// its parent's text and must stay masked, while text after the OUTERMOST closing +/// tag is ordinary code and must not be — the fixture's `.tsx` has a real +/// `invoke` call on the line after a closed element, so masking past it would be +/// an S2 failure. +fn scan_jsx_element(bytes: &[u8], lex: &mut JsLex, start: usize, jsx_depth: &mut usize) -> usize { + let closing = bytes.get(start + 1) == Some(&b'/'); + let mut i = start + 1; + let mut self_closing = false; + while i < bytes.len() { + let b = bytes[i]; + if b == b'"' || b == b'\'' { + i = mask_js_quoted(bytes, lex, i, b); + continue; + } + if b == b'{' { + let close = scan_js(bytes, lex, i + 1, true, true); + if close >= bytes.len() { + return bytes.len(); + } + i = close + 1; + continue; + } + if b == b'/' && bytes.get(i + 1) == Some(&b'>') { + self_closing = true; + i += 2; + break; + } + if b == b'>' { + i += 1; + break; + } + i += 1; + } + if closing { + *jsx_depth = jsx_depth.saturating_sub(1); + } else if !self_closing { + *jsx_depth += 1; + } + if *jsx_depth == 0 { + return i; + } + while i < bytes.len() && bytes[i] != b'<' { + lex.mask[i] = false; + i += 1; + } + i +} + +fn is_js_ident_byte(b: u8) -> bool { + b.is_ascii_alphanumeric() || b == b'_' || b == b'$' || b >= 0x80 +} + +fn is_jsx_capable(path: &str) -> bool { + path.ends_with(".tsx") || path.ends_with(".jsx") +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::types::ImportMapping; + + /// This file's own bytes, and the integration test's, read at compile time so + /// the guard below cannot drift away from the files it guards. + const SELF_SRC: &str = include_str!("tauri.rs"); + const IPC_TEST_SRC: &str = include_str!("../../tests/tauri_ipc.rs"); + + /// The adversarial registration-side fixture — measured legal Rust + /// (`rustc --edition 2021 --emit=metadata`, exit 0) carrying SIX raw + /// `tauri::command` hits of which exactly ONE is a genuine attribute on a + /// genuine command. `tests/tauri_ipc.rs` holds the same bytes for the + /// end-to-end edge count, and both sides assert the raw hit count so the two + /// copies cannot drift apart unnoticed. + const ADVERSARIAL: &str = r####"/* outer /* inner */ #[tauri::command] +fn nested_fake() -> u16 { 0 } */ +const D1: &str = r##" +#[tauri::command] +fn hash_fake() -> u16 { 1 } +"##; +const D2: &[u8] = br##" +#[tauri::command] +fn byteraw_fake() -> u16 { 2 } +"##; +const D3: &[u8] = b"#[tauri::command] fn bytestr_fake() -> u16 { 3 }"; +fn life<'a>(s: &'a str) -> &'a str { s } +const Q: char = '\''; +#[tauri::commandant] +fn attr_boundary_fake() -> u16 { 4 } +fn nested_fake() -> u16 { 40 } +fn hash_fake() -> u16 { 41 } +fn byteraw_fake() -> u16 { 42 } +fn bytestr_fake() -> u16 { 43 } +#[tauri::command] +fn real_cmd() -> u16 { 8111 } +"####; + + #[derive(Default)] + struct TestContext { + files: BTreeMap, + nodes: Vec, + } + + impl TestContext { + fn with_file(mut self, path: &str, content: &str) -> Self { + self.files.insert(path.to_string(), content.to_string()); + self + } + + fn with_fn(mut self, file: &str, name: &str, line: i64) -> Self { + self.nodes.push(node(file, name, line)); + self + } + } + + fn node(file: &str, name: &str, line: i64) -> Node { + Node { + id: format!("function:{file}:{name}:{line}"), + kind: NodeKind::Function, + name: name.to_string(), + qualified_name: name.to_string(), + file_path: file.to_string(), + language: Language::Rust, + start_line: line, + end_line: line, + start_column: 0, + end_column: 0, + docstring: None, + signature: None, + visibility: None, + is_exported: false, + is_async: false, + is_static: false, + is_abstract: false, + decorators: Vec::new(), + type_parameters: Vec::new(), + return_type: None, + updated_at: 0, + } + } + + impl ResolutionContext for TestContext { + fn get_nodes_in_file(&self, file_path: &str) -> Vec { + self.nodes + .iter() + .filter(|n| n.file_path == file_path) + .cloned() + .collect() + } + fn get_nodes_by_name(&self, name: &str) -> Vec { + self.nodes + .iter() + .filter(|n| n.name == name) + .cloned() + .collect() + } + fn get_nodes_by_qualified_name(&self, _qualified_name: &str) -> Vec { + Vec::new() + } + fn get_nodes_by_kind(&self, kind: NodeKind) -> Vec { + self.nodes + .iter() + .filter(|n| n.kind == kind) + .cloned() + .collect() + } + fn file_exists(&self, file_path: &str) -> bool { + self.files.contains_key(file_path) + } + fn read_file(&self, file_path: &str) -> Option { + self.files.get(file_path).cloned() + } + fn get_project_root(&self) -> &str { + "/project" + } + fn get_all_files(&self) -> Vec { + self.files.keys().cloned().collect() + } + fn get_nodes_by_lower_name(&self, lower_name: &str) -> Vec { + self.nodes + .iter() + .filter(|n| n.name.to_lowercase() == lower_name) + .cloned() + .collect() + } + fn get_node_by_id(&self, id: &str) -> Option { + self.nodes.iter().find(|n| n.id == id).cloned() + } + fn get_import_mappings(&self, _file_path: &str, _language: Language) -> Vec { + Vec::new() + } + } + + /// The attribute spelled at run time, so these tests add no code-position + /// occurrence of the literal they are testing. + fn attr() -> String { + format!("#[{COMMAND_ATTR}]") + } + + fn roster_keys(ctx: &TestContext) -> Vec { + build_roster(ctx).into_keys().collect() + } + + fn live_keys(ctx: &TestContext) -> Vec { + build_roster(ctx) + .into_iter() + .filter_map(|(k, v)| v.map(|_| k)) + .collect() + } + + // ----------------------------------------------------------------------- + // T0b / T3 — probe 4 must not make this repository self-detect + // ----------------------------------------------------------------------- + + /// Both files carry the raw literal (in a doc comment, a `const`, and test + /// fixtures), so a bare `contains` scan over `.rs` files turns codegraph-rust + /// into a self-detecting Tauri project — which would destroy the premise that + /// `detect()` is false for all sixteen golden corpora. + #[test] + fn tauri_probe4_masked_scan_finds_no_code_hit_in_own_sources() { + assert!( + SELF_SRC.matches(COMMAND_ATTR).count() >= 1, + "the guard is pointless unless the literal really occurs here" + ); + assert!( + IPC_TEST_SRC.matches(COMMAND_ATTR).count() >= 1, + "the guard is pointless unless the literal really occurs in the test" + ); + + assert_eq!( + command_attr_code_hits(SELF_SRC), + Vec::::new(), + "frameworks/tauri.rs must have NO code-position occurrence" + ); + assert_eq!( + command_attr_code_hits(IPC_TEST_SRC), + Vec::::new(), + "tests/tauri_ipc.rs must have NO code-position occurrence" + ); + } + + /// The counter-test: a mask that failed closed on genuine Tauri source would + /// be a worse bug than the one it fixes. + #[test] + fn tauri_probe4_masked_scan_still_sees_genuine_attributes() { + let src = format!("{}\nfn get_mcp_port() -> u16 {{ 8111 }}\n", attr()); + assert_eq!(command_attr_code_hits(&src).len(), 1); + } + + /// A vendored corpus must never decide the host project's framework, and the + /// mask cannot help here: the planted attribute is genuine, unmasked code. + #[test] + fn tauri_probe4_excludes_fixture_corpora() { + let planted = format!("{}\nfn vendored() -> u16 {{ 0 }}\n", attr()); + for prefix in FIXTURE_PREFIXES { + let ctx = + TestContext::default().with_file(&format!("{prefix}vendor/main.rs"), &planted); + assert!( + !TauriResolver::default().detect(&ctx), + "a `{prefix}` file must not flip detect()" + ); + } + } + + // ----------------------------------------------------------------------- + // T3 — detection: one positive per probe, plus two negatives + // ----------------------------------------------------------------------- + + #[test] + fn tauri_detect_probe1_src_tauri_conf() { + for marker in ["src-tauri/tauri.conf.json", "src-tauri/tauri.conf.json5"] { + let ctx = TestContext::default().with_file(marker, "{}"); + assert!(TauriResolver::default().detect(&ctx), "{marker}"); + } + } + + #[test] + fn tauri_detect_probe2_root_conf() { + for marker in ["tauri.conf.json", "tauri.conf.json5"] { + let ctx = TestContext::default().with_file(marker, "{}"); + assert!(TauriResolver::default().detect(&ctx), "{marker}"); + } + } + + #[test] + fn tauri_detect_probe3_package_json_api_dependency() { + let ctx = TestContext::default().with_file( + "package.json", + r#"{"dependencies":{"@tauri-apps/api":"^2.0.0"}}"#, + ); + assert!(TauriResolver::default().detect(&ctx)); + } + + #[test] + fn tauri_detect_probe4_rust_attribute() { + let ctx = TestContext::default().with_file( + "src-tauri/src/main.rs", + &format!("{}\nfn get_mcp_port() -> u16 {{ 8111 }}\n", attr()), + ); + assert!(TauriResolver::default().detect(&ctx)); + } + + /// The token boundary's own guard, and the place it is genuinely load-bearing: + /// probe 4 has no following-token structure to lean on, so without the boundary + /// check a file whose only occurrence is `tauri::commandant` — entirely ordinary + /// code the mask cannot reject — flips `detect()` for the whole project. + #[test] + fn tauri_detect_negative_token_boundary_collision() { + let ctx = TestContext::default().with_file( + "src/lib.rs", + &format!( + "mod tauri_mod {{ pub fn commandant() -> u16 {{ 7 }} }}\n\ + use tauri_mod as {COMMAND_ATTR}ant_alias;\n" + ), + ); + assert_eq!( + ctx.read_file("src/lib.rs") + .expect("fixture") + .matches(COMMAND_ATTR) + .count(), + 1, + "the guard is pointless unless the raw literal occurs" + ); + assert!(!TauriResolver::default().detect(&ctx)); + } + + #[test] + fn tauri_detect_negative_empty_project() { + assert!(!TauriResolver::default().detect(&TestContext::default())); + } + + /// Mirrors THIS repository: plain Rust, an `invoke`-named function, no marker. + #[test] + fn tauri_detect_negative_plain_rust_with_invoke_fn() { + let ctx = TestContext::default() + .with_file("src/lib.rs", "fn invoke(name: &str) -> u16 { 0 }\n") + .with_file("package.json", r#"{"dependencies":{"react":"18"}}"#); + assert!(!TauriResolver::default().detect(&ctx)); + } + + // ----------------------------------------------------------------------- + // T4 — the roster accept-shape (D2) + // ----------------------------------------------------------------------- + + #[test] + fn tauri_roster_accepts_all_four_attribute_arrangements() { + let src = format!( + "{a}\nfn a_cmd() -> u16 {{ 0 }}\n\ + {a}\npub async fn b_cmd(x: String) -> bool {{ x.is_empty() }}\n\ + #[specta::specta]\n{a}\nfn c_cmd() -> u16 {{ 0 }}\n\ + {a}\n#[specta::specta]\nfn d_cmd() -> u16 {{ 0 }}\n", + a = attr() + ); + assert_eq!( + command_fn_names(&src), + vec!["a_cmd", "b_cmd", "c_cmd", "d_cmd"] + ); + } + + #[test] + fn tauri_roster_accepts_attribute_arguments() { + let src = format!( + "#[{COMMAND_ATTR}(rename_all = \"snake_case\")]\nfn with_args() -> u16 {{ 0 }}\n" + ); + assert_eq!(command_fn_names(&src), vec!["with_args"]); + } + + #[test] + fn tauri_roster_accepts_pub_crate_visibility() { + let src = format!("{}\npub(crate) fn scoped() -> u16 {{ 0 }}\n", attr()); + assert_eq!(command_fn_names(&src), vec!["scoped"]); + } + + #[test] + fn tauri_roster_negative_unattributed_fn() { + assert!(command_fn_names("fn plain() -> u16 { 0 }\n").is_empty()); + } + + /// An attribute whose name has no matching node in that file contributes + /// nothing rather than a dangling target. + #[test] + fn tauri_roster_negative_no_matching_node_in_file() { + let ctx = TestContext::default().with_file( + "src/main.rs", + &format!("{}\nfn ghost() -> u16 {{ 0 }}\n", attr()), + ); + assert!(roster_keys(&ctx).is_empty()); + } + + /// A `use` of the attribute path is not an attribute: the following byte is a + /// boundary, but no `fn` declaration follows the token. + #[test] + fn tauri_roster_negative_use_statement() { + let src = format!("use {COMMAND_ATTR};\nfn unrelated() -> u16 {{ 0 }}\n"); + assert!(command_fn_names(&src).is_empty()); + } + + // ----------------------------------------------------------------------- + // T4b — the roster's lexical isolation. The ONLY defence on the Rust side. + // ----------------------------------------------------------------------- + + /// Every masked copy of the attribute mints no node, while the real + /// UNATTRIBUTED function does — so a naive scan's same-file/same-name join has + /// exactly one candidate per fake name and it is the wrong one. Measured: a + /// raw scan yields six keys here; the masked, boundary-checked scan yields one. + #[test] + fn tauri_roster_masked_shapes_yield_only_the_real_command() { + assert_eq!( + ADVERSARIAL.matches(COMMAND_ATTR).count(), + 6, + "the fixture must still carry six raw hits" + ); + assert_eq!(command_fn_names(ADVERSARIAL), vec!["real_cmd"]); + } + + #[test] + fn tauri_roster_negative_a_nested_block_comment() { + let src = format!( + "/* outer /* inner */ {a}\nfn nested_fake() -> u16 {{ 0 }} */\n\ + fn nested_fake() -> u16 {{ 40 }}\n", + a = attr() + ); + assert!(command_fn_names(&src).is_empty()); + } + + #[test] + fn tauri_roster_negative_b_hash_raw_string() { + let src = format!( + "const D1: &str = r##\"\n{a}\nfn hash_fake() -> u16 {{ 1 }}\n\"##;\n\ + fn hash_fake() -> u16 {{ 41 }}\n", + a = attr() + ); + assert!(command_fn_names(&src).is_empty()); + } + + #[test] + fn tauri_roster_negative_c_byte_raw_string() { + let src = format!( + "const D2: &[u8] = br##\"\n{a}\nfn byteraw_fake() -> u16 {{ 2 }}\n\"##;\n\ + fn byteraw_fake() -> u16 {{ 42 }}\n", + a = attr() + ); + assert!(command_fn_names(&src).is_empty()); + } + + #[test] + fn tauri_roster_negative_d_byte_string() { + let src = format!( + "const D3: &[u8] = b\"{a} fn bytestr_fake() -> u16 {{ 3 }}\";\n\ + fn bytestr_fake() -> u16 {{ 43 }}\n", + a = attr() + ); + assert!(command_fn_names(&src).is_empty()); + } + + /// The one negative the mask CANNOT catch: every byte masks `true`, so gate 1 + /// alone accepts it and only the token boundary rejects it. + #[test] + fn tauri_roster_negative_e_token_boundary_commandant() { + let src = format!("#[{COMMAND_ATTR}ant]\nfn attr_boundary_fake() -> u16 {{ 4 }}\n"); + assert!(command_fn_names(&src).is_empty()); + } + + /// What gate 2 buys: the attribute is genuine code, so gate 1 admits it, and a + /// scan that took the first textual `fn` would key the roster on `fake`. + #[test] + fn tauri_roster_negative_f_comment_hidden_fn() { + let src = format!( + "{a}\n// fn fake() -> u16 {{ 0 }}\npub fn real_one() -> u16 {{ 1 }}\n\ + fn fake() -> u16 {{ 42 }}\n", + a = attr() + ); + assert_eq!(command_fn_names(&src), vec!["real_one"]); + } + + /// A mask that opens a char state on every `'` masks from the lifetime forward + /// and swallows the genuine attribute — a silent feature death. + #[test] + fn tauri_roster_positive_g_lifetime_does_not_open_char_state() { + let src = format!( + "fn life<'a>(s: &'a str) -> &'a str {{ s }}\n{a}\nfn real_cmd() -> u16 {{ 8111 }}\n", + a = attr() + ); + assert_eq!(command_fn_names(&src), vec!["real_cmd"]); + } + + #[test] + fn tauri_roster_positive_h_escaped_quote_char_literal() { + let src = format!( + "const Q: char = '\\'';\n{a}\nfn real_cmd() -> u16 {{ 8111 }}\n", + a = attr() + ); + assert_eq!(command_fn_names(&src), vec!["real_cmd"]); + } + + /// Legal Rust, so a whole-span mask gate fails closed on real code. + #[test] + fn tauri_roster_positive_i_doc_comment_interleave() { + let src = format!( + "{a}\n/// Returns the MCP port.\n#[allow(dead_code)]\n\ + pub async fn get_mcp_port() -> u16 {{ 8111 }}\n", + a = attr() + ); + assert_eq!(command_fn_names(&src), vec!["get_mcp_port"]); + } + + // ----------------------------------------------------------------------- + // T5 — the poisoning rule (D4) + // ----------------------------------------------------------------------- + + #[test] + fn tauri_roster_poisons_same_name_commands_in_two_files() { + let src = format!("{}\nfn save() -> u16 {{ 0 }}\n", attr()); + let ctx = TestContext::default() + .with_file("a.rs", &src) + .with_fn("a.rs", "save", 2) + .with_file("b.rs", &src) + .with_fn("b.rs", "save", 2); + assert_eq!(roster_keys(&ctx), vec!["save".to_string()]); + assert!(live_keys(&ctx).is_empty(), "the key must be POISONED"); + } + + #[test] + fn tauri_roster_poisons_only_the_ambiguous_camel_spelling() { + let ctx = TestContext::default() + .with_file( + "a.rs", + &format!( + "{a}\nfn get_mcp_port() -> u16 {{ 0 }}\n{a}\nfn getMcpPort() -> u16 {{ 1 }}\n", + a = attr() + ), + ) + .with_fn("a.rs", "get_mcp_port", 2) + .with_fn("a.rs", "getMcpPort", 4); + assert_eq!(live_keys(&ctx), vec!["get_mcp_port".to_string()]); + assert_eq!( + build_roster(&ctx).get("getMcpPort"), + Some(&None), + "the camel spelling is ambiguous and must be poisoned" + ); + } + + /// The plan's most plausible lazy-implementation bug: without the "different + /// target id" clause every single-word command poisons itself. + #[test] + fn tauri_roster_single_word_command_is_not_self_poisoned() { + let ctx = TestContext::default() + .with_file( + "a.rs", + &format!("{}\nfn refresh() -> u16 {{ 0 }}\n", attr()), + ) + .with_fn("a.rs", "refresh", 2); + assert_eq!(live_keys(&ctx), vec!["refresh".to_string()]); + } + + #[test] + fn tauri_roster_camel_key_reaches_the_same_node() { + let ctx = TestContext::default() + .with_file( + "a.rs", + &format!("{}\nfn get_mcp_port() -> u16 {{ 0 }}\n", attr()), + ) + .with_fn("a.rs", "get_mcp_port", 2); + let roster = build_roster(&ctx); + let snake = roster.get("get_mcp_port").cloned().flatten(); + let camel = roster.get("getMcpPort").cloned().flatten(); + assert!(snake.is_some()); + assert_eq!(snake.map(|n| n.id), camel.map(|n| n.id)); + } + + /// Two same-named functions in ONE file leave the attribute unable to say + /// which it belongs to, so the key is poisoned rather than guessed. + #[test] + fn tauri_roster_poisons_ambiguous_same_file_nodes() { + let ctx = TestContext::default() + .with_file("a.rs", &format!("{}\nfn dup() -> u16 {{ 0 }}\n", attr())) + .with_fn("a.rs", "dup", 2) + .with_fn("a.rs", "dup", 9); + assert!(live_keys(&ctx).is_empty()); + } + + #[test] + fn tauri_snake_to_camel_shapes() { + assert_eq!(snake_to_camel("get_mcp_port"), "getMcpPort"); + assert_eq!(snake_to_camel("refresh"), "refresh"); + assert_eq!(snake_to_camel("getMcpPort"), "getMcpPort"); + assert_eq!(snake_to_camel("_private_cmd"), "_privateCmd"); + } + + // ----------------------------------------------------------------------- + // T6 — `claims_reference` is prefix-gated + // ----------------------------------------------------------------------- + + #[test] + fn tauri_claims_only_the_namespaced_prefix() { + let r = TauriResolver::default(); + assert!(r.claims_reference("tauri:invoke:get_mcp_port")); + assert!(!r.claims_reference(INVOKE)); + assert!(!r.claims_reference("get_mcp_port")); + assert!(!r.claims_reference("")); + } + + #[test] + fn tauri_resolver_identity() { + let r = TauriResolver::default(); + assert_eq!(r.name(), "tauri"); + assert!(r.languages().is_none()); + } + + // ----------------------------------------------------------------------- + // The Rust mask, state by state + // ----------------------------------------------------------------------- + + /// `src` with every masked byte replaced by a space, so a test can assert the + /// exact code/non-code split rather than a boolean. + fn masked_text(src: &str) -> String { + let mask = rust_code_mask(src); + src.bytes() + .zip(mask) + .map(|(b, code)| if code { b as char } else { ' ' }) + .collect() + } + + #[test] + fn rust_mask_line_and_nested_block_comments() { + assert_eq!(masked_text("a // b\nc"), "a \nc"); + assert_eq!( + masked_text("a /* x /* y */ z */ b"), + "a b" + ); + } + + #[test] + fn rust_mask_raw_strings_capture_hash_count() { + assert_eq!(masked_text("r##\"a\"#b\"##c"), " c"); + assert_eq!(masked_text("br#\"x\"#y"), " y"); + assert_eq!(masked_text("r\"x\"y"), " y"); + } + + /// The `b` of a byte string stays marked as code — it is consumed as an + /// identifier before the string state opens — and that is irrelevant to every + /// gate, which tests the CONTENT bytes. What matters is that the content is + /// masked, including an escaped quote that must not close the literal early. + #[test] + fn rust_mask_byte_and_normal_strings() { + assert_eq!(masked_text("b\"x\"y"), "b y"); + assert_eq!(masked_text("\"a\\\"b\"c"), " c"); + } + + #[test] + fn rust_mask_lifetime_is_not_a_char_literal() { + assert_eq!(masked_text("&'a str; x"), "&'a str; x"); + assert_eq!(masked_text("'static X"), "'static X"); + } + + #[test] + fn rust_mask_char_literals_including_escapes() { + assert_eq!(masked_text("'\\'' x"), " x"); + assert_eq!(masked_text("'a' x"), " x"); + } + + #[test] + fn rust_mask_multiline_string_is_not_reset_at_newline() { + assert_eq!(masked_text("\"a\nb\" c"), " c"); + } + + #[test] + fn ends_attribute_token_accepts_only_boundaries() { + assert!(ends_attribute_token(Some(&b']'))); + assert!(ends_attribute_token(Some(&b'('))); + assert!(ends_attribute_token(Some(&b','))); + assert!(ends_attribute_token(Some(&b' '))); + assert!(ends_attribute_token(Some(&b'\n'))); + assert!(!ends_attribute_token(Some(&b'a'))); + assert!(!ends_attribute_token(None)); + } + + // ----------------------------------------------------------------------- + // T7 — the JS/TS mask, one test per lexer state + // ----------------------------------------------------------------------- + + fn js_masked_text(src: &str, jsx: bool) -> String { + let lex = js_code_mask(src, jsx); + src.bytes() + .zip(lex.mask) + .map(|(b, code)| if code { b as char } else { ' ' }) + .collect() + } + + #[test] + fn js_mask_state1_comments_are_not_nestable() { + assert_eq!(js_masked_text("a // b\nc", false), "a \nc"); + assert_eq!( + js_masked_text("a /* x /* y */ z", false), + "a z" + ); + } + + #[test] + fn js_mask_state2_quotes_reset_at_newline() { + assert_eq!(js_masked_text("a 'b' c", false), "a c"); + assert_eq!(js_masked_text("a 'b\nc", false), "a \nc"); + } + + #[test] + fn js_mask_state3_template_interpolation_stays_code() { + assert_eq!(js_masked_text("`a${b}c`", false), " b "); + assert_eq!(js_masked_text("`a\nb` c", false), " c"); + } + + #[test] + fn js_mask_state4_regex_versus_division() { + assert_eq!(js_masked_text("x = /a+/; y", false), "x = ; y"); + assert_eq!( + js_masked_text("q = a / b, r = a /2/ b;", false), + "q = a / b, r = a /2/ b;" + ); + assert_eq!(js_masked_text("return /a/;", false), "return ;"); + assert_eq!(js_masked_text("x = /a[/]b/; y", false), "x = ; y"); + } + + #[test] + fn js_mask_state5_jsx_text_and_attributes() { + let src = "const a =

invoke('x')

;"; + assert_eq!(js_masked_text(src, true), "const a =

;"); + // A generic argument list is NOT a JSX element: reading it as one would + // mask the real code that follows. + assert_eq!( + js_masked_text("const a: Promise = f(); invoke('x');", true), + "const a: Promise = f(); invoke( );" + ); + } + + #[test] + fn js_mask_state5_jsx_nesting_ends_at_the_outermost_close() { + let src = "const a =

tuv

; invoke('x');"; + assert_eq!( + js_masked_text(src, true), + "const a =

; invoke( );" + ); + } + + #[test] + fn js_mask_literal_spans_are_recorded_only_when_closed() { + let lex = js_code_mask("a 'b' 'c\n", false); + assert_eq!(lex.literals.get(&2), Some(&(3, 4))); + assert_eq!( + lex.literals.get(&6), + None, + "an unterminated literal is not a token" + ); + } + + // ----------------------------------------------------------------------- + // T8 — `extract()` asserted shape by shape against the extractor's output + // ----------------------------------------------------------------------- + + /// `/tmp/m-matrix`'s measured bytes: 15 shape lines, of which 12 must emit + /// nothing and 3 must emit. + const MATRIX_TS: &str = r#"import { invoke } from '@tauri-apps/api/core'; +// invoke('save_config') +/* invoke('save_config') */ +const doc = "invoke('save_config')"; +const tpl = `invoke('save_config')`; +const reEsc = /invoke\('save_config'\)/; +const reRaw = /invoke('save_config')/; +const a = 6, b = 3; +const q = a / b, r = a /2/ b; +export function viaMember() { return client.invoke('save_config'); } +export function dyn(c: string) { return invoke(c); } +export function interp(id: string) { return invoke(`cmd_${id}`); } +export async function multi() { + return await invoke( + 'save_config' + ); +} +export async function cmt() { return await invoke /* x */ ('save_config'); } +export async function real() { return await invoke('save_config'); } +"#; + + const MATRIX_TSX: &str = r#"import { invoke } from '@tauri-apps/api/core'; +export function A() { return
y
; } +export function B() { return

invoke('save_config')

; } +export async function C() { return await invoke('save_config'); } +"#; + + fn positions(sites: &[CallSite]) -> Vec<(i64, i64)> { + sites.iter().map(|s| (s.line, s.column)).collect() + } + + /// Assertion 2 of T8: the RECOGNISED call sites must equal the extractor's + /// bare `invoke | calls` positions exactly — `line` 1-based, `col` 0-based at + /// the start of the reference expression, both measured on the baseline binary. + #[test] + fn tauri_extract_recognised_sites_match_the_extractor_positions() { + assert_eq!( + positions(&invoke_call_sites("src/app.ts", MATRIX_TS)), + vec![(11, 40), (12, 44), (14, 15), (18, 43), (19, 44)] + ); + assert_eq!( + positions(&invoke_call_sites("src/comp.tsx", MATRIX_TSX)), + vec![(4, 41)] + ); + } + + /// Assertion 1 of T8: three refs from `app.ts`, one from `comp.tsx`. Measured + /// on these bytes, a raw scan yields 7 in `app.ts`, a mask without the regex + /// state 5, and mask-plus-receiver-guard-without-regex 4 — so the count moves + /// for every defect and no `!contains` check separates any of them. + #[test] + fn tauri_extract_emits_one_ref_per_literal_call_site() { + let ts = invoke_call_refs("src/app.ts", MATRIX_TS); + assert_eq!(ts.len(), 3, "{ts:#?}"); + assert_eq!( + ts.iter() + .map(|r| (r.line, r.column, r.reference_name.clone())) + .collect::>(), + vec![ + (14, 15, "tauri:invoke:save_config".to_string()), + (18, 43, "tauri:invoke:save_config".to_string()), + (19, 44, "tauri:invoke:save_config".to_string()), + ] + ); + let tsx = invoke_call_refs("src/comp.tsx", MATRIX_TSX); + assert_eq!(tsx.len(), 1, "{tsx:#?}"); + assert_eq!(tsx[0].line, 4); + assert_eq!(tsx[0].column, 41); + } + + /// The two rows where the mask AGREES a call site exists and gate 4 still + /// declines: a non-literal argument has no name to recover, and no dynamic + /// sentinel is emitted either, because nothing would ever read it. + #[test] + fn tauri_extract_dynamic_arguments_are_recognised_but_emit_nothing() { + let sites = invoke_call_sites("src/app.ts", MATRIX_TS); + let dynamic: Vec<&CallSite> = sites + .iter() + .filter(|s| matches!((s.line, s.column), (11, 40) | (12, 44))) + .collect(); + assert_eq!(dynamic.len(), 2); + assert!(dynamic.iter().all(|s| s.wire_name.is_none())); + } + + #[test] + fn tauri_extract_ref_shape_is_file_granular_calls() { + let refs = invoke_call_refs("src/app.ts", MATRIX_TS); + let first = refs.first().expect("a ref"); + assert_eq!(first.from_node_id, "file:src/app.ts"); + assert_eq!(first.reference_kind, EdgeKind::Calls); + assert_eq!(first.language, Language::TypeScript); + assert!(first.row_id.is_none()); + assert!(first.reference_subkind.is_none()); + } + + /// The one enumerated S1 exception (D6): an object-literal call site is a REAL + /// call the extractor could not attribute to any function, so we emit a + /// superset there — six refs for six call sites where the extractor records + /// four rows, the two extra being the object-literal shapes, both attributed + /// to the file node. + #[test] + fn tauri_extract_object_literal_sites_are_a_deliberate_superset() { + let src = r#"import { invoke } from '@tauri-apps/api/core'; +export function plainFn() { return invoke('save_config'); } +export const arrowConst = () => invoke('save_config'); +class C { method() { return invoke('save_config'); } } +const obj = { prop() { return invoke('save_config'); } }; +const obj2 = { arrow: () => invoke('save_config') }; +export default function anon() { return invoke('save_config'); } +"#; + let refs = invoke_call_refs("src/shapes.ts", src); + assert_eq!(refs.len(), 6); + assert_eq!( + refs.iter().map(|r| r.line).collect::>(), + vec![2, 3, 4, 5, 6, 7] + ); + assert!(refs.iter().all(|r| r.from_node_id == "file:src/shapes.ts")); + } + + #[test] + fn tauri_extract_only_runs_on_js_family_files() { + let ctx = crate::framework::FrameworkExtractionContext::without_config("/root"); + let r = TauriResolver::default(); + assert!( + r.extract("src-tauri/src/main.rs", "invoke('x')", &ctx) + .is_none() + ); + assert!(r.extract("src-tauri/tauri.conf.json", "{}", &ctx).is_none()); + let ts = r + .extract("src/app.ts", "invoke('x');", &ctx) + .expect("js file"); + assert_eq!(ts.references.len(), 1); + assert!(ts.nodes.is_empty(), "this resolver emits NO nodes"); + } + + #[test] + fn tauri_extract_rejects_non_wire_name_literals() { + for arg in ["'9lives'", "''", "'a b'", "'a/b'"] { + let src = format!("invoke({arg});"); + assert!( + invoke_call_refs("a.ts", &src).is_empty(), + "{arg} is not a wire name" + ); + } + assert_eq!(invoke_call_refs("a.ts", "invoke('a-b.c:d');").len(), 1); + } + + #[test] + fn tauri_extract_generic_argument_list_still_reaches_the_call() { + assert_eq!( + invoke_call_refs("a.ts", "invoke('get_port');").len(), + 1 + ); + } + + #[test] + fn tauri_extract_second_literal_cannot_smuggle_a_name() { + let refs = invoke_call_refs("a.ts", "invoke('save_config', { body: 'other' });"); + assert_eq!(refs.len(), 1); + assert_eq!(refs[0].reference_name, "tauri:invoke:save_config"); + } + + #[test] + fn tauri_extract_identifier_boundaries() { + assert!(invoke_call_refs("a.ts", "invokeAll('x');").is_empty()); + assert!(invoke_call_refs("a.ts", "myinvoke('x');").is_empty()); + assert!(invoke_call_refs("a.ts", "$invoke('x');").is_empty()); + } + + #[test] + fn is_rust_source_and_js_family_extensions() { + assert!(is_rust_source("crates/a/src/lib.rs")); + assert!(!is_rust_source("reference/golden/x/main.rs")); + assert!(!is_rust_source("src/app.ts")); + assert!(is_js_family("src/app.ts")); + assert!(is_js_family("src/comp.tsx")); + assert!(is_js_family("src/a.mjs")); + assert!(!is_js_family("src-tauri/src/main.rs")); + } +} diff --git a/crates/codegraph-resolve/tests/tauri_ipc.rs b/crates/codegraph-resolve/tests/tauri_ipc.rs new file mode 100644 index 00000000..4a99dd1c --- /dev/null +++ b/crates/codegraph-resolve/tests/tauri_ipc.rs @@ -0,0 +1,666 @@ +//! Tauri IPC-bridge integration tests (Tier 4 item 10, upstream issue #1543). +//! +//! Every test here drives the FULL pipeline — base extraction → +//! `extract_and_persist_frameworks` (where `TauriResolver::extract` emits the +//! `tauri:invoke:*` refs) → `resolve_and_persist` (where `TauriResolver::resolve` +//! turns them into edges) — over a fixture written into a temp directory, so the +//! worktree stays clean and nothing under `reference/` is touched. +//! +//! The fixtures are the ones MEASURED in the plan, not invented here: +//! * `build_repro_fixture` — the §2 reproduction (2 commands, 2 literal invokes) +//! * `build_matrix_fixture` — `/tmp/m-matrix`: 15 shape lines in `.ts` + 4 in `.tsx` +//! * `build_adversarial_fixture` — `/tmp/m-rust`: 6 raw `tauri::command` hits, 1 real +//! +//! A count is asserted everywhere a presence check could pass on a fabricated +//! edge: the failure this tier's review rounds kept finding is an edge that EXISTS +//! but points at a function the developer never exposed over IPC. + +use std::collections::BTreeSet; +use std::path::{Path, PathBuf}; + +use codegraph_core::types::{Edge, EdgeKind, FileRecord, Node, NodeKind}; +use codegraph_extract::{detect_language, extract_file}; +use codegraph_resolve::ReferenceResolver; +use codegraph_store::Store; + +fn unique_dir(slug: &str) -> PathBuf { + let dir = std::env::temp_dir().join(format!( + "codegraph-tauri-{slug}-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("clock after epoch") + .as_nanos() + )); + std::fs::create_dir_all(&dir).expect("mkdir fixture"); + dir +} + +fn temp_db_path(test_name: &str) -> PathBuf { + let mut path = std::env::temp_dir(); + let nanos = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .expect("clock after epoch") + .as_nanos(); + path.push(format!( + "codegraph-tauri-{test_name}-{}-{nanos}.db", + std::process::id() + )); + path +} + +fn write(root: &Path, relative: &str, content: &str) { + let path = root.join(relative); + if let Some(parent) = path.parent() { + std::fs::create_dir_all(parent).expect("mkdir parent"); + } + std::fs::write(&path, content).expect("write fixture file"); +} + +/// Index `relative_files` under `root` into a fresh store and run the whole +/// resolver pipeline (detect → framework extract → resolve). Mirrors +/// `tests/godot_post_extract.rs:54`'s `run_pipeline`; `run_post_extract` is +/// omitted because the Tauri resolver has no finalization pass. +struct Indexed { + store: Store, + edges: Vec, +} + +fn run_pipeline(test_name: &str, root: &Path, relative_files: &[&str]) -> Indexed { + let mut store = index_without_resolve(test_name, root, relative_files); + let root_str = root.to_string_lossy().to_string(); + let mut resolver = ReferenceResolver::new(root_str.clone()); + { + let context = codegraph_resolve::StoreResolutionContext::new(&store, &root_str); + resolver.initialize(&context); + } + resolver + .resolve_and_persist(&mut store) + .expect("resolve and persist"); + + let edges = all_resolved_edges(&store); + Indexed { store, edges } +} + +/// Base extraction plus `extract_and_persist_frameworks`, with NO resolution pass. +/// +/// The split exists because resolution DELETES the rows it resolves, so a test that +/// needs to see the emitted `tauri:invoke:*` references beside the extractor's own +/// `invoke | calls` rows has to look before that happens. +fn index_without_resolve(test_name: &str, root: &Path, relative_files: &[&str]) -> Store { + let mut store = Store::open(&temp_db_path(test_name)).expect("open store"); + + for &relative in relative_files { + let language = detect_language(relative); + let result = extract_file(root, relative).expect("extract file"); + store + .upsert_file(&FileRecord { + path: relative.to_string(), + content_hash: "fixture".to_string(), + language, + size: 0, + modified_at: 0, + indexed_at: 0, + node_count: result.nodes.len() as i64, + errors: Vec::new(), + }) + .expect("upsert file"); + store.upsert_nodes(&result.nodes).expect("upsert nodes"); + store + .insert_edges(&result.edges) + .expect("insert contains edges"); + store + .insert_unresolved_refs(&result.unresolved_references) + .expect("insert unresolved refs"); + } + + let root_str = root.to_string_lossy().to_string(); + let mut resolver = ReferenceResolver::new(root_str.clone()); + { + let context = codegraph_resolve::StoreResolutionContext::new(&store, &root_str); + resolver.initialize(&context); + } + let relative: Vec = relative_files.iter().map(|f| (*f).to_string()).collect(); + resolver + .extract_and_persist_frameworks(&mut store, &relative) + .expect("framework extract"); + store +} + +fn all_resolved_edges(store: &Store) -> Vec { + let mut resolved = Vec::new(); + for kind in NodeKind::ALL { + for node in store.nodes_by_kind(kind).expect("nodes by kind") { + for edge in store + .edges_by_source_kind(&node.id, None) + .expect("edges by source") + { + if edge.kind != codegraph_core::types::EdgeKind::Contains { + resolved.push(edge); + } + } + } + } + resolved +} + +impl Indexed { + fn file_path_of(&self, node_id: &str) -> String { + self.store + .node_by_id(node_id) + .expect("node lookup") + .map(|n| n.file_path) + .unwrap_or_default() + } + + fn name_of(&self, node_id: &str) -> String { + self.store + .node_by_id(node_id) + .expect("node lookup") + .map(|n| n.name) + .unwrap_or_default() + } + + /// `calls` edges from a JS-family file into a `.rs` file — the IPC bridge, and + /// the count every acceptance criterion in the plan asserts. + fn ipc_edges(&self) -> Vec<&Edge> { + self.edges + .iter() + .filter(|e| e.kind == codegraph_core::types::EdgeKind::Calls) + .filter(|e| { + let source = self.file_path_of(&e.source); + let target = self.file_path_of(&e.target); + target.ends_with(".rs") + && (source.ends_with(".ts") + || source.ends_with(".tsx") + || source.ends_with(".js") + || source.ends_with(".jsx")) + }) + .collect() + } + + /// IPC edges landing on a target NAMED `name` — asserted per name so a + /// failure says which mask state broke rather than reporting a wrong total. + fn ipc_edges_to(&self, name: &str) -> usize { + self.ipc_edges() + .into_iter() + .filter(|e| self.name_of(&e.target) == name) + .count() + } + + fn nodes_named(&self, name: &str) -> Vec { + self.store.nodes_by_name(name).expect("nodes by name") + } +} + +/// The §2 reproduction fixture, byte-for-byte: two `#[tauri::command]` fns +/// registered through `tauri::generate_handler!`, and two literal `invoke` call +/// sites in `src/app.ts`. +fn build_repro_fixture(root: &Path) { + write( + root, + "src-tauri/src/main.rs", + r#"#[tauri::command] +fn get_mcp_port() -> u16 { + 8111 +} + +#[tauri::command] +fn save_config(body: String) -> bool { + !body.is_empty() +} + +fn main() { + tauri::Builder::default() + .invoke_handler(tauri::generate_handler![get_mcp_port, save_config]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} +"#, + ); + write( + root, + "src/app.ts", + r#"import { invoke } from '@tauri-apps/api/core'; + +export async function loadPort(): Promise { + return await invoke('get_mcp_port'); +} + +export async function persist(body: string): Promise { + return await invoke('save_config', { body }); +} +"#, + ); + write( + root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"ac-tauri\", \"identifier\": \"dev.ac.tauri\", \"build\": {}, \"app\": {} }\n", + ); + write( + root, + "package.json", + "{ \"name\": \"ac-tauri\", \"dependencies\": { \"@tauri-apps/api\": \"^2.0.0\" } }\n", + ); +} + +/// `/tmp/m-matrix`'s measured bytes — 15 shape lines in `.ts` (12 that must emit +/// nothing, 3 that must emit) plus 4 in `.tsx`, every one naming the SAME command +/// so any fabricated reference becomes a visible edge. +fn build_matrix_fixture(root: &Path) { + write( + root, + "src/app.ts", + r#"import { invoke } from '@tauri-apps/api/core'; +// invoke('save_config') +/* invoke('save_config') */ +const doc = "invoke('save_config')"; +const tpl = `invoke('save_config')`; +const reEsc = /invoke\('save_config'\)/; +const reRaw = /invoke('save_config')/; +const a = 6, b = 3; +const q = a / b, r = a /2/ b; +export function viaMember() { return client.invoke('save_config'); } +export function dyn(c: string) { return invoke(c); } +export function interp(id: string) { return invoke(`cmd_${id}`); } +export async function multi() { + return await invoke( + 'save_config' + ); +} +export async function cmt() { return await invoke /* x */ ('save_config'); } +export async function real() { return await invoke('save_config'); } +"#, + ); + write( + root, + "src/comp.tsx", + r#"import { invoke } from '@tauri-apps/api/core'; +export function A() { return
y
; } +export function B() { return

invoke('save_config')

; } +export async function C() { return await invoke('save_config'); } +"#, + ); + write( + root, + "src-tauri/src/main.rs", + "#[tauri::command]\nfn save_config(body: String) -> bool { !body.is_empty() }\n", + ); + write( + root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"neg\", \"identifier\": \"dev.neg\", \"build\": {}, \"app\": {} }\n", + ); +} + +/// `/tmp/m-rust`'s measured bytes — SIX raw `tauri::command` hits of which exactly +/// one is a genuine attribute, and a `.ts` invoking all six names. The five fake +/// names each have a real UNATTRIBUTED function, so an unmasked roster binds each +/// to the wrong node: measured, six edges where the correct answer is one. +/// +/// `frameworks/tauri.rs` holds the same bytes for the roster key-set test, and both +/// sides assert the raw hit count so the two copies cannot drift apart unnoticed. +fn build_adversarial_fixture(root: &Path) { + write( + root, + "src-tauri/src/main.rs", + r####"/* outer /* inner */ #[tauri::command] +fn nested_fake() -> u16 { 0 } */ +const D1: &str = r##" +#[tauri::command] +fn hash_fake() -> u16 { 1 } +"##; +const D2: &[u8] = br##" +#[tauri::command] +fn byteraw_fake() -> u16 { 2 } +"##; +const D3: &[u8] = b"#[tauri::command] fn bytestr_fake() -> u16 { 3 }"; +fn life<'a>(s: &'a str) -> &'a str { s } +const Q: char = '\''; +#[tauri::commandant] +fn attr_boundary_fake() -> u16 { 4 } +fn nested_fake() -> u16 { 40 } +fn hash_fake() -> u16 { 41 } +fn byteraw_fake() -> u16 { 42 } +fn bytestr_fake() -> u16 { 43 } +#[tauri::command] +fn real_cmd() -> u16 { 8111 } +"####, + ); + write( + root, + "src/app.ts", + r#"import { invoke } from '@tauri-apps/api/core'; +export const a = () => invoke('nested_fake'); +export const b = () => invoke('hash_fake'); +export const c = () => invoke('byteraw_fake'); +export const d = () => invoke('bytestr_fake'); +export const e = () => invoke('attr_boundary_fake'); +export const f = () => invoke('real_cmd'); +"#, + ); + write( + root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"adv\", \"identifier\": \"dev.adv\", \"build\": {}, \"app\": {} }\n", + ); +} + +const FAKE_NAMES: &[&str] = &[ + "nested_fake", + "hash_fake", + "byteraw_fake", + "bytestr_fake", + "attr_boundary_fake", +]; + +/// T1 — the reproduction, as a test. +/// +/// Measured on the real binary at the baseline (plan §2): `codegraph callers +/// get_mcp_port` reports "No callers found", the fixture holds ZERO `calls` edges +/// in either direction, and the raw material sits in `unresolved_refs` as two +/// bare `invoke | calls` rows whose string-literal argument is not stored +/// anywhere. So this asserts `2` and reads `0` until the resolver lands. +#[test] +fn tauri_invoke_bridges_both_commands() { + let root = unique_dir("repro"); + build_repro_fixture(&root); + let g = run_pipeline("repro", &root, &["src-tauri/src/main.rs", "src/app.ts"]); + + let edges = g.ipc_edges(); + assert_eq!( + edges.len(), + 2, + "expected one .ts -> .rs `calls` edge per literal invoke site, got {:?}", + edges + .iter() + .map(|e| format!("{} -> {}", g.file_path_of(&e.source), g.name_of(&e.target))) + .collect::>() + ); + assert_eq!(g.ipc_edges_to("get_mcp_port"), 1); + assert_eq!(g.ipc_edges_to("save_config"), 1); +} + +/// T9 — the edge's metadata, not merely its existence. +/// +/// 0.9 is the switch that says "this resolver already applied its own uniqueness +/// rule": `resolve_one_pure_inner` returns immediately from Strategy 1 at `>= 0.9`, +/// so nothing downstream may add to or override the result. Upstream #878 uses 0.7, +/// which hands the final say to the name matcher — the component whose +/// cross-language behaviour produced the Tier 2 regression. +#[test] +fn tauri_edge_is_framework_resolved_at_confidence_0_9() { + let root = unique_dir("meta"); + build_repro_fixture(&root); + let g = run_pipeline("meta", &root, &["src-tauri/src/main.rs", "src/app.ts"]); + + let edges = g.ipc_edges(); + assert_eq!(edges.len(), 2); + for edge in edges { + let metadata = edge.metadata.clone().expect("the edge carries metadata"); + assert_eq!(metadata["resolvedBy"].as_str(), Some("framework")); + assert_eq!(metadata["confidence"].as_f64(), Some(0.9)); + assert_eq!(edge.kind, EdgeKind::Calls); + assert_eq!( + g.file_path_of(&edge.source), + "src/app.ts", + "the source is the FILE node (D6)" + ); + } +} + +/// T8 / AC-5b — the mask AGREES with the extractor, positionally, in both +/// directions. +/// +/// The criterion that does not depend on having enumerated the right shapes in +/// advance: it compares this resolver's lexer against the base extractor's on +/// whatever the fixture happens to contain, so an unenumerated fabricating shape +/// fails without anyone having thought of it first. +/// +/// Asserted BEFORE resolution, deliberately: `delete_resolved_rows` removes every +/// ref that resolved (keyed on `unresolved_refs.id`), so after a full pipeline the +/// `tauri:invoke:*` rows for resolvable names are gone and a post-resolution +/// comparison would read an empty set as agreement. +#[test] +fn tauri_extracted_refs_agree_with_the_extractor_positions() { + let root = unique_dir("agree"); + build_matrix_fixture(&root); + let files = ["src-tauri/src/main.rs", "src/app.ts", "src/comp.tsx"]; + let store = index_without_resolve("agree", &root, &files); + let refs = store.all_unresolved_refs().expect("all unresolved refs"); + + let extractor_sites: BTreeSet<(String, i64, i64)> = refs + .iter() + .filter(|r| r.reference_name == "invoke" && r.reference_kind == EdgeKind::Calls) + .map(|r| (r.file_path.clone(), r.line, r.col)) + .collect(); + let ours: BTreeSet<(String, i64, i64)> = refs + .iter() + .filter(|r| r.reference_name.starts_with("tauri:invoke:")) + .map(|r| (r.file_path.clone(), r.line, r.col)) + .collect(); + + // The extractor's own call-site set, measured on these bytes with the baseline + // binary. Pinned so a change in ITS behaviour is reported here rather than + // silently redefining what agreement means. + let site = |f: &str, l: i64, c: i64| (f.to_string(), l, c); + assert_eq!( + extractor_sites, + BTreeSet::from([ + site("src/app.ts", 11, 40), + site("src/app.ts", 12, 44), + site("src/app.ts", 14, 15), + site("src/app.ts", 18, 43), + site("src/app.ts", 19, 44), + site("src/comp.tsx", 4, 41), + ]) + ); + + // S1 (no fabrication) and S2 (no over-masking) at once: our emitted set must be + // the extractor's set MINUS exactly the two non-literal-argument sites, which + // gate 4 declines. This fixture carries no object-literal shape, so the one + // enumerated D6 exception does not apply and equality is exact. + let non_literal = BTreeSet::from([site("src/app.ts", 11, 40), site("src/app.ts", 12, 44)]); + assert!( + non_literal.is_subset(&extractor_sites), + "the excluded positions must be REAL extractor rows, not typos" + ); + let expected: BTreeSet<(String, i64, i64)> = + extractor_sites.difference(&non_literal).cloned().collect(); + assert_eq!(ours, expected); +} + +/// AC-5 — the call-side shape matrix as an EDGE count. +/// +/// Four call sites, one command, four distinct `(line, col)` — and four rows, +/// because `idx_edges_identity` includes the position, so three sites in one file +/// all sourced from `file:src/app.ts` and all targeting the same command still +/// produce three distinct edges. Measured on these bytes: a raw text scan yields +/// 7, a mask without the regex state 5, mask-plus-receiver-guard-without-regex 4 +/// (with a wrong position set), and gates that do not skip masked bytes 2. +#[test] +fn tauri_shape_matrix_yields_exactly_four_edges() { + let root = unique_dir("matrix"); + build_matrix_fixture(&root); + let g = run_pipeline( + "matrix", + &root, + &["src-tauri/src/main.rs", "src/app.ts", "src/comp.tsx"], + ); + + let mut positions: Vec<(String, i64, i64)> = g + .ipc_edges() + .into_iter() + .map(|e| { + ( + g.file_path_of(&e.source), + e.line.unwrap_or(-1), + e.col.unwrap_or(-1), + ) + }) + .collect(); + positions.sort(); + assert_eq!( + positions, + vec![ + ("src/app.ts".to_string(), 14, 15), + ("src/app.ts".to_string(), 18, 43), + ("src/app.ts".to_string(), 19, 44), + ("src/comp.tsx".to_string(), 4, 41), + ] + ); + assert_eq!(g.ipc_edges_to("save_config"), 4); +} + +/// T4b(j) / AC-6b — a masked or boundary-colliding attribute must not promote an +/// unattributed function. +/// +/// The three parts are all required: the per-name zeros are the fabrication guard, +/// `real_cmd == 1` stops them being satisfied by a mask that fails closed and +/// resolves nothing, and the total pins both at once. The node-count provenance is +/// what proves the fabrication is REACHABLE rather than theoretical — each fake +/// name has exactly one node, the real unattributed function, so a naive roster's +/// same-file/same-name join has one candidate and it is the wrong one. +#[test] +fn tauri_masked_attributes_promote_no_unattributed_function() { + let root = unique_dir("adv"); + build_adversarial_fixture(&root); + let g = run_pipeline("adv", &root, &["src-tauri/src/main.rs", "src/app.ts"]); + + for fake in FAKE_NAMES { + assert_eq!( + g.nodes_named(fake).len(), + 1, + "{fake} must have exactly one node — the real UNATTRIBUTED fn" + ); + assert_eq!( + g.ipc_edges_to(fake), + 0, + "{fake} is not a command; an edge to it is fabricated" + ); + } + assert_eq!(g.ipc_edges_to("real_cmd"), 1); + assert_eq!( + g.ipc_edges().len(), + 1, + "six call sites, ONE command, one edge" + ); +} + +/// T10 — the accepted false positive, asserted deliberately. +/// +/// `generate_handler!` is not parsed (D2), so an attributed-but-unregistered +/// command DOES get an edge. Wrong, but wrong in the direction of the developer's +/// intent and visible. If a later change starts parsing the macro, this is the +/// assertion that notices. +#[test] +fn tauri_unregistered_command_still_bridges() { + let root = unique_dir("unregistered"); + write( + &root, + "src-tauri/src/main.rs", + r#"#[tauri::command] +fn registered_cmd() -> u16 { 1 } + +#[tauri::command] +fn unregistered_cmd() -> u16 { 2 } + +fn main() { + tauri::Builder::default() + .invoke_handler(tauri::generate_handler![registered_cmd]) + .run(tauri::generate_context!()) + .expect("error while running tauri application"); +} +"#, + ); + write( + &root, + "src/app.ts", + "import { invoke } from '@tauri-apps/api/core';\n\ + export const a = () => invoke('registered_cmd');\n\ + export const b = () => invoke('unregistered_cmd');\n", + ); + write( + &root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"u\", \"identifier\": \"dev.u\", \"build\": {}, \"app\": {} }\n", + ); + let g = run_pipeline( + "unregistered", + &root, + &["src-tauri/src/main.rs", "src/app.ts"], + ); + + assert_eq!(g.ipc_edges_to("registered_cmd"), 1); + assert_eq!( + g.ipc_edges_to("unregistered_cmd"), + 1, + "attributed-but-unregistered is a DELIBERATE false positive (D2)" + ); +} + +/// T5 / AC-4 — ambiguity produces no edge, end to end. +/// +/// A Tauri app cannot really register two commands under one wire name, but the +/// roster is built by scanning attributes rather than the registration list, so +/// dead code, a `#[cfg]`-excluded duplicate or a vendored copy can reach this. The +/// honest answer is "cannot tell which", and no edge is the answer. +#[test] +fn tauri_two_same_named_commands_produce_no_edge() { + let root = unique_dir("ambiguous"); + let command = "#[tauri::command]\npub fn save() -> u16 { 0 }\n"; + write(&root, "src-tauri/src/a.rs", command); + write(&root, "src-tauri/src/b.rs", command); + write( + &root, + "src/app.ts", + "import { invoke } from '@tauri-apps/api/core';\n\ + export const a = () => invoke('save');\n", + ); + write( + &root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"amb\", \"identifier\": \"dev.amb\", \"build\": {}, \"app\": {} }\n", + ); + let g = run_pipeline( + "ambiguous", + &root, + &["src-tauri/src/a.rs", "src-tauri/src/b.rs", "src/app.ts"], + ); + + assert_eq!(g.nodes_named("save").len(), 2, "both commands are indexed"); + assert_eq!(g.ipc_edges().len(), 0, "a poisoned key builds NO edge"); +} + +/// D4 — the `tauri-specta` camelCase spelling reaches the same command, and the +/// single-word case is not self-poisoned. +#[test] +fn tauri_camel_and_single_word_spellings_bridge() { + let root = unique_dir("camel"); + write( + &root, + "src-tauri/src/main.rs", + "#[tauri::command]\nfn get_mcp_port() -> u16 { 8111 }\n\ + #[tauri::command]\nfn refresh() -> u16 { 1 }\n", + ); + write( + &root, + "src/app.ts", + "import { invoke } from '@tauri-apps/api/core';\n\ + export const a = () => invoke('getMcpPort');\n\ + export const b = () => invoke('refresh');\n", + ); + write( + &root, + "src-tauri/tauri.conf.json", + "{ \"productName\": \"c\", \"identifier\": \"dev.c\", \"build\": {}, \"app\": {} }\n", + ); + let g = run_pipeline("camel", &root, &["src-tauri/src/main.rs", "src/app.ts"]); + + assert_eq!( + g.ipc_edges_to("get_mcp_port"), + 1, + "camel key -> snake command" + ); + assert_eq!(g.ipc_edges_to("refresh"), 1, "single-word is not poisoned"); +} diff --git a/crates/codegraph-store/src/index_state.rs b/crates/codegraph-store/src/index_state.rs index 0bfb9f3f..b33aba64 100644 --- a/crates/codegraph-store/src/index_state.rs +++ b/crates/codegraph-store/src/index_state.rs @@ -115,7 +115,7 @@ pub const CURRENT_STORAGE_PROTOCOL: u64 = 2; /// The extraction-pipeline version this binary produces. This is the single /// source of truth: no other crate may define its own copy. -pub const CURRENT_EXTRACTION_VERSION: u64 = 7; +pub const CURRENT_EXTRACTION_VERSION: u64 = 8; /// The `project_metadata` key under which a built index records the extraction /// version it was produced with. Single source of truth for the key spelling. diff --git a/crates/codegraph-store/tests/index_state.rs b/crates/codegraph-store/tests/index_state.rs index 6493cb72..5d0fa0a6 100644 --- a/crates/codegraph-store/tests/index_state.rs +++ b/crates/codegraph-store/tests/index_state.rs @@ -119,7 +119,7 @@ fn assert_corrupt( fn index_state_constants_and_canonical_payload_are_exact() { /// The extraction version the immediately preceding release wrote. Read from /// `index_state.rs` at implementation time; see plan decision C1 step 0. - const PREVIOUS_EXTRACTION_VERSION: u64 = 6; + const PREVIOUS_EXTRACTION_VERSION: u64 = 7; assert_eq!(CURRENT_STORAGE_PROTOCOL, 2); assert_eq!(CURRENT_EXTRACTION_VERSION, PREVIOUS_EXTRACTION_VERSION + 1);