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 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 5x 4x 4x 4x 4x 1x 1x 3x 3x 3x 3x 3x 4x 4x 3x 3x 3x 3x 3x 3x 3x 5x 19x 19x 19x 19x 19x 19x 19x 8x 8x 8x 3x 1x 8x 8x 2x 2x 8x 8x 2x 2x 8x 8x 1x 8x 8x 2x 2x 2x 1x 1x 1x 1x 1x 2x 2x 2x 8x 19x 19x 19x 19x 19x 19x 5x 1x 1x 1x 4x 5x 5x 5x 5x 5x 5x 5x 5x 1x 1x 3x 5x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 5x 5x 5x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 2x 2x 2x 5x 19x 19x 19x 19x 19x 19x 5x 5x 5x 5x 2x 2x 2x 5x 3x 3x 3x 3x 1x 1x 1x 1x 1x 3x 5x 19x 19x 19x 19x 19x 19x 19x 10x 10x 2x 2x 8x 8x 8x 8x 6x 6x 8x 1x 1x 1x 1x 1x 1x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 3x 1x 1x 2x 2x 8x 8x 8x 7x 1x 1x 6x 6x 6x 8x 8x 6x 6x 10x 19x 19x 19x 19x 19x 19x 19x 19x 8x 8x 8x 8x 8x 8x 8x 8x 1x 1x 1x 8x 1x 1x 1x 7x 1x 1x 8x 8x 8x 8x 8x 8x 8x 19x 19x 19x 19x 19x 19x 19x 5x 5x 5x 5x 5x 5x 19x 19x 19x 19x 19x 19x 19x 19x 9x 9x 9x 1x 1x 9x 1x 1x 9x 1x 1x 9x 3x 3x 7x 1x 1x 2x 2x 9x 19x 19x 19x 19x 19x 19x 3x 3x 3x 2x 2x 1x 1x 3x 19x 19x 19x 19x 19x 1x 1x 1x 1x 19x 19x 19x | /**
* Componente backend para manejar el modal de generación de planes de prueba
* Coordina entre el webview, servicios de Azure DevOps y generador de test plans
*/
const vscode = require("vscode");
const { spawn } = require("child_process");
const TestPlanGenerator = require("../../generators/testPlanGenerator");
const WorkItemService = require("../../api/workItemService");
class TestPlanModal {
/**
* @param {vscode.Webview} webview - Webview donde se muestra el modal
* @param {Object} config - Configuración de Azure DevOps
* @param {Object} azureDevOpsService - Servicio de Azure DevOps
* @param {Object} summaryService - Servicio de IA
* @param {Object} logger - Logger de la extensión
*/
constructor(webview, config, azureDevOpsService, summaryService, logger) {
this.webview = webview;
this.config = config;
this.azureDevOpsService = azureDevOpsService;
this.summaryService = summaryService;
this.logger = logger;
this.workItemService = null;
this.testPlanGenerator = null;
this.isProcessing = false;
this.logger.info("TestPlanModal inicializado");
}
/**
* Inicializar servicios
* @private
*/
_initServices() {
if (!this.workItemService) {
const organization = this.config.azure?.organization;
const pat = this.config.azure?.pat;
if (!organization || !pat) {
throw new Error("Configuración de Azure Boards incompleta");
}
this.workItemService = new WorkItemService(organization, pat, (msg) =>
this.logger.info(msg),
);
}
if (!this.testPlanGenerator) {
this.testPlanGenerator = new TestPlanGenerator(
this.workItemService,
this.azureDevOpsService,
this.summaryService,
(msg) => this.logger.info(msg)
);
}
}
/**
* Manejar mensajes desde el webview
* @param {Object} message - Mensaje desde el webview
* @returns {Promise<boolean>} True si el mensaje fue manejado
*/
async handleMessage(message) {
try {
switch (message.command) {
case "generateTestPlan":
await this.generateTestPlan(message.data);
return true;
case "openTestPlanDocument":
await this.openDocument(message.path);
return true;
case "revealTestPlanInExplorer":
await this.revealInExplorer(message.path);
return true;
default:
return false;
}
} catch (error) {
this.logger.error(`Error en TestPlanModal.handleMessage: ${error.message}`);
if (!error?.__testPlanErrorHandled) {
this.webview.postMessage({
type: "testPlanError",
error: this._buildUiError(error),
});
}
return true;
}
}
/**
* Generar plan de pruebas
* @param {Object} data - Datos de entrada
*/
async generateTestPlan(data) {
if (this.isProcessing) {
this.logger.warn("Ya hay una generación en proceso");
return;
}
const workItemUrl = String(data?.workItemUrl || "").trim();
const pullRequestUrls = Array.isArray(data?.pullRequestUrls)
? [...new Set(data.pullRequestUrls.map((url) => String(url || "").trim()).filter(Boolean))]
: [];
const additionalContext = String(data?.additionalContext || "").trim();
const aiModel = String(data?.aiModel || "").trim() || null;
if (!workItemUrl) {
throw new Error("Debes ingresar un URL de Work Item válido.");
}
if (pullRequestUrls.length > 0 && !this.azureDevOpsService) {
throw new Error(
"Hay URLs de PR pero la configuración de Azure DevOps (Git/PRs) no está completa. Configura organización y PAT en G-Bot.",
);
}
this.isProcessing = true;
try {
this.logger.info("🧪 Iniciando generación de plan de pruebas");
this.logger.info(` Work Item URL: ${workItemUrl}`);
if (pullRequestUrls.length > 0) {
this.logger.info(` PRs: ${pullRequestUrls.length} URL(s)`);
}
// Inicializar servicios
this._initServices();
// Vincular progreso del generador al webview.
this.testPlanGenerator.logCallback = (message) => {
this.logger.info(message);
this._postProgressMessage(this._mapGeneratorLogToProgress(message));
};
// Notificar inicio
this._postProgressMessage("Iniciando generación...");
// Generar plan de pruebas
const result = await this.testPlanGenerator.generateTestPlan({
workItemUrl,
pullRequestUrls,
additionalContext,
useAI: data?.useAI !== false, // Default true
aiModel,
});
this.logger.info("✅ Plan de pruebas generado exitosamente");
this.logger.info(` Documento: ${result.path}`);
this.logger.info(` Escenarios: ${result.scenarios.length}`);
// Notificar éxito
this.webview.postMessage({
type: "testPlanSuccess",
result: {
path: result.path,
scenarios: result.scenarios,
scenariosCount: result.scenarios.length,
},
});
// Abrir documento automáticamente
await this.openDocument(result.path);
} catch (error) {
this.logger.error(`❌ Error generando plan de pruebas: ${error.message}`);
const uiError = this._buildUiError(error);
this.webview.postMessage({
type: "testPlanError",
error: uiError,
});
if (error && typeof error === "object") {
error.__testPlanErrorHandled = true;
}
throw error;
} finally {
this.isProcessing = false;
}
}
/**
* Abrir documento generado
* @param {string} filePath - Ruta del documento
*/
async openDocument(filePath) {
try {
const uri = vscode.Uri.file(filePath);
const opened = await vscode.env.openExternal(uri);
if (!opened) {
throw new Error("No fue posible abrir el documento con la aplicación predeterminada");
}
this.logger.info(`📄 Documento abierto: ${filePath}`);
} catch (error) {
this.logger.error(`Error abriendo documento: ${error.message}`);
try {
await this._openWithSystemDefault(filePath);
} catch (systemError) {
this.logger.error(`Error con método alternativo: ${systemError.message}`);
vscode.window.showErrorMessage(
`No se pudo abrir el documento automáticamente. Ábrelo manualmente en: ${filePath}`,
);
}
}
}
/**
* Abrir con aplicación por defecto del sistema
* @param {string} filePath - Ruta del archivo
* @private
*/
async _openWithSystemDefault(filePath) {
const safePath = String(filePath || "").trim();
if (!safePath) {
throw new Error("Ruta de archivo inválida");
}
let command;
let args;
if (process.platform === "win32") {
command = "cmd";
args = ["/c", "start", "", safePath];
} else if (process.platform === "darwin") {
command = "open";
args = [safePath];
} else {
command = "xdg-open";
args = [safePath];
}
await new Promise((resolve, reject) => {
const child = spawn(command, args, {
detached: true,
stdio: "ignore",
windowsHide: true,
});
let settled = false;
child.once("error", (error) => {
if (settled) {
return;
}
settled = true;
reject(error);
});
child.once("spawn", () => {
if (settled) {
return;
}
settled = true;
child.unref();
resolve();
});
});
this.logger.info(`📄 Documento abierto con aplicación por defecto`);
}
/**
* Normaliza excepciones para mostrar mensajes claros en UI.
* @param {Error} error
* @returns {{type: string, message: string, details?: string}}
* @private
*/
_buildUiError(error) {
const fallbackMessage = "Ocurrió un error inesperado al generar el plan de pruebas.";
const rawMessage = String(error?.message || fallbackMessage).trim() || fallbackMessage;
const normalizedMessage = rawMessage.toLowerCase();
let type = "general";
let message = rawMessage;
if (/configuraci[oó]n|pat|azure devops/.test(normalizedMessage)) {
type = "config";
message =
"La configuración de Azure DevOps está incompleta o es inválida. Verifica organización y PAT en G-Bot.";
} else if (/timeout|network|econn|socket|enotfound|etimedout/.test(normalizedMessage)) {
type = "network";
message =
"No fue posible completar la operación por un problema de conexión con Azure DevOps. Intenta nuevamente.";
} else if (/url|inv[aá]lid/.test(normalizedMessage)) {
type = "validation";
}
return {
type,
message,
details: this.config?.settings?.debug ? error?.stack : undefined,
};
}
/**
* Publica estado de progreso al webview.
* @param {string} message
* @private
*/
_postProgressMessage(message) {
this.webview.postMessage({
type: "testPlanProgress",
status: "running",
message,
});
}
/**
* Mapea logs técnicos del generador a mensajes breves para UI.
* @param {string} rawLog
* @returns {string}
* @private
*/
_mapGeneratorLogToProgress(rawLog) {
const message = String(rawLog || "").trim();
if (message.includes("PASO 1/5")) {
return "Obteniendo datos del Work Item...";
}
if (message.includes("PASO 2/5")) {
return "Procesando Pull Requests...";
}
if (message.includes("PASO 3/5")) {
return "Analizando archivos y comentarios de PR...";
}
if (message.includes("PASO 4/5")) {
return "Generando escenarios con IA...";
}
if (message.includes("PASO 5/5")) {
return "Construyendo documento de pruebas...";
}
return "Procesando información del plan de pruebas...";
}
/**
* Revelar archivo en explorador
* @param {string} filePath - Ruta del archivo
*/
async revealInExplorer(filePath) {
try {
const uri = vscode.Uri.file(filePath);
await vscode.commands.executeCommand("revealFileInOS", uri);
this.logger.info(`📂 Archivo revelado en explorador: ${filePath}`);
} catch (error) {
this.logger.error(`Error revelando archivo: ${error.message}`);
}
}
/**
* Limpiar recursos
*/
dispose() {
this.workItemService = null;
this.testPlanGenerator = null;
this.logger.info("TestPlanModal disposed");
}
}
module.exports = TestPlanModal;
|