import test from 'node:test'; import assert from 'node:assert/strict'; import vm from 'node:vm'; import { injectMindSpaceAnalytics, pseudonymizeAnalyticsId, resolveAnalyticsOwnerSegment, resolveAnalyticsOwnerLabel, resolveMindSpaceAnalyticsConfig, sendMindSpaceAnalyticsEvent, } from './mindspace-analytics.mjs'; test('analytics config is disabled unless explicitly enabled and configured', () => { assert.equal(resolveMindSpaceAnalyticsConfig({ MEMIND_ANALYTICS_ENABLED: 'true' }).enabled, false); assert.equal(resolveMindSpaceAnalyticsConfig({ MEMIND_ANALYTICS_ENABLED: 'true', MEMIND_ANALYTICS_WEBSITE_ID: 'local-website', }).enabled, false); const config = resolveMindSpaceAnalyticsConfig({ MEMIND_ANALYTICS_ENABLED: 'true', MEMIND_ANALYTICS_URL: 'http://127.0.0.1:3200', MEMIND_ANALYTICS_WEBSITE_ID: 'local-website', MEMIND_ANALYTICS_ID_SECRET: 'local-secret', }); assert.equal(config.enabled, true); assert.equal(config.analyticsUrl, 'http://127.0.0.1:3200'); assert.equal(config.hostPath, '/analytics'); }); test('owner ids are stable pseudonyms and never expose the source id', () => { const first = pseudonymizeAnalyticsId('user-123', 'secret'); assert.equal(first, pseudonymizeAnalyticsId('user-123', 'secret')); assert.notEqual(first, 'user-123'); assert.notEqual(first, pseudonymizeAnalyticsId('user-456', 'secret')); }); test('owner segments come from server-side Memind user profile data', () => { assert.equal(resolveAnalyticsOwnerSegment({ role: 'admin' }), 'admin'); assert.equal(resolveAnalyticsOwnerSegment({ role: 'user', planType: 'pro' }), 'plan:pro'); assert.equal(resolveAnalyticsOwnerSegment({ role: 'user' }), 'plan:free'); }); test('owner labels are readable but bounded and stripped of control characters', () => { assert.equal(resolveAnalyticsOwnerLabel({ displayName: '张三\n管理员' }), '张三 管理员'); assert.equal(resolveAnalyticsOwnerLabel({}), '未命名用户'); }); test('injects one local same-origin tracker with page dimensions', () => { const html = '