/**
* Test file to verify the fix for natural language parsing issues
*
* Issues fixed:
* 1. Display format was backwards (showed "variable: statement" instead of "statement: variable")
* 2. Individual words were being mapped separately even when part of complete phrases
*/
import { PropositionalLogic } from '../src/systems/propositional.js';
describe('Natural Language Parsing Fix', () => {
let logic: PropositionalLogic;
beforeEach(() => {
logic = new PropositionalLogic();
});
describe('Variable Mapping Display Format', () => {
it('should display variable mapping in correct format (statement: variable)', () => {
const input = 'If it is raining then the ground is wet';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
expect(result.details.formalizedInput).toContain('• it is raining: It');
expect(result.details.formalizedInput).toContain('• the ground is wet: Ground');
// Should NOT show reversed format
expect(result.details.formalizedInput).not.toContain('• It: it is raining');
expect(result.details.formalizedInput).not.toContain('• Ground: the ground is wet');
});
it('should display correct format for validate operation', () => {
const input = 'If it is raining then the ground is wet';
const result = logic.process('validate', input, 'natural');
expect(result.status).toBe('success');
const explanation = result.details.analysis.explanation;
expect(explanation).toContain('• it is raining: It');
expect(explanation).toContain('• the ground is wet: Ground');
});
it('should display correct format for visualize operation', () => {
const input = 'If it is raining then the ground is wet';
const result = logic.process('visualize', input, 'natural');
expect(result.status).toBe('success');
expect(result.details.visualization).toContain('• it is raining: It');
expect(result.details.visualization).toContain('• the ground is wet: Ground');
});
});
describe('Complete Phrase Mapping', () => {
it('should map complete phrases without mapping individual words', () => {
const input = 'If it is raining then the ground is wet';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
// Should map complete phrases
expect(result.details.formalizedInput).toContain('• it is raining: It');
expect(result.details.formalizedInput).toContain('• the ground is wet: Ground');
// Should NOT map individual words
expect(result.details.formalizedInput).not.toContain('• it: ');
expect(result.details.formalizedInput).not.toContain('• is: ');
expect(result.details.formalizedInput).not.toContain('• raining: ');
expect(result.details.formalizedInput).not.toContain('• the: ');
expect(result.details.formalizedInput).not.toContain('• ground: ');
expect(result.details.formalizedInput).not.toContain('• wet: ');
});
it('should handle parenthesized phrases correctly', () => {
const input = '(the sky is blue) and (the grass is green)';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
// Should map complete phrases
expect(result.details.formalizedInput).toContain('• the sky is blue: Sky');
expect(result.details.formalizedInput).toContain('• the grass is green: Grass');
// Should NOT map individual words
expect(result.details.formalizedInput).not.toContain('• the: ');
expect(result.details.formalizedInput).not.toContain('• sky: ');
expect(result.details.formalizedInput).not.toContain('• is: ');
expect(result.details.formalizedInput).not.toContain('• blue: ');
});
it('should handle mixed parenthesized and standalone words', () => {
const input = '(it is sunny) and warm';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
// Should map the parenthesized phrase
expect(result.details.formalizedInput).toContain('• it is sunny: It');
// Should map the standalone word
expect(result.details.formalizedInput).toContain('• warm: warm');
// Should NOT map words from the phrase separately
expect(result.details.formalizedInput).not.toContain('• it: ');
expect(result.details.formalizedInput).not.toContain('• is: ');
expect(result.details.formalizedInput).not.toContain('• sunny: ');
});
});
describe('Edge Cases', () => {
it('should handle simple variables without phrases', () => {
const input = 'P and Q';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
// Simple variables still get mapped to themselves
expect(result.details.formalizedInput).toContain('(P ∧ Q)');
expect(result.details.formalizedInput).toContain('• P: P');
expect(result.details.formalizedInput).toContain('• Q: Q');
});
it('should handle complex nested statements', () => {
const input = 'If (it is raining and it is cold) then (the ground is wet)';
const result = logic.process('formalize', input, 'natural');
expect(result.status).toBe('success');
// Should handle nested structure
expect(result.details.formalizedInput).toContain('Variable mapping:');
});
});
});