Files
oh-my-pi/packages/natives/test/native.test.ts
T
can1357 978b93ba62 refactor: migrated native tools to WASM-based implementation
- Consolidated grep, find, and image processing into a new `@oh-my-pi/pi-natives` WASM package.
- Eliminated subprocess overhead and simplified JS tool implementations.
- Updated CI to include Rust workspace, linting, formatting, and WASM build steps.
- Documented `Promise.withResolvers()` and `using` declarations for wasm-bindgen.
2026-01-29 09:29:21 +01:00

107 lines
2.2 KiB
TypeScript

import { beforeAll, describe, expect, it } from "bun:test";
import * as fs from "node:fs/promises";
import * as os from "node:os";
import * as path from "node:path";
import { type FindMatch, find, grep } from "../src/index";
let testDir: string;
async function setupFixtures() {
testDir = await fs.mkdtemp(path.join(os.tmpdir(), "natives-test-"));
await fs.writeFile(
path.join(testDir, "file1.ts"),
`export function hello() {
// TODO: implement
return "hello";
}
`,
);
await fs.writeFile(
path.join(testDir, "file2.ts"),
`export function world() {
// FIXME: fix this
return "world";
}
`,
);
await fs.writeFile(
path.join(testDir, "readme.md"),
`# Test README
This is a test file.
`,
);
}
async function cleanupFixtures() {
await fs.rm(testDir, { recursive: true, force: true });
}
describe("pi-natives", () => {
beforeAll(async () => {
await setupFixtures();
return async () => {
await cleanupFixtures();
};
});
describe("grep", () => {
it("should find patterns in files", async () => {
const result = await grep({
pattern: "TODO",
path: testDir,
});
expect(result.totalMatches).toBe(1);
expect(result.matches.length).toBe(1);
expect(result.matches[0].line).toContain("TODO");
});
it("should respect glob patterns", async () => {
const result = await grep({
pattern: "test",
path: testDir,
glob: "*.md",
ignoreCase: true,
});
expect(result.totalMatches).toBe(2); // "Test" in title + "test" in body
});
it("should return filesWithMatches mode", async () => {
const result = await grep({
pattern: "return",
path: testDir,
mode: "filesWithMatches",
});
expect(result.filesWithMatches).toBeGreaterThan(0);
});
});
describe("find", () => {
it("should find files matching pattern", async () => {
const result = await find({
pattern: "*.ts",
path: testDir,
});
expect(result.totalMatches).toBe(2);
expect(result.matches.every((m: FindMatch) => m.path.endsWith(".ts"))).toBe(true);
});
it("should filter by file type", async () => {
const result = await find({
pattern: "*",
path: testDir,
fileType: "file",
});
expect(result.totalMatches).toBe(3);
});
});
});