|
| 1 | +#!/usr/bin/env node |
| 2 | +/** |
| 3 | + * Property test for PATH modification idempotency. |
| 4 | + * |
| 5 | + * Property 9: For any shell configuration file content that already contains |
| 6 | + * the ~/.cortex/bin path string, the PATH configuration function SHALL not |
| 7 | + * modify the file (no duplicate entries). Conversely, for content that does |
| 8 | + * not contain the path, the function SHALL prepend (not append) the export line. |
| 9 | + * |
| 10 | + * Validates: Requirements 6.3, 6.4, 6.5 |
| 11 | + */ |
| 12 | + |
| 13 | +const assert = require("node:assert"); |
| 14 | +const os = require("node:os"); |
| 15 | +const path = require("node:path"); |
| 16 | + |
| 17 | +const INSTALL_DIR = path.join(os.homedir(), ".cortex", "bin"); |
| 18 | +const EXPORT_LINE = `export PATH="${INSTALL_DIR}:$PATH"`; |
| 19 | + |
| 20 | +/** |
| 21 | + * Simulate configurePathUnix logic for a single file. |
| 22 | + * This mirrors the actual logic in install.js: |
| 23 | + * if (content.includes(installDir)) { continue; } |
| 24 | + * fs.appendFileSync(file, `\n# Added by cortex installer\n${exportLine}\n`); |
| 25 | + * |
| 26 | + * Returns the new content (or same content if no modification needed). |
| 27 | + */ |
| 28 | +function simulateConfigurePathUnix(existingContent, installDir) { |
| 29 | + if (existingContent.includes(installDir)) { |
| 30 | + return existingContent; // No modification — already configured |
| 31 | + } |
| 32 | + const exportLine = `export PATH="${installDir}:$PATH"`; |
| 33 | + return existingContent + `\n# Added by cortex installer\n${exportLine}\n`; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Generate random shell config content for property testing. |
| 38 | + * @param {boolean} includeInstallDir - Whether to embed the install dir in the content |
| 39 | + */ |
| 40 | +function generateRandomShellContent(includeInstallDir) { |
| 41 | + const lines = []; |
| 42 | + const numLines = Math.floor(Math.random() * 20) + 1; |
| 43 | + |
| 44 | + for (let i = 0; i < numLines; i++) { |
| 45 | + const lineType = Math.floor(Math.random() * 6); |
| 46 | + switch (lineType) { |
| 47 | + case 0: |
| 48 | + lines.push(`# Comment ${Math.random().toString(36).slice(2)}`); |
| 49 | + break; |
| 50 | + case 1: |
| 51 | + lines.push(`export PATH="/usr/local/bin:$PATH"`); |
| 52 | + break; |
| 53 | + case 2: |
| 54 | + lines.push(`alias ll='ls -la'`); |
| 55 | + break; |
| 56 | + case 3: |
| 57 | + lines.push(`export EDITOR=vim`); |
| 58 | + break; |
| 59 | + case 4: |
| 60 | + lines.push(""); |
| 61 | + break; |
| 62 | + case 5: |
| 63 | + lines.push(`source ~/.nvm/nvm.sh`); |
| 64 | + break; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + if (includeInstallDir) { |
| 69 | + // Insert the install dir reference at a random position |
| 70 | + const pos = Math.floor(Math.random() * (lines.length + 1)); |
| 71 | + lines.splice(pos, 0, `export PATH="${INSTALL_DIR}:$PATH"`); |
| 72 | + } |
| 73 | + |
| 74 | + return lines.join("\n"); |
| 75 | +} |
| 76 | + |
| 77 | +// --------------------------------------------------------------------------- |
| 78 | +// Property Tests |
| 79 | +// --------------------------------------------------------------------------- |
| 80 | + |
| 81 | +const NUM_ITERATIONS = 100; |
| 82 | + |
| 83 | +console.log("Property 9: PATH modification idempotency"); |
| 84 | +console.log(`Running ${NUM_ITERATIONS} iterations per sub-property...\n`); |
| 85 | + |
| 86 | +// Sub-property A: Content already containing install dir should NOT be modified |
| 87 | +let passCount = 0; |
| 88 | +for (let i = 0; i < NUM_ITERATIONS; i++) { |
| 89 | + const content = generateRandomShellContent(true); |
| 90 | + const result = simulateConfigurePathUnix(content, INSTALL_DIR); |
| 91 | + assert.strictEqual( |
| 92 | + result, |
| 93 | + content, |
| 94 | + `Iteration ${i}: Content was modified when install dir already present` |
| 95 | + ); |
| 96 | + passCount++; |
| 97 | +} |
| 98 | +console.log(` [PASS] ${passCount}/${NUM_ITERATIONS}: No modification when path already present`); |
| 99 | + |
| 100 | +// Sub-property B: Content NOT containing install dir should have export line added |
| 101 | +passCount = 0; |
| 102 | +for (let i = 0; i < NUM_ITERATIONS; i++) { |
| 103 | + const content = generateRandomShellContent(false); |
| 104 | + const result = simulateConfigurePathUnix(content, INSTALL_DIR); |
| 105 | + |
| 106 | + // The result must contain the install dir |
| 107 | + assert( |
| 108 | + result.includes(INSTALL_DIR), |
| 109 | + `Iteration ${i}: Result does not contain install dir after modification` |
| 110 | + ); |
| 111 | + |
| 112 | + // The export line should appear exactly once |
| 113 | + const escapedDir = INSTALL_DIR.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 114 | + const occurrences = (result.match(new RegExp(escapedDir, "g")) || []).length; |
| 115 | + assert.strictEqual( |
| 116 | + occurrences, |
| 117 | + 1, |
| 118 | + `Iteration ${i}: Install dir appears ${occurrences} times (expected 1)` |
| 119 | + ); |
| 120 | + |
| 121 | + // The PATH entry should prepend (installDir comes before $PATH in the export) |
| 122 | + assert( |
| 123 | + result.includes(`"${INSTALL_DIR}:$PATH"`), |
| 124 | + `Iteration ${i}: PATH not prepended correctly — expected "${INSTALL_DIR}:$PATH"` |
| 125 | + ); |
| 126 | + |
| 127 | + passCount++; |
| 128 | +} |
| 129 | +console.log(` [PASS] ${passCount}/${NUM_ITERATIONS}: Export line correctly added when path not present`); |
| 130 | + |
| 131 | +// Sub-property C: Idempotency — applying the function twice yields the same result |
| 132 | +passCount = 0; |
| 133 | +for (let i = 0; i < NUM_ITERATIONS; i++) { |
| 134 | + const content = generateRandomShellContent(false); |
| 135 | + const firstPass = simulateConfigurePathUnix(content, INSTALL_DIR); |
| 136 | + const secondPass = simulateConfigurePathUnix(firstPass, INSTALL_DIR); |
| 137 | + |
| 138 | + assert.strictEqual( |
| 139 | + firstPass, |
| 140 | + secondPass, |
| 141 | + `Iteration ${i}: Second application modified the content (not idempotent)` |
| 142 | + ); |
| 143 | + |
| 144 | + // Count occurrences — must be exactly 1 after any number of applications |
| 145 | + const escapedDir = INSTALL_DIR.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); |
| 146 | + const occurrences = (secondPass.match(new RegExp(escapedDir, "g")) || []).length; |
| 147 | + assert.strictEqual( |
| 148 | + occurrences, |
| 149 | + 1, |
| 150 | + `Iteration ${i}: Duplicate entries after second pass (found ${occurrences})` |
| 151 | + ); |
| 152 | + |
| 153 | + passCount++; |
| 154 | +} |
| 155 | +console.log(` [PASS] ${passCount}/${NUM_ITERATIONS}: Idempotent — no duplicates on repeated application`); |
| 156 | + |
| 157 | +// Sub-property D: The added content is appended (not inserted before existing content) |
| 158 | +passCount = 0; |
| 159 | +for (let i = 0; i < NUM_ITERATIONS; i++) { |
| 160 | + const content = generateRandomShellContent(false); |
| 161 | + const result = simulateConfigurePathUnix(content, INSTALL_DIR); |
| 162 | + |
| 163 | + // Original content should still be at the start of the result |
| 164 | + assert( |
| 165 | + result.startsWith(content), |
| 166 | + `Iteration ${i}: Original content was not preserved at the start of the file` |
| 167 | + ); |
| 168 | + |
| 169 | + passCount++; |
| 170 | +} |
| 171 | +console.log(` [PASS] ${passCount}/${NUM_ITERATIONS}: Original file content preserved (export appended to end)`); |
| 172 | + |
| 173 | +console.log("\nAll property tests passed."); |
0 commit comments