Skip to content

Commit 59f1d4c

Browse files
authored
Add import tests for @roo-code/types, fix the build (#4060)
1 parent 6370131 commit 59f1d4c

File tree

6 files changed

+209
-7
lines changed

6 files changed

+209
-7
lines changed

packages/types/package.json

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
{
22
"name": "@roo-code/types",
3-
"version": "1.15.0",
3+
"version": "1.16.0",
44
"description": "TypeScript type definitions for Roo Code.",
55
"publishConfig": {
6-
"access": "public"
6+
"access": "public",
7+
"main": "./dist/index.js",
8+
"module": "./dist/index.mjs",
9+
"types": "./dist/index.d.ts",
10+
"exports": {
11+
".": {
12+
"types": "./dist/index.d.ts",
13+
"import": "./dist/index.mjs",
14+
"require": "./dist/index.js"
15+
}
16+
}
717
},
818
"author": "Roo Code Team",
919
"license": "MIT",
@@ -20,13 +30,11 @@
2030
"roo-code",
2131
"ai"
2232
],
23-
"main": "./dist/index.cjs",
24-
"module": "./dist/index.mjs",
25-
"types": "./dist/index.d.ts",
33+
"main": "./dist/index.js",
2634
"exports": {
2735
".": {
28-
"types": "./dist/index.d.ts",
29-
"import": "./dist/index.mjs",
36+
"types": "./src/index.ts",
37+
"import": "./src/index.ts",
3038
"require": {
3139
"types": "./dist/index.d.ts",
3240
"default": "./dist/index.js"
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
// npx vitest run src/__tests__/cjs-import.test.ts
2+
3+
import { resolve } from "path"
4+
5+
describe("CommonJS Import Tests", () => {
6+
const packageRoot = resolve(__dirname, "../..")
7+
const cjsPath = resolve(packageRoot, "dist", "index.js")
8+
9+
it("should import types using require() syntax", () => {
10+
// Clear require cache to ensure fresh import.
11+
delete require.cache[cjsPath]
12+
13+
// Use require to test CJS functionality.
14+
// eslint-disable-next-line @typescript-eslint/no-require-imports
15+
const module = require(cjsPath)
16+
17+
// Verify that key exports are available
18+
expect(module.GLOBAL_STATE_KEYS).toBeDefined()
19+
expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
20+
expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
21+
})
22+
23+
it("should import specific exports using destructuring", () => {
24+
// Clear require cache.
25+
delete require.cache[cjsPath]
26+
27+
// Test destructured require.
28+
// eslint-disable-next-line @typescript-eslint/no-require-imports
29+
const { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } = require(cjsPath)
30+
31+
expect(GLOBAL_STATE_KEYS).toBeDefined()
32+
expect(SECRET_STATE_KEYS).toBeDefined()
33+
expect(Array.isArray(GLOBAL_STATE_KEYS)).toBe(true)
34+
expect(Array.isArray(SECRET_STATE_KEYS)).toBe(true)
35+
})
36+
37+
it("should have default export available", () => {
38+
// Clear require cache
39+
delete require.cache[cjsPath]
40+
41+
// eslint-disable-next-line @typescript-eslint/no-require-imports
42+
const module = require(cjsPath)
43+
44+
// Check if module has expected structure
45+
expect(typeof module).toBe("object")
46+
expect(module).not.toBeNull()
47+
})
48+
49+
it("should maintain consistency between multiple require calls", () => {
50+
// Clear require cache first.
51+
delete require.cache[cjsPath]
52+
53+
// Multiple require calls should return the same cached module.
54+
// eslint-disable-next-line @typescript-eslint/no-require-imports
55+
const firstRequire = require(cjsPath)
56+
57+
// eslint-disable-next-line @typescript-eslint/no-require-imports
58+
const secondRequire = require(cjsPath)
59+
60+
// Should be the exact same object (cached).
61+
expect(firstRequire).toBe(secondRequire)
62+
expect(firstRequire.GLOBAL_STATE_KEYS).toBe(secondRequire.GLOBAL_STATE_KEYS)
63+
})
64+
})
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// npx vitest run src/__tests__/esm-import.test.ts
2+
3+
describe("ESM Import Tests", () => {
4+
it("should import types using ESM syntax", async () => {
5+
// Dynamic import to test ESM functionality.
6+
const module = await import("../index.js")
7+
8+
// Verify that key exports are available.
9+
expect(module.GLOBAL_STATE_KEYS).toBeDefined()
10+
expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
11+
expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
12+
})
13+
14+
it("should import specific exports using ESM syntax", async () => {
15+
// Test named imports.
16+
const { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } = await import("../index.js")
17+
18+
expect(GLOBAL_STATE_KEYS).toBeDefined()
19+
expect(SECRET_STATE_KEYS).toBeDefined()
20+
expect(Array.isArray(GLOBAL_STATE_KEYS)).toBe(true)
21+
expect(Array.isArray(SECRET_STATE_KEYS)).toBe(true)
22+
})
23+
24+
it("should have consistent exports between static and dynamic imports", async () => {
25+
// Static import.
26+
const staticImport = await import("../index.js")
27+
28+
// Dynamic import.
29+
const dynamicImport = await import("../index.js")
30+
31+
// Both should have the same exports.
32+
expect(Object.keys(staticImport)).toEqual(Object.keys(dynamicImport))
33+
expect(staticImport.GLOBAL_STATE_KEYS).toEqual(dynamicImport.GLOBAL_STATE_KEYS)
34+
})
35+
})
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
// npx vitest run src/__tests__/package-exports.test.ts
2+
3+
import { resolve } from "path"
4+
5+
describe("Package Exports Integration Tests", () => {
6+
const packageRoot = resolve(__dirname, "../..")
7+
const distPath = resolve(packageRoot, "dist")
8+
9+
it("should import from built ESM file", async () => {
10+
const esmPath = resolve(distPath, "index.mjs")
11+
12+
// Dynamic import of the built ESM file
13+
const module = await import(esmPath)
14+
15+
expect(module.GLOBAL_STATE_KEYS).toBeDefined()
16+
expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
17+
expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
18+
})
19+
20+
it("should import from built CJS file", () => {
21+
const cjsPath = resolve(distPath, "index.js")
22+
23+
// Clear require cache to ensure fresh import
24+
delete require.cache[cjsPath]
25+
26+
// Require the built CJS file
27+
// eslint-disable-next-line @typescript-eslint/no-require-imports
28+
const module = require(cjsPath)
29+
30+
expect(module.GLOBAL_STATE_KEYS).toBeDefined()
31+
expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
32+
expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
33+
})
34+
35+
it("should have consistent exports between ESM and CJS builds", async () => {
36+
const esmPath = resolve(distPath, "index.mjs")
37+
const cjsPath = resolve(distPath, "index.js")
38+
39+
// Clear require cache.
40+
delete require.cache[cjsPath]
41+
42+
// Import both versions.
43+
const esmModule = await import(esmPath)
44+
// eslint-disable-next-line @typescript-eslint/no-require-imports
45+
const cjsModule = require(cjsPath)
46+
47+
// Compare key exports.
48+
expect(esmModule.GLOBAL_STATE_KEYS).toEqual(cjsModule.GLOBAL_STATE_KEYS)
49+
expect(esmModule.SECRET_STATE_KEYS).toEqual(cjsModule.SECRET_STATE_KEYS)
50+
51+
// Ensure both have the same export keys.
52+
const esmKeys = Object.keys(esmModule).sort()
53+
const cjsKeys = Object.keys(cjsModule).sort()
54+
expect(esmKeys).toEqual(cjsKeys)
55+
})
56+
57+
it("should import using package name resolution (simulated)", async () => {
58+
// This simulates how the package would be imported by consumers.
59+
// We test the source files since we can't easily test the published package.
60+
const module = await import("../index.js")
61+
62+
// Verify the main exports that consumers would use.
63+
expect(module.GLOBAL_STATE_KEYS).toBeDefined()
64+
expect(module.SECRET_STATE_KEYS).toBeDefined()
65+
66+
// Test some common type exports exist.
67+
expect(typeof module.GLOBAL_STATE_KEYS).toBe("object")
68+
expect(typeof module.SECRET_STATE_KEYS).toBe("object")
69+
})
70+
71+
it("should have TypeScript definitions available", () => {
72+
const dtsPath = resolve(distPath, "index.d.ts")
73+
// eslint-disable-next-line @typescript-eslint/no-require-imports
74+
const fs = require("fs")
75+
76+
// Check that the .d.ts file exists and has content.
77+
expect(fs.existsSync(dtsPath)).toBe(true)
78+
79+
const dtsContent = fs.readFileSync(dtsPath, "utf8")
80+
expect(dtsContent.length).toBeGreaterThan(0)
81+
expect(dtsContent).toContain("export")
82+
})
83+
})

packages/types/tsup.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,9 @@ export default defineConfig({
88
splitting: false,
99
sourcemap: true,
1010
outDir: "dist",
11+
outExtension({ format }) {
12+
return {
13+
js: format === "cjs" ? ".js" : ".mjs",
14+
}
15+
},
1116
})

packages/types/vitest.config.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { defineConfig } from "vitest/config"
2+
3+
export default defineConfig({
4+
test: {
5+
globals: true,
6+
},
7+
})

0 commit comments

Comments
 (0)