Browse Source

Add import tests for @roo-code/types, fix the build (#4060)

Chris Estreich 9 months ago
parent
commit
59f1d4c529

+ 15 - 7
packages/types/package.json

@@ -1,9 +1,19 @@
 {
 	"name": "@roo-code/types",
-	"version": "1.15.0",
+	"version": "1.16.0",
 	"description": "TypeScript type definitions for Roo Code.",
 	"publishConfig": {
-		"access": "public"
+		"access": "public",
+		"main": "./dist/index.js",
+		"module": "./dist/index.mjs",
+		"types": "./dist/index.d.ts",
+		"exports": {
+			".": {
+				"types": "./dist/index.d.ts",
+				"import": "./dist/index.mjs",
+				"require": "./dist/index.js"
+			}
+		}
 	},
 	"author": "Roo Code Team",
 	"license": "MIT",
@@ -20,13 +30,11 @@
 		"roo-code",
 		"ai"
 	],
-	"main": "./dist/index.cjs",
-	"module": "./dist/index.mjs",
-	"types": "./dist/index.d.ts",
+	"main": "./dist/index.js",
 	"exports": {
 		".": {
-			"types": "./dist/index.d.ts",
-			"import": "./dist/index.mjs",
+			"types": "./src/index.ts",
+			"import": "./src/index.ts",
 			"require": {
 				"types": "./dist/index.d.ts",
 				"default": "./dist/index.js"

+ 64 - 0
packages/types/src/__tests__/cjs-import.test.ts

@@ -0,0 +1,64 @@
+// npx vitest run src/__tests__/cjs-import.test.ts
+
+import { resolve } from "path"
+
+describe("CommonJS Import Tests", () => {
+	const packageRoot = resolve(__dirname, "../..")
+	const cjsPath = resolve(packageRoot, "dist", "index.js")
+
+	it("should import types using require() syntax", () => {
+		// Clear require cache to ensure fresh import.
+		delete require.cache[cjsPath]
+
+		// Use require to test CJS functionality.
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const module = require(cjsPath)
+
+		// Verify that key exports are available
+		expect(module.GLOBAL_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
+		expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
+	})
+
+	it("should import specific exports using destructuring", () => {
+		// Clear require cache.
+		delete require.cache[cjsPath]
+
+		// Test destructured require.
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } = require(cjsPath)
+
+		expect(GLOBAL_STATE_KEYS).toBeDefined()
+		expect(SECRET_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(GLOBAL_STATE_KEYS)).toBe(true)
+		expect(Array.isArray(SECRET_STATE_KEYS)).toBe(true)
+	})
+
+	it("should have default export available", () => {
+		// Clear require cache
+		delete require.cache[cjsPath]
+
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const module = require(cjsPath)
+
+		// Check if module has expected structure
+		expect(typeof module).toBe("object")
+		expect(module).not.toBeNull()
+	})
+
+	it("should maintain consistency between multiple require calls", () => {
+		// Clear require cache first.
+		delete require.cache[cjsPath]
+
+		// Multiple require calls should return the same cached module.
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const firstRequire = require(cjsPath)
+
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const secondRequire = require(cjsPath)
+
+		// Should be the exact same object (cached).
+		expect(firstRequire).toBe(secondRequire)
+		expect(firstRequire.GLOBAL_STATE_KEYS).toBe(secondRequire.GLOBAL_STATE_KEYS)
+	})
+})

+ 35 - 0
packages/types/src/__tests__/esm-import.test.ts

@@ -0,0 +1,35 @@
+// npx vitest run src/__tests__/esm-import.test.ts
+
+describe("ESM Import Tests", () => {
+	it("should import types using ESM syntax", async () => {
+		// Dynamic import to test ESM functionality.
+		const module = await import("../index.js")
+
+		// Verify that key exports are available.
+		expect(module.GLOBAL_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
+		expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
+	})
+
+	it("should import specific exports using ESM syntax", async () => {
+		// Test named imports.
+		const { GLOBAL_STATE_KEYS, SECRET_STATE_KEYS } = await import("../index.js")
+
+		expect(GLOBAL_STATE_KEYS).toBeDefined()
+		expect(SECRET_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(GLOBAL_STATE_KEYS)).toBe(true)
+		expect(Array.isArray(SECRET_STATE_KEYS)).toBe(true)
+	})
+
+	it("should have consistent exports between static and dynamic imports", async () => {
+		// Static import.
+		const staticImport = await import("../index.js")
+
+		// Dynamic import.
+		const dynamicImport = await import("../index.js")
+
+		// Both should have the same exports.
+		expect(Object.keys(staticImport)).toEqual(Object.keys(dynamicImport))
+		expect(staticImport.GLOBAL_STATE_KEYS).toEqual(dynamicImport.GLOBAL_STATE_KEYS)
+	})
+})

+ 83 - 0
packages/types/src/__tests__/package-exports.test.ts

@@ -0,0 +1,83 @@
+// npx vitest run src/__tests__/package-exports.test.ts
+
+import { resolve } from "path"
+
+describe("Package Exports Integration Tests", () => {
+	const packageRoot = resolve(__dirname, "../..")
+	const distPath = resolve(packageRoot, "dist")
+
+	it("should import from built ESM file", async () => {
+		const esmPath = resolve(distPath, "index.mjs")
+
+		// Dynamic import of the built ESM file
+		const module = await import(esmPath)
+
+		expect(module.GLOBAL_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
+		expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
+	})
+
+	it("should import from built CJS file", () => {
+		const cjsPath = resolve(distPath, "index.js")
+
+		// Clear require cache to ensure fresh import
+		delete require.cache[cjsPath]
+
+		// Require the built CJS file
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const module = require(cjsPath)
+
+		expect(module.GLOBAL_STATE_KEYS).toBeDefined()
+		expect(Array.isArray(module.GLOBAL_STATE_KEYS)).toBe(true)
+		expect(module.GLOBAL_STATE_KEYS.length).toBeGreaterThan(0)
+	})
+
+	it("should have consistent exports between ESM and CJS builds", async () => {
+		const esmPath = resolve(distPath, "index.mjs")
+		const cjsPath = resolve(distPath, "index.js")
+
+		// Clear require cache.
+		delete require.cache[cjsPath]
+
+		// Import both versions.
+		const esmModule = await import(esmPath)
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const cjsModule = require(cjsPath)
+
+		// Compare key exports.
+		expect(esmModule.GLOBAL_STATE_KEYS).toEqual(cjsModule.GLOBAL_STATE_KEYS)
+		expect(esmModule.SECRET_STATE_KEYS).toEqual(cjsModule.SECRET_STATE_KEYS)
+
+		// Ensure both have the same export keys.
+		const esmKeys = Object.keys(esmModule).sort()
+		const cjsKeys = Object.keys(cjsModule).sort()
+		expect(esmKeys).toEqual(cjsKeys)
+	})
+
+	it("should import using package name resolution (simulated)", async () => {
+		// This simulates how the package would be imported by consumers.
+		// We test the source files since we can't easily test the published package.
+		const module = await import("../index.js")
+
+		// Verify the main exports that consumers would use.
+		expect(module.GLOBAL_STATE_KEYS).toBeDefined()
+		expect(module.SECRET_STATE_KEYS).toBeDefined()
+
+		// Test some common type exports exist.
+		expect(typeof module.GLOBAL_STATE_KEYS).toBe("object")
+		expect(typeof module.SECRET_STATE_KEYS).toBe("object")
+	})
+
+	it("should have TypeScript definitions available", () => {
+		const dtsPath = resolve(distPath, "index.d.ts")
+		// eslint-disable-next-line @typescript-eslint/no-require-imports
+		const fs = require("fs")
+
+		// Check that the .d.ts file exists and has content.
+		expect(fs.existsSync(dtsPath)).toBe(true)
+
+		const dtsContent = fs.readFileSync(dtsPath, "utf8")
+		expect(dtsContent.length).toBeGreaterThan(0)
+		expect(dtsContent).toContain("export")
+	})
+})

+ 5 - 0
packages/types/tsup.config.ts

@@ -8,4 +8,9 @@ export default defineConfig({
 	splitting: false,
 	sourcemap: true,
 	outDir: "dist",
+	outExtension({ format }) {
+		return {
+			js: format === "cjs" ? ".js" : ".mjs",
+		}
+	},
 })

+ 7 - 0
packages/types/vitest.config.ts

@@ -0,0 +1,7 @@
+import { defineConfig } from "vitest/config"
+
+export default defineConfig({
+	test: {
+		globals: true,
+	},
+})