import { NodeJSAnalyzer } from "./nodejs.js";
import { FileSystem } from "./fs.js";
import { Volume, IFs, createFsFromVolume } from "memfs";
import { test, expect } from "@jest/globals";

class InMemoryFileSystem implements FileSystem {
	constructor(private readonly fs: IFs) {}

	exists(path: string): boolean {
		return this.fs.existsSync(path);
	}

	// TODO(gio): add encoding
	async readFile(path: string, encoding?: BufferEncoding): Promise<string> {
		const contents = await this.fs.promises.readFile(path);
		return contents.toString(encoding);
	}
}

test("nodejs", async () => {
	return;
	const root = "/";
	const vol = Volume.fromNestedJSON(
		{
			"package.json": JSON.stringify({
				name: "test",
				version: "1.0.0",
				dependencies: {
					prisma: "6.6.0",
				},
			}),
			"package-lock.json": "fake",
			"prisma/schema.prisma": `
				datasource db {
					provider = "sqlite"
					url = env("DB_URL")
				}
			`,
		},
		root,
	);
	const fs: FileSystem = new InMemoryFileSystem(createFsFromVolume(vol));
	const analyzer = new NodeJSAnalyzer();
	expect(analyzer.detect(fs, root)).toBe(true);
	const info = await analyzer.analyze(fs, root);
	console.log(info);
});
