import { test, expect, vi } from 'vitest'
import { types } from 'node:util'
function shape(namespace) {
 return { nativeBrand: types.isModuleNamespaceObject(namespace), keys: Reflect.ownKeys(namespace).map(key => typeof key === 'symbol' ? String(key) : key), prototypeNull: Object.getPrototypeOf(namespace) === null, tag: namespace[Symbol.toStringTag] ?? null, extensible: Object.isExtensible(namespace), sealed: Object.isSealed(namespace), frozen: Object.isFrozen(namespace), descriptors: Reflect.ownKeys(namespace).map(key => { const d = Object.getOwnPropertyDescriptor(namespace,key); return { key: typeof key === 'symbol' ? String(key) : key, valueType: typeof d.value, get: typeof d.get, set: typeof d.set, writable: d.writable ?? null, enumerable: d.enumerable, configurable: d.configurable } }) }
}
test('observes the actual CommonJS require cache for direct-dependency across repeated resets', async () => {
 const first = await import('./root.cjs')
 const cached = await import('./root.cjs')
 const dependencyFirst = await import('./dependency.cjs')
 vi.resetModules()
 const second = await import('./root.cjs')
 const dependencySecond = await import('./dependency.cjs')
 vi.resetModules()
 const third = await import('./root.cjs')
 const namespaces = [first, second, third]
 const roots = namespaces.map(ns => ns.default)
 console.log('WS2_EXTERNAL_CJS_REQUIRE_DIRECT_DEPENDENCY:' + JSON.stringify({ cached: first === cached, namespaceIdentity: [first === second, second === third], rootIdentity: [roots[0] === roots[1], roots[1] === roots[2]], dependencyIdentity: [roots[0].dependency === roots[1].dependency, roots[1].dependency === roots[2].dependency], rootValues: roots.map(root => root.evaluation), dependencyValues: roots.map(root => root.dependency.evaluation), reads: roots.map(root => root.read()), rootSourceEvaluations: globalThis.__ws2CjsDependencyRoot, dependencySourceEvaluations: globalThis.__ws2CjsDependencyLeaf, namespaceShapes: namespaces.map(shape), rootShapes: roots.map(shape), dependencyShapes: roots.map(root => shape(root.dependency)), direct: { namespaceSame: dependencyFirst === dependencySecond, defaultSame: dependencyFirst.default === dependencySecond.default, values: [dependencyFirst.default.evaluation, dependencySecond.default.evaluation], sourceAndRequired: [dependencyFirst.default === roots[0].dependency, dependencySecond.default === roots[1].dependency], namespaceShapes: [shape(dependencyFirst), shape(dependencySecond)], defaultShapes: [shape(dependencyFirst.default), shape(dependencySecond.default)] } }))
 expect(first).toBe(cached)
 expect(roots[1]).not.toBe(roots[0])
 expect(roots[2]).not.toBe(roots[1])
})
