use sdivi_patterns::queries::{
async_patterns, classify_hint, collection_pipelines, concurrency, data_access, framework_hooks,
http_routing, logging, schema_validation, serialization, state_store, testing,
};
use sdivi_patterns::PatternHintInput;
fn hint(node_kind: &str, text: &str) -> PatternHintInput {
PatternHintInput {
node_kind: node_kind.to_string(),
text: text.to_string(),
}
}
fn all_matching_categories(text: &str, language: &str) -> Vec<&'static str> {
let mut matched = Vec::new();
if async_patterns::matches_callee(text, language) {
matched.push("async_patterns");
}
if testing::matches_callee(text, language) {
matched.push("testing");
}
if serialization::matches_callee(text, language) {
matched.push("serialization");
}
if schema_validation::matches_callee(text, language) {
matched.push("schema_validation");
}
if state_store::matches_callee(text, language) {
matched.push("state_store");
}
if framework_hooks::matches_callee(text, language) {
matched.push("framework_hooks");
}
if http_routing::matches_callee(text, language) {
matched.push("http_routing");
}
if logging::matches_callee(text, language) {
matched.push("logging");
}
if data_access::matches_callee(text, language) {
matched.push("data_access");
}
if collection_pipelines::matches_callee(text, language) {
matched.push("collection_pipelines");
}
if concurrency::matches_callee(text, language) {
matched.push("concurrency");
}
matched
}
const KNOWN_OVERLAPS: &[(&str, &str, &str, &str)] = &[
(
"fetch(url).catch(err => {})",
"javascript",
"async_patterns",
"data_access",
),
("logger.get(\"x\")", "typescript", "logging", "data_access"),
(
"useSelector(s => s.user)",
"typescript",
"state_store",
"framework_hooks",
),
(
"useDispatch()",
"typescript",
"state_store",
"framework_hooks",
),
("useStore()", "javascript", "state_store", "framework_hooks"),
(
"app.get('/u', h)",
"typescript",
"http_routing",
"data_access",
),
(
"router.post('/user', cb)",
"typescript",
"http_routing",
"data_access",
),
];
const CORPUS: &[(&str, &str, &str)] = &[
("promise.then(resolve)", "typescript", "async_patterns"),
(
"fetch(url).catch(err => {})",
"javascript",
"async_patterns",
),
("describe('suite', fn)", "typescript", "testing"), ("JSON.parse(s)", "typescript", "serialization"),
("json.dumps(o)", "python", "serialization"),
("json.Marshal(v)", "go", "serialization"),
("useState(0)", "typescript", "framework_hooks"), ("useEffect(fn, [])", "typescript", "framework_hooks"),
("logger.get(\"x\")", "typescript", "logging"),
("console.log(\"x\")", "typescript", "logging"),
("logger.info(\"x\")", "typescript", "logging"),
("log.debug(\"x\")", "javascript", "logging"),
("logging.info(\"x\")", "python", "logging"),
("print(x)", "python", "logging"),
("fmt.Println(\"x\")", "go", "logging"),
("fmt.Printf(\"%v\", x)", "go", "logging"),
("System.out.println(\"x\")", "java", "logging"),
("logger.info(\"x\")", "java", "logging"),
("createSlice({})", "typescript", "state_store"),
("createStore(rootReducer)", "javascript", "state_store"),
("makeAutoObservable(this)", "typescript", "state_store"),
("createSignal(0)", "typescript", "state_store"),
("create((set) => ({}))", "typescript", "state_store"),
("useSelector(s => s.user)", "typescript", "state_store"),
("useDispatch()", "typescript", "state_store"),
("useStore()", "javascript", "state_store"),
("prisma.user.create(data)", "typescript", ""),
("z.object({})", "typescript", "schema_validation"),
("z.string()", "javascript", "schema_validation"),
("yup.object().shape({})", "typescript", "schema_validation"),
("v.object({})", "javascript", "schema_validation"),
("s.object({})", "typescript", "schema_validation"),
(
"UserSchema.safeParse(input)",
"typescript",
"schema_validation",
),
("Field(default=0)", "python", "schema_validation"),
("constr(min_length=1)", "python", "schema_validation"),
("conint(gt=0)", "python", "schema_validation"),
("SomeClass.parse(x)", "typescript", ""), ("@Injectable()", "typescript", ""),
("fetch(\"/api/users\")", "typescript", "data_access"),
("axios.get(\"/api\")", "typescript", "data_access"),
("db.query(sql)", "go", "data_access"),
("sql.Open(\"postgres\", dsn)", "go", "data_access"),
("cursor.execute(sql)", "python", "data_access"),
("requests.get(url)", "python", "data_access"),
("app.get('/u', h)", "typescript", "http_routing"),
("r.GET(\"/users\", h)", "go", "http_routing"),
("axios.get(url)", "typescript", "data_access"), ("xs.map(f)", "typescript", "collection_pipelines"),
("xs.filter(p)", "javascript", "collection_pipelines"),
("data.flatMap(fn)", "typescript", "collection_pipelines"),
("items.forEach(cb)", "javascript", "collection_pipelines"),
("xs.find(p)", "typescript", "collection_pipelines"),
("client.read(buf)", "typescript", "data_access"),
("Promise.all([a, b])", "typescript", "concurrency"),
("Promise.race([x, y])", "javascript", "concurrency"),
("asyncio.gather(*tasks)", "python", "concurrency"),
("Math.max(a, b)", "typescript", ""),
("len(x)", "python", ""),
("os.Exit(1)", "go", ""),
];
#[test]
fn corpus_resolves_to_expected_category() {
for &(text, lang, expected) in CORPUS {
let result = classify_hint(&hint("call_expression", text), lang);
if expected.is_empty() {
assert!(
result.is_empty(),
"({text:?}, {lang:?}): expected empty Vec but got {result:?}"
);
} else {
assert_eq!(
result,
vec![expected],
"({text:?}, {lang:?}): expected [{expected:?}] but got {result:?}"
);
}
}
}
#[test]
fn corpus_resolves_identically_for_call_node_kind() {
for &(text, lang, _expected) in CORPUS {
let via_call = classify_hint(&hint("call", text), lang);
let via_call_expr = classify_hint(&hint("call_expression", text), lang);
assert_eq!(
via_call, via_call_expr,
"({text:?}, {lang:?}): `call` and `call_expression` arms must behave identically"
);
}
}
#[test]
fn no_undocumented_overlaps_in_corpus() {
for &(text, lang, _expected) in CORPUS {
let matched = all_matching_categories(text, lang);
if matched.len() <= 1 {
continue; }
let documented = KNOWN_OVERLAPS
.iter()
.any(|&(ot, ol, _, _)| ot == text && ol == lang);
assert!(
documented,
"Undocumented overlap for ({text:?}, {lang:?}): matches {matched:?}. \
Add an entry to KNOWN_OVERLAPS with the winning category named."
);
}
}
#[test]
fn known_overlaps_winner_matches_dispatch_order() {
for &(text, lang, winner, loser) in KNOWN_OVERLAPS {
let result = classify_hint(&hint("call_expression", text), lang);
assert_eq!(
result,
vec![winner],
"KNOWN_OVERLAPS ({text:?}, {lang:?}): documented winner is {winner:?} \
but CALL_DISPATCH returned {result:?}. Update KNOWN_OVERLAPS or fix CALL_DISPATCH order."
);
let loser_matches = match loser {
"async_patterns" => async_patterns::matches_callee(text, lang),
"testing" => testing::matches_callee(text, lang),
"serialization" => serialization::matches_callee(text, lang),
"schema_validation" => schema_validation::matches_callee(text, lang),
"state_store" => state_store::matches_callee(text, lang),
"framework_hooks" => framework_hooks::matches_callee(text, lang),
"logging" => logging::matches_callee(text, lang),
"data_access" => data_access::matches_callee(text, lang),
"collection_pipelines" => collection_pipelines::matches_callee(text, lang),
"http_routing" => http_routing::matches_callee(text, lang),
"concurrency" => concurrency::matches_callee(text, lang),
other => panic!("KNOWN_OVERLAPS references unknown category {other:?}"),
};
assert!(
loser_matches,
"KNOWN_OVERLAPS ({text:?}, {lang:?}): loser category {loser:?} does not \
actually match — remove it from KNOWN_OVERLAPS or fix the regex."
);
}
}