blob: 5de84230e98ebf32dc22ea79c054f71f391f7ea1 [file] [log] [blame]
gioc31bf142025-06-16 07:48:20 +00001import { NodeJSAnalyzer } from "./nodejs.js";
2import { FileSystem, RealFileSystem } from "./fs.js";
gioa71316d2025-05-24 09:41:36 +04003import { Volume, IFs, createFsFromVolume } from "memfs";
4import { test, expect } from "@jest/globals";
gioc31bf142025-06-16 07:48:20 +00005import { expandValue } from "./env.js";
gioa71316d2025-05-24 09:41:36 +04006import shell from "shelljs";
7
8class InMemoryFileSystem implements FileSystem {
9 constructor(private readonly fs: IFs) {}
10
11 exists(path: string): boolean {
12 return this.fs.existsSync(path);
13 }
14
15 // TODO(gio): add encoding
16 async readFile(path: string, encoding?: BufferEncoding): Promise<string> {
17 const contents = await this.fs.promises.readFile(path);
18 return contents.toString(encoding);
19 }
20}
21
22test("canvas", async () => {
23 const fs: FileSystem = new RealFileSystem("/home/gio/code/apps/canvas/back");
24 const analyzer = new NodeJSAnalyzer();
25 expect(analyzer.detect(fs, "/")).toBe(true);
26 const info = await analyzer.analyze(fs, "/");
27 console.log(info);
28});
29
30test("nodejs", async () => {
31 return;
32 const root = "/";
33 const vol = Volume.fromNestedJSON(
34 {
35 "package.json": JSON.stringify({
36 name: "test",
37 version: "1.0.0",
38 dependencies: {
39 prisma: "6.6.0",
40 },
41 }),
42 "package-lock.json": "fake",
43 "prisma/schema.prisma": `
44 datasource db {
45 provider = "sqlite"
46 url = env("DB_URL")
47 }
48 `,
49 },
50 root,
51 );
52 const fs: FileSystem = new InMemoryFileSystem(createFsFromVolume(vol));
53 const analyzer = new NodeJSAnalyzer();
54 expect(analyzer.detect(fs, root)).toBe(true);
55 const info = await analyzer.analyze(fs, root);
56 console.log(info);
57});
58
59test("env", () => {
60 console.log(expandValue("${PORT} ${DODO_VOLUME_DB}"));
61 console.log(expandValue("$PORT $DODO_VOLUME_DB"));
62 console.log(expandValue("${UNDEFINED:-${MACHINE}${UNDEFINED:-default}}"));
63});
64
65test("clone", async () => {
66 expect(shell.which("ssh-agent")).toBeTruthy();
67 expect(shell.which("ssh-add")).toBeTruthy();
68 expect(shell.which("git")).toBeTruthy();
69 expect(
70 shell.exec(
71 "GIT_SSH_COMMAND='ssh -i /home/gio/.ssh/key -o IdentitiesOnly=yes' git clone git@github.com:giolekva/dodo-blog.git /tmp/dodo-blog",
72 ).code,
73 ).toBe(0);
74 const fs: FileSystem = new RealFileSystem("/tmp/dodo-blog");
75 const analyzer = new NodeJSAnalyzer();
76 expect(analyzer.detect(fs, "/")).toBe(true);
77 const info = await analyzer.analyze(fs, "/");
78 console.log(info);
79});
80
81test("keygen", () => {
82 expect(shell.exec(`ssh-keygen -y -t ed25519 -f /tmp/key`).code).toBe(0);
83});