Compare commits

...

4 Commits

Author SHA1 Message Date
Alex Yang fbe22d5ce3 test: import 2025-02-11 14:44:56 +08:00
Alex Yang f9aa7624cb test: remove 2025-02-11 14:40:11 +08:00
Alex Yang 1e5007c33a test: cleanup 2025-02-11 14:39:14 +08:00
Alex Yang 245821a690 test: smoke test with cjs/esm dual package 2025-02-11 14:33:48 +08:00
2 changed files with 87 additions and 0 deletions
+1
View File
@@ -1 +1,2 @@
logs
.temp
+86
View File
@@ -0,0 +1,86 @@
import { execSync } from "node:child_process";
import { mkdir, rm, writeFile } from "node:fs/promises";
import { resolve } from "node:path";
import { test } from "node:test";
import { testRootDir } from "./utils.js";
await test("cjs/esm dual module check", async (t) => {
const esmImports = `import fs from 'node:fs/promises'
import { Document, MetadataMode, VectorStoreIndex } from 'llamaindex'
import { OpenAIEmbedding } from '@llamaindex/openai'
import { Settings } from '@llamaindex/core/global'`;
const cjsRequire = `const fs = require('fs').promises
const { Document, MetadataMode, VectorStoreIndex } = require('llamaindex')
const { OpenAIEmbedding } = require('@llamaindex/openai')
const { Settings } = require('@llamaindex/core/global')`;
const mainCode = `
async function main() {
Settings.embedModel = new OpenAIEmbedding({
model: 'text-embedding-3-small',
apiKey: '${process.env.OPENAI_API_KEY}',
})
const model = Settings.embedModel
if (model == null) {
process.exit(-1)
}
}
main().catch(console.error)`;
t.before(async () => {
await mkdir(resolve(testRootDir, ".temp"), {
recursive: true,
mode: 0o755,
});
});
t.after(async () => {
await rm(resolve(testRootDir, ".temp"), {
recursive: true,
force: true,
});
});
await t.test("cjs", async () => {
const cjsCode = `${cjsRequire}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.cjs`,
);
await writeFile(filePath, cjsCode, "utf-8");
execSync(`${process.argv[0]} ${filePath}`, {
cwd: process.cwd(),
});
});
await t.test("esm", async () => {
const esmCode = `${esmImports}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.mjs`,
);
await writeFile(filePath, esmCode, "utf-8");
execSync(`${process.argv[0]} ${filePath}`, {
cwd: process.cwd(),
});
});
const specialConditions = ["edge-light", "workerd", "react-server"];
for (const condition of specialConditions) {
await t.test(condition, async () => {
const esmCode = `${esmImports}\n${mainCode}`;
const filePath = resolve(
testRootDir,
".temp",
`${crypto.randomUUID()}.mjs`,
);
await writeFile(filePath, esmCode, "utf-8");
execSync(`${process.argv[0]} ${filePath} -C ${condition}`, {
cwd: process.cwd(),
});
});
}
});