Beautiful, AI-native markdown IDE and LLM wiki
TypeScript 2nd-brainagent-skillsclaudecodexcompany-braindocsknowledge-knowledge-baseknowledge-managementllmllm-wikillm-wiki-karpathymarkdownmarkdown-editormdmdxnotespkmsecond-brainskills
import { afterEach, beforeEach, describe, expect, test } from 'vitest';
import {
getParseHealth,
incrementBlockFallback,
incrementJsxActionAborted,
incrementJsxAutoConvertFailed,
incrementJsxChromeDeleteFailed,
incrementJsxKeyboardDeleteFailed,
incrementJsxRenderFailure,
incrementWholeDocFallback,
incrementYpsMismatchBlock,
incrementYpsMismatchInline,
resetParseHealth,
} from './parse-health.ts';
describe('parse-health metrics', () => {
beforeEach(() => resetParseHealth());
afterEach(() => resetParseHealth());
test('initial state is all zeros', () => {
const h = getParseHealth();
expect(h.parseFallback.blockLevel).toBe(0);
expect(h.parseFallback.wholeDoc).toBe(0);
expect(h.parseFallback.wholeDocBudget).toBe(0);
expect(h.ypsMismatch.block).toBe(0);
expect(h.ypsMismatch.inline).toBe(0);
expect(h.jsxRenderFailure).toEqual({});
expect(h.jsxAutoConvertFailed).toEqual({});
});
test('incrementBlockFallback increments blockLevel', () => {
incrementBlockFallback();
incrementBlockFallback();
expect(getParseHealth().parseFallback.blockLevel).toBe(2);
});
test('incrementWholeDocFallback increments wholeDoc', () => {
incrementWholeDocFallback();
expect(getParseHealth().parseFallback.wholeDoc).toBe(1);
});
test('incrementYpsMismatchBlock increments ypsMismatch.block', () => {
incrementYpsMismatchBlock();
incrementYpsMismatchBlock();
incrementYpsMismatchBlock();
expect(getParseHealth().ypsMismatch.block).toBe(3);
});
test('incrementYpsMismatchInline increments ypsMismatch.inline', () => {
incrementYpsMismatchInline();
expect(getParseHealth().ypsMismatch.inline).toBe(1);
});
test('getParseHealth returns a defensive copy', () => {
incrementBlockFallback();
const snap1 = getParseHealth();
incrementBlockFallback();
const snap2 = getParseHealth();
expect(snap1.parseFallback.blockLevel).toBe(1);
expect(snap2.parseFallback.blockLevel).toBe(2);
});
test('resetParseHealth resets all counters', () => {
incrementBlockFallback();
incrementWholeDocFallback();
incrementYpsMismatchBlock();
incrementYpsMismatchInline();
incrementJsxRenderFailure('Callout');
incrementJsxAutoConvertFailed('wildcard');
resetParseHealth();
const h = getParseHealth();
expect(h.parseFallback.blockLevel).toBe(0);
expect(h.parseFallback.wholeDoc).toBe(0);
expect(h.parseFallback.wholeDocBudget).toBe(0);
expect(h.ypsMismatch.block).toBe(0);
expect(h.ypsMismatch.inline).toBe(0);
expect(h.jsxRenderFailure).toEqual({});
expect(h.jsxAutoConvertFailed).toEqual({});
});
test('incrementJsxRenderFailure keys by clamped descriptor name', () => {
incrementJsxRenderFailure('Callout');
incrementJsxRenderFailure('Callout');
incrementJsxRenderFailure('img');
incrementJsxRenderFailure('wildcard');
const h = getParseHealth();
expect(h.jsxRenderFailure).toEqual({ Callout: 2, img: 1, wildcard: 1 });
});
test('incrementJsxAutoConvertFailed keys by clamped descriptor name', () => {
incrementJsxAutoConvertFailed('wildcard');
incrementJsxAutoConvertFailed('wildcard');
incrementJsxAutoConvertFailed('video');
const h = getParseHealth();
expect(h.jsxAutoConvertFailed).toEqual({ wildcard: 2, video: 1 });
});
test('getParseHealth returns defensive copies of jsx counter objects', () => {
incrementJsxRenderFailure('Callout');
const snap1 = getParseHealth();
incrementJsxRenderFailure('Callout');
const snap2 = getParseHealth();
expect(snap1.jsxRenderFailure.Callout).toBe(1);
expect(snap2.jsxRenderFailure.Callout).toBe(2);
});
test('action aborts are separate from conversion failures and reset without mutating snapshots', () => {
incrementJsxActionAborted('delete-chrome');
const snapshot = getParseHealth();
incrementJsxActionAborted('delete-chrome');
incrementJsxActionAborted('delete-keyboard');
expect(getParseHealth().jsxActionAborted).toEqual({
'delete-chrome': 2,
'delete-keyboard': 1,
});
expect(getParseHealth().jsxAutoConvertFailed).toEqual({});
resetParseHealth();
expect(getParseHealth().jsxActionAborted).toEqual({});
expect(snapshot.jsxActionAborted).toEqual({ 'delete-chrome': 1 });
});
test('chrome and keyboard delete failures remain separate and reset without mutating snapshots', () => {
incrementJsxChromeDeleteFailed('Callout');
incrementJsxKeyboardDeleteFailed('Callout');
incrementJsxChromeDeleteFailed('Callout');
const snapshot = getParseHealth();
expect(snapshot.jsxChromeDeleteFailed).toEqual({ Callout: 2 });
expect(snapshot.jsxKeyboardDeleteFailed).toEqual({ Callout: 1 });
resetParseHealth();
expect(getParseHealth().jsxChromeDeleteFailed).toEqual({});
expect(getParseHealth().jsxKeyboardDeleteFailed).toEqual({});
expect(snapshot.jsxChromeDeleteFailed).toEqual({ Callout: 2 });
expect(snapshot.jsxKeyboardDeleteFailed).toEqual({ Callout: 1 });
});
test('ypsMismatch counters are bridged via globalThis (CJS patch ↔ ESM)', () => {
type GlobalWithCounters = typeof globalThis & {
__okYpsCounters?: { block: number; inline: number };
};
const g = globalThis as GlobalWithCounters;
g.__okYpsCounters ||= { block: 0, inline: 0 };
g.__okYpsCounters.block += 5;
g.__okYpsCounters.inline += 2;
const h = getParseHealth();
expect(h.ypsMismatch.block).toBe(5);
expect(h.ypsMismatch.inline).toBe(2);
});
});