use splice::ingest::typescript::{extract_typescript_symbols, TypeScriptSymbolKind};
use std::path::Path;
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_interface_declaration() {
let source = b"interface User {\n name: string;\n age: number;\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "User");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Interface);
}
#[test]
fn test_extract_type_alias() {
let source = b"type UserId = string | number;\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "UserId");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::TypeAlias);
}
#[test]
fn test_extract_enum_declaration() {
let source = b"enum Color {\n Red,\n Green,\n Blue\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "Color");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Enum);
}
#[test]
fn test_extract_namespace_declaration() {
let source = b"namespace Utils {\n export function helper() {}\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 2); assert_eq!(symbols[0].name, "Utils");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Namespace);
}
#[test]
fn test_extract_class_declaration() {
let source =
b"class Person {\n constructor(public name: string) {}\n greet(): void {}\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert!(!symbols.is_empty());
assert_eq!(symbols[0].name, "Person");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Class);
}
#[test]
fn test_extract_function_with_type_annotations() {
let source = b"function add(a: number, b: number): number {\n return a + b;\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "add");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Function);
assert_eq!(symbols[0].parameters, vec!["a", "b"]);
}
#[test]
fn test_extract_exported_interface() {
let source = b"export interface IUser {\n id: string;\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "IUser");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Interface);
assert!(symbols[0].is_exported);
}
#[test]
fn test_extract_generic_function() {
let source = b"function identity<T>(arg: T): T {\n return arg;\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "identity");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Function);
}
#[test]
fn test_extract_class_with_method() {
let source = b"class Calculator {\n add(a: number, b: number): number {\n return a + b;\n }\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 2);
assert_eq!(symbols[0].name, "Calculator");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Class);
assert_eq!(symbols[1].name, "add");
assert_eq!(symbols[1].kind, TypeScriptSymbolKind::Method);
}
#[test]
fn test_extract_async_function() {
let source = b"async function fetchData(): Promise<string> {\n return \"data\";\n}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "fetchData");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Function);
assert!(symbols[0].is_async);
}
#[test]
fn test_symbol_has_byte_span() {
let source = b"interface Foo {}\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert!(symbols[0].byte_start < symbols[0].byte_end);
assert_eq!(symbols[0].byte_start, 0); }
#[test]
fn test_extract_variable_declaration_with_type() {
let source = b"const count: number = 42;\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "count");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Variable);
}
#[test]
fn test_extract_arrow_function_assignment() {
let source = b"const greet = (name: string): string => `Hello ${name}`;\n";
let symbols =
extract_typescript_symbols(Path::new("/tmp/test.ts"), source).expect("Failed to parse");
assert_eq!(symbols.len(), 1);
assert_eq!(symbols[0].name, "greet");
assert_eq!(symbols[0].kind, TypeScriptSymbolKind::Variable);
}
}