seshat-scanner 0.3.2

Tree-sitter parsing and file discovery for Seshat
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//! Integration tests for the Rust parser.
//!
//! Parses fixture files in `tests/fixtures/rust_project/` and verifies
//! the expected IR is produced.

use std::fs;
use std::path::Path;

use seshat_core::{Language, LanguageIR, TypeDefKind};
use seshat_scanner::parse_file;

fn fixture_path(relative: &str) -> std::path::PathBuf {
    Path::new(env!("CARGO_MANIFEST_DIR"))
        .join("tests/fixtures/rust_project")
        .join(relative)
}

fn parse_fixture(relative: &str) -> seshat_core::ProjectFile {
    let path = fixture_path(relative);
    let source = fs::read_to_string(&path).expect("fixture file should exist");
    parse_file(&path, &source, Language::Rust)
}

// ---------------------------------------------------------------------------
// main.rs
// ---------------------------------------------------------------------------

#[test]
fn main_rs_imports() {
    let pf = parse_fixture("src/main.rs");

    // use std::io::{self, Read, Write}
    let io_import = pf
        .imports
        .iter()
        .find(|i| i.module.contains("std::io"))
        .expect("should find std::io import");
    assert!(io_import.names.contains(&"Read".to_string()));
    assert!(io_import.names.contains(&"Write".to_string()));

    // use serde::{Deserialize, Serialize}
    let serde_import = pf
        .imports
        .iter()
        .find(|i| i.module.contains("serde"))
        .expect("should find serde import");
    assert!(serde_import.names.contains(&"Deserialize".to_string()));
    assert!(serde_import.names.contains(&"Serialize".to_string()));
}

#[test]
fn main_rs_mod_declarations() {
    let pf = parse_fixture("src/main.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    assert!(ir.mod_declarations.iter().any(|m| m.name == "config"));
    assert!(ir.mod_declarations.iter().any(|m| m.name == "error"));
    assert!(ir.mod_declarations.iter().any(|m| m.name == "server"));
}

#[test]
fn main_rs_functions() {
    let pf = parse_fixture("src/main.rs");

    let run_fn = pf
        .functions
        .iter()
        .find(|f| f.name == "run")
        .expect("should find 'run' function");
    assert!(run_fn.is_public);
    assert!(run_fn.is_async);

    let main_fn = pf
        .functions
        .iter()
        .find(|f| f.name == "main")
        .expect("should find 'main' function");
    assert!(!main_fn.is_public);
    assert!(!main_fn.is_async);
}

#[test]
fn main_rs_exports() {
    let pf = parse_fixture("src/main.rs");

    // pub async fn run should be exported
    assert!(pf.exports.iter().any(|e| e.name == "run"));
    // fn main should NOT be exported
    assert!(!pf.exports.iter().any(|e| e.name == "main"));
}

// ---------------------------------------------------------------------------
// config.rs
// ---------------------------------------------------------------------------

#[test]
fn config_rs_imports() {
    let pf = parse_fixture("src/config.rs");
    assert!(pf.imports.iter().any(|i| i.module.contains("std::path")));
}

#[test]
fn config_rs_types() {
    let pf = parse_fixture("src/config.rs");

    let config_type = pf
        .types
        .iter()
        .find(|t| t.name == "Config")
        .expect("should find Config struct");
    assert_eq!(config_type.kind, TypeDefKind::Struct);
    assert!(config_type.is_public);

    let error_type = pf
        .types
        .iter()
        .find(|t| t.name == "ConfigError")
        .expect("should find ConfigError struct");
    assert_eq!(error_type.kind, TypeDefKind::Struct);
    assert!(error_type.is_public);

    let alias = pf
        .types
        .iter()
        .find(|t| t.name == "ConfigResult")
        .expect("should find ConfigResult type alias");
    assert_eq!(alias.kind, TypeDefKind::TypeAlias);
}

/// Schema v8: every TypeDef and Export carries an `end_line` covering the
/// full source range of the declaration. Multi-line declarations (struct
/// bodies) end past their `line`; single-line type aliases land on the
/// same row.
#[test]
fn config_rs_typedef_and_export_end_lines() {
    let pf = parse_fixture("src/config.rs");

    // pub struct Config — multi-line struct body (lines 6..=10 in fixture)
    let config_type = pf
        .types
        .iter()
        .find(|t| t.name == "Config")
        .expect("should find Config struct");
    assert!(
        config_type.end_line > config_type.line,
        "multi-line struct body should have end_line > line, \
         got line={} end_line={}",
        config_type.line,
        config_type.end_line
    );

    // pub type ConfigResult<T> = Result<T, ConfigError>; — single-line
    let alias = pf
        .types
        .iter()
        .find(|t| t.name == "ConfigResult")
        .expect("should find ConfigResult type alias");
    assert_eq!(
        alias.end_line, alias.line,
        "single-line type alias should have end_line == line, \
         got line={} end_line={}",
        alias.line, alias.end_line
    );

    // The exported `Config` symbol mirrors the TypeDef range exactly because
    // the rust parser feeds td.line/td.end_line straight into the Export.
    let config_export = pf
        .exports
        .iter()
        .find(|e| e.name == "Config")
        .expect("should find Config export");
    assert_eq!(config_export.line, config_type.line);
    assert_eq!(config_export.end_line, config_type.end_line);
    assert!(config_export.end_line > config_export.line);
}

#[test]
fn config_rs_derive_macros() {
    let pf = parse_fixture("src/config.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    let config_derive = ir
        .derive_macros
        .iter()
        .find(|d| d.type_name == "Config")
        .expect("should find derive for Config");
    assert!(config_derive.derives.contains(&"Debug".to_string()));
    assert!(config_derive.derives.contains(&"Clone".to_string()));
    assert!(config_derive.derives.contains(&"Serialize".to_string()));
    assert!(config_derive.derives.contains(&"Deserialize".to_string()));
}

#[test]
fn config_rs_trait_impl() {
    let pf = parse_fixture("src/config.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    assert!(
        ir.trait_implementations
            .iter()
            .any(|ti| ti.trait_name == "Default" && ti.type_name == "Config")
    );
}

#[test]
fn config_rs_impl_methods() {
    let pf = parse_fixture("src/config.rs");

    assert!(pf.functions.iter().any(|f| f.name == "new" && f.is_public));
    assert!(pf.functions.iter().any(|f| f.name == "load" && f.is_public));
    assert!(
        pf.functions
            .iter()
            .any(|f| f.name == "validate" && !f.is_public)
    );
}

#[test]
fn config_rs_error_types() {
    let pf = parse_fixture("src/config.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    assert!(ir.error_types.contains(&"ConfigError".to_string()));
}

// ---------------------------------------------------------------------------
// error.rs
// ---------------------------------------------------------------------------

#[test]
fn error_rs_error_types() {
    let pf = parse_fixture("src/error.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    assert!(ir.error_types.contains(&"AppError".to_string()));
}

#[test]
fn error_rs_types() {
    let pf = parse_fixture("src/error.rs");

    let app_error = pf
        .types
        .iter()
        .find(|t| t.name == "AppError")
        .expect("should find AppError enum");
    assert_eq!(app_error.kind, TypeDefKind::Enum);
    assert!(app_error.is_public);

    // type alias: pub type Result<T> = ...
    let result_alias = pf
        .types
        .iter()
        .find(|t| t.name == "Result")
        .expect("should find Result type alias");
    assert_eq!(result_alias.kind, TypeDefKind::TypeAlias);
}

#[test]
fn error_rs_trait_impls() {
    let pf = parse_fixture("src/error.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    // impl Display for AppError
    assert!(
        ir.trait_implementations
            .iter()
            .any(|ti| ti.trait_name.contains("Display") && ti.type_name == "AppError")
    );

    // impl From<io::Error> for AppError
    assert!(
        ir.trait_implementations
            .iter()
            .any(|ti| ti.trait_name.contains("From") && ti.type_name == "AppError")
    );
}

// ---------------------------------------------------------------------------
// server.rs
// ---------------------------------------------------------------------------

#[test]
fn server_rs_trait() {
    let pf = parse_fixture("src/server.rs");

    let handler = pf
        .types
        .iter()
        .find(|t| t.name == "Handler")
        .expect("should find Handler trait");
    assert_eq!(handler.kind, TypeDefKind::Trait);
    assert!(handler.is_public);
}

#[test]
fn server_rs_struct() {
    let pf = parse_fixture("src/server.rs");

    let echo = pf
        .types
        .iter()
        .find(|t| t.name == "EchoServer")
        .expect("should find EchoServer struct");
    assert_eq!(echo.kind, TypeDefKind::Struct);
    assert!(echo.is_public);
}

#[test]
fn server_rs_trait_impl() {
    let pf = parse_fixture("src/server.rs");
    let ir = match &pf.language_ir {
        LanguageIR::Rust(ir) => ir,
        _ => panic!("expected RustIR"),
    };

    assert!(
        ir.trait_implementations
            .iter()
            .any(|ti| ti.trait_name == "Handler" && ti.type_name == "EchoServer")
    );
}

#[test]
fn server_rs_async_method() {
    let pf = parse_fixture("src/server.rs");

    let start_fn = pf
        .functions
        .iter()
        .find(|f| f.name == "start")
        .expect("should find 'start' method");
    assert!(start_fn.is_async);
    assert!(start_fn.is_public);
}

#[test]
fn server_rs_private_method() {
    let pf = parse_fixture("src/server.rs");

    let log_fn = pf
        .functions
        .iter()
        .find(|f| f.name == "log")
        .expect("should find 'log' method");
    assert!(!log_fn.is_public);
}

#[test]
fn server_rs_wildcard_import() {
    let pf = parse_fixture("src/server.rs");

    let io_import = pf
        .imports
        .iter()
        .find(|i| i.module.contains("std::io"))
        .expect("should find std::io wildcard import");
    assert!(io_import.names.contains(&"*".to_string()));
}

// ---------------------------------------------------------------------------
// Cross-cutting concerns
// ---------------------------------------------------------------------------

#[test]
fn all_fixtures_have_content_hash() {
    for rel in &[
        "src/main.rs",
        "src/config.rs",
        "src/error.rs",
        "src/server.rs",
    ] {
        let pf = parse_fixture(rel);
        assert!(
            !pf.content_hash.is_empty(),
            "{rel} should have a content hash"
        );
        assert_eq!(
            pf.content_hash.len(),
            64,
            "{rel} hash should be 64 hex chars"
        );
    }
}

#[test]
fn all_fixtures_are_rust_language() {
    for rel in &[
        "src/main.rs",
        "src/config.rs",
        "src/error.rs",
        "src/server.rs",
    ] {
        let pf = parse_fixture(rel);
        assert_eq!(pf.language, Language::Rust, "{rel} should be Rust");
        assert!(
            matches!(pf.language_ir, LanguageIR::Rust(_)),
            "{rel} should have RustIR"
        );
    }
}

#[test]
fn parsing_errors_gracefully_degraded() {
    // Malformed Rust should not panic — it should still produce a ProjectFile
    let source = "fn invalid( { struct }}}";
    let path = Path::new("broken.rs");
    let pf = parse_file(path, source, Language::Rust);
    assert_eq!(pf.language, Language::Rust);
    assert!(!pf.content_hash.is_empty());
    // The file should still parse (tree-sitter is error-tolerant), though IR may be partial
}