All files test-helpers.js

98.9% Statements 90/91
98.97% Branches 97/98
100% Functions 9/9
98.9% Lines 90/91

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279                7x               46x 46x   46x 2x     44x 44x     44x 44x   44x 202x 202x 1x     201x 201x 28x     173x 173x     173x   17x       173x 4x 169x 2x       173x 173x 3x     173x     44x                       64x 4x     60x 60x   60x 8x     52x 52x     52x 2x   50x 2x   48x 2x     46x                 32x 4x     28x 28x 6x     22x     22x               22x                         21x 3x       18x 3x       15x 15x                 25x 25x                 13x 13x                 8x                 13x 13x                 35x     35x 35x   35x 2x     35x 2x       35x 2x       35x 2x       35x 1x       35x 1x       35x 1x       35x 1x       35x 2x 2x           35x 1x     35x                                  
/**
 * Test helpers for heartbeat-cron skill tests
 */
 
import { readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
 
const __dirname = dirname(fileURLToPath(import.meta.url));
 
/**
 * Parse YAML frontmatter from markdown content
 * @param {string} content - Markdown content with YAML frontmatter
 * @returns {object|null} Parsed frontmatter object or null if invalid
 */
export function parseFrontmatter(content) {
  const frontmatterRegex = /^---\s*\n([\s\S]*?)\n---\s*\n([\s\S]*)$/;
  const match = content.match(frontmatterRegex);
  
  if (!match) {
    return null;
  }
  
  const yamlContent = match[1];
  const bodyContent = match[2];
  
  // Simple YAML parser for heartbeat frontmatter
  const frontmatter = {};
  const lines = yamlContent.split('\n');
  
  for (const line of lines) {
    const trimmed = line.trim();
    if (!trimmed || trimmed.startsWith('#')) {
      continue;
    }
    
    const colonIndex = line.indexOf(':');
    if (colonIndex === -1) {
      continue;
    }
    
    const key = line.slice(0, colonIndex).trim();
    let value = line.slice(colonIndex + 1).trim();
    
    // Remove quotes if present
    if ((value.startsWith('"') && value.endsWith('"')) ||
        (value.startsWith("'") && value.endsWith("'"))) {
      value = value.slice(1, -1);
    }
    
    // Convert boolean strings
    if (value === 'true') {
      value = true;
    } else if (value === 'false') {
      value = false;
    }
    
    // Convert numbers (but not if they're quoted)
    const originalValue = line.slice(colonIndex + 1).trim();
    if (/^\d+$/.test(value) && !originalValue.startsWith('"') && !originalValue.startsWith("'")) {
      value = parseInt(value, 10);
    }
    
    frontmatter[key] = value;
  }
  
  return {
    frontmatter,
    body: bodyContent.trim()
  };
}
 
/**
 * Validate interval format (e.g., 15m, 1h, 6h, 1d)
 * @param {string} interval - Interval string to validate
 * @returns {boolean} True if valid interval format
 */
export function isValidInterval(interval) {
  if (typeof interval !== 'string') {
    return false;
  }
  
  const intervalPattern = /^(\d+)(m|h|d)$/;
  const match = interval.match(intervalPattern);
  
  if (!match) {
    return false;
  }
  
  const value = parseInt(match[1], 10);
  const unit = match[2];
  
  // Validate reasonable ranges
  if (unit === 'm' && (value < 1 || value > 1440)) {
    return false;
  }
  if (unit === 'h' && (value < 1 || value > 168)) {
    return false;
  }
  if (unit === 'd' && (value < 1 || value > 365)) {
    return false;
  }
  
  return true;
}
 
/**
 * Validate cron expression (basic 5-field validation)
 * @param {string} cron - Cron expression to validate
 * @returns {boolean} True if valid cron format
 */
export function isValidCron(cron) {
  if (typeof cron !== 'string') {
    return false;
  }
  
  const parts = cron.trim().split(/\s+/);
  if (parts.length !== 5) {
    return false;
  }
  
  const [minute, hour, dayOfMonth, month, dayOfWeek] = parts;
  
  // Basic validation patterns
  const patterns = {
    minute: /^(\*|(\d{1,2}(-\d{1,2})?)(\/\d+)?|(\*(\/\d+)?))$/,
    hour: /^(\*|(\d{1,2}(-\d{1,2})?)(\/\d+)?|(\*(\/\d+)?))$/,
    dayOfMonth: /^(\*|(\d{1,2}(-\d{1,2})?)(\/\d+)?|(\*(\/\d+)?))$/,
    month: /^(\*|(\d{1,2}(-\d{1,2})?)(\/\d+)?|(\*(\/\d+)?))$/,
    dayOfWeek: /^(\*|(\d{1}(-\d{1})?)(\/\d+)?|(\*(\/\d+)?))$/
  };
  
  return patterns.minute.test(minute) &&
         patterns.hour.test(hour) &&
         patterns.dayOfMonth.test(dayOfMonth) &&
         patterns.month.test(month) &&
         patterns.dayOfWeek.test(dayOfWeek);
}
 
/**
 * Validate timezone string (basic IANA timezone format)
 * @param {string} tz - Timezone string
 * @returns {boolean} True if valid timezone format
 */
export function isValidTimezone(tz) {
  if (typeof tz !== 'string') {
    return false;
  }
  
  // Allow UTC as a special case
  if (tz === 'UTC') {
    return true;
  }
  
  // Basic IANA timezone pattern
  const tzPattern = /^[A-Za-z]+\/[A-Za-z_]+(?:\/[A-Za-z_]+)?$/;
  return tzPattern.test(tz);
}
 
/**
 * Validate agent type
 * @param {string} agent - Agent type
 * @returns {boolean} True if valid agent
 */
export function isValidAgent(agent) {
  const validAgents = ['claude-code', 'codex', 'pi'];
  return validAgents.includes(agent);
}
 
/**
 * Validate sandbox type
 * @param {string} sandbox - Sandbox type
 * @returns {boolean} True if valid sandbox
 */
export function isValidSandbox(sandbox) {
  const validSandboxes = ['read-only', 'workspace-write', 'danger-full-access'];
  return validSandboxes.includes(sandbox);
}
 
/**
 * Validate permissions value
 * @param {string} permissions - Permissions value
 * @returns {boolean} True if valid permissions
 */
export function isValidPermissions(permissions) {
  return permissions === 'skip';
}
 
/**
 * Load fixture file content
 * @param {string} filename - Fixture filename
 * @returns {string} File content
 */
export function loadFixture(filename) {
  const fixturePath = join(__dirname, 'fixtures', filename);
  return readFileSync(fixturePath, 'utf-8');
}
 
/**
 * Validate complete heartbeat configuration
 * @param {object} frontmatter - Parsed frontmatter object
 * @returns {object} Validation result with isValid and errors
 */
export function validateHeartbeatConfig(frontmatter) {
  const errors = [];
  
  // Check for schedule (interval or cron, but not both)
  const hasInterval = frontmatter.hasOwnProperty('interval');
  const hasCron = frontmatter.hasOwnProperty('cron');
  
  if (!hasInterval && !hasCron) {
    errors.push('Missing schedule: must have either "interval" or "cron"');
  }
  
  if (hasInterval && hasCron) {
    errors.push('Invalid schedule: cannot have both "interval" and "cron"');
  }
  
  // Validate interval if present
  if (hasInterval && !isValidInterval(frontmatter.interval)) {
    errors.push(`Invalid interval format: "${frontmatter.interval}"`);
  }
  
  // Validate cron if present
  if (hasCron && !isValidCron(frontmatter.cron)) {
    errors.push(`Invalid cron expression: "${frontmatter.cron}"`);
  }
  
  // Validate timezone if present
  if (frontmatter.hasOwnProperty('tz') && !isValidTimezone(frontmatter.tz)) {
    errors.push(`Invalid timezone: "${frontmatter.tz}"`);
  }
  
  // Validate agent if present
  if (frontmatter.hasOwnProperty('agent') && !isValidAgent(frontmatter.agent)) {
    errors.push(`Invalid agent: "${frontmatter.agent}". Valid agents: claude-code, codex, pi`);
  }
  
  // Validate sandbox if present
  if (frontmatter.hasOwnProperty('sandbox') && !isValidSandbox(frontmatter.sandbox)) {
    errors.push(`Invalid sandbox: "${frontmatter.sandbox}". Valid sandboxes: read-only, workspace-write, danger-full-access`);
  }
  
  // Validate permissions if present
  if (frontmatter.hasOwnProperty('permissions') && !isValidPermissions(frontmatter.permissions)) {
    errors.push(`Invalid permissions: "${frontmatter.permissions}". Only "skip" is supported`);
  }
  
  // Validate maxTurns if present (should be positive integer)
  if (frontmatter.hasOwnProperty('maxTurns')) {
    const maxTurns = frontmatter.maxTurns;
    Iif (typeof maxTurns !== 'number' || maxTurns < 1 || !Number.isInteger(maxTurns)) {
      errors.push(`Invalid maxTurns: "${maxTurns}". Must be a positive integer`);
    }
  }
  
  // Validate timeout format if present
  if (frontmatter.hasOwnProperty('timeout') && !isValidInterval(frontmatter.timeout)) {
    errors.push(`Invalid timeout format: "${frontmatter.timeout}"`);
  }
  
  return {
    isValid: errors.length === 0,
    errors
  };
}
 
export default {
  parseFrontmatter,
  isValidInterval,
  isValidCron,
  isValidTimezone,
  isValidAgent,
  isValidSandbox,
  isValidPermissions,
  loadFixture,
  validateHeartbeatConfig
};