UNPKG

tdd-guard

Version:

TDD Guard enforces Test-Driven Development principles using Claude Code hooks

94 lines (93 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildContext = buildContext; const lintSchemas_1 = require("../contracts/schemas/lintSchemas"); const lintProcessor_1 = require("../processors/lintProcessor"); async function buildContext(storage, hookData) { const [modifications, rawTest, todo, lint] = await Promise.all([ storage.getModifications(), storage.getTest(), storage.getTodo(), storage.getLint(), ]); // Filter test results by framework if hookData is available const test = rawTest && hookData ? filterTestByFramework(rawTest, hookData) : rawTest; let processedLintData; try { if (lint) { const rawLintData = lintSchemas_1.LintDataSchema.parse(JSON.parse(lint)); processedLintData = (0, lintProcessor_1.processLintData)(rawLintData); } else { processedLintData = (0, lintProcessor_1.processLintData)(); } } catch { processedLintData = (0, lintProcessor_1.processLintData)(); } return { modifications: formatModifications(modifications ?? ''), test: test ?? '', todo: todo ?? '', lint: processedLintData, }; } function formatModifications(modifications) { if (!modifications) { return ''; } try { const parsed = JSON.parse(modifications); return JSON.stringify(parsed, null, 2); } catch { // If it's not valid JSON, leave it as is return modifications; } } function filterTestByFramework(testData, hookData) { try { const hookDataTyped = hookData; const operationFile = hookDataTyped.tool_input?.file_path ?? ''; const isPythonOperation = operationFile.endsWith('.py'); const isPhpOperation = operationFile.endsWith('.php'); // Parse test data to filter modules const firstParse = JSON.parse(testData); const testResults = typeof firstParse === 'string' ? JSON.parse(firstParse) : firstParse; if (!testResults || typeof testResults !== 'object' || !('testModules' in testResults)) { return testData; // Return original if structure is unexpected } // Filter modules based on operation type const filteredModules = testResults.testModules.filter((module) => { const moduleFile = module.moduleId ?? ''; const isTypeScriptModule = moduleFile.includes('.test.ts') || moduleFile.includes('.test.js'); const isPythonModule = moduleFile.endsWith('.py') && !isTypeScriptModule; const isPhpModule = moduleFile.endsWith('.php') && (moduleFile.includes('Test.php') || moduleFile.includes('test.php') || moduleFile.includes('/test/') || moduleFile.includes('/tests/')); // Return only modules matching the operation type if (isPythonOperation) { return isPythonModule; } if (isPhpOperation) { return isPhpModule; } return isTypeScriptModule; }); // Return filtered results in same format const typedTestResults = testResults; const filteredResults = { ...typedTestResults, testModules: filteredModules, }; return JSON.stringify(JSON.stringify(filteredResults, null, 2)); } catch { // If filtering fails, return original return testData; } }