Newer
Older
TelosDB / tests / scripts.test.js
import { describe, expect, test } from "bun:test";
import { spawnSync } from "child_process";
import fs from "fs";
import path from "path";

const TMP_TEST_DIR = "test/tmp_scripts";

describe("Scripts Rust Support Test", () => {
    // テスト用ディレクトリ作成
    if (!fs.existsSync(TMP_TEST_DIR)) {
        fs.mkdirSync(TMP_TEST_DIR, { recursive: true });
    }

    test("analyze_nesting.js identifies deep nesting in Rust", () => {
        const rustCode = `
        fn nested() {
            if true {
                if true {
                    if true {
                        match Some(1) {
                            Some(_) => {
                                println!("Deep!");
                            }
                            None => {}
                        }
                    }
                }
            }
        }
        `;
        const testFile = path.join(TMP_TEST_DIR, "test_deep.rs");
        fs.writeFileSync(testFile, rustCode);

        const result = spawnSync("node", ["scripts/analyze_nesting.js", testFile], { encoding: "utf-8" });
        expect(result.stdout).toContain("Level 5");
        expect(result.stdout).toContain("REFACTOR NEEDED");
    });

    test("count_lines.js counts logical lines in Rust properly", () => {
        const rustCode = `
        // Comment
        fn main() {
            /* Block
               Comment */
            let x = 1;

            println!("{}", x);
        }
        `;
        const testFile = path.join(TMP_TEST_DIR, "test_count.rs");
        fs.writeFileSync(testFile, rustCode);

        const result = spawnSync("node", ["scripts/count_lines.js", testFile], { encoding: "utf-8" });
        // fn main, let x, println, } = 4 logical lines (概算)
        // 実際の実装にあわせたアサーション
        expect(result.stdout).toContain("Logical : 4");
    });
});