You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
4.3 KiB
4.3 KiB
Plan de Refactorización - Mejora de Mantenibilidad
Fecha: 2024 Estado: Propuesto Objetivo: Mejorar la mantenibilidad del código eliminando duplicación y reduciendo la complejidad de archivos grandes.
Resumen Ejecutivo
El proyecto ha crecido orgánicamente y presenta varios problemas de mantenibilidad:
- 19 duplicaciones de la función
toIsoSql - Múltiples duplicaciones de
sha256Hexen tests - Archivos excesivamente grandes:
group-sync.ts(1307 líneas),server.ts(665 líneas) - Código compartido duplicado entre
src/(bot) yapps/web/ - Setup repetitivo en tests (60+ archivos)
Este plan aborda estos problemas en 4 fases priorizadas.
🔴 Fase 1: Eliminación de Duplicación de Utilidades (1-2 horas)
Prioridad: CRÍTICA
Impacto: Alto (reduce ~150 líneas duplicadas)
Riesgo: Bajo (cambio mecánico)
Objetivos
- Centralizar función
toIsoSqlen un único lugar - Centralizar función
sha256Hexpara tests - Eliminar todas las duplicaciones mediante imports
Archivos a Crear
src/utils/date.ts
- Exportar función
toIsoSql(d?: Date): string - Documentar formato de salida (ISO SQL:
YYYY-MM-DD HH:MM:SS.mmm)
tests/helpers/crypto.ts
- Exportar función
sha256Hex(input: string): Promise<string> - Reutilizar en todos los tests web y unit
Archivos a Modificar
Archivos con toIsoSql duplicada (19 archivos):
apps/web/src/hooks.server.tsapps/web/src/routes/api/tasks/[id]/claim/+server.tsapps/web/src/routes/api/tasks/[id]/uncomplete/+server.tssrc/services/group-sync.tstests/unit/services/cleanup-inactive.test.tstests/unit/tasks/complete-reaction.test.tstests/unit/services/metrics-health.test.tstests/unit/services/response-queue.cleanup.test.tstests/web/api.integrations.feeds.test.tstests/web/api.me.preferences.test.tstests/web/api.me.tasks.test.tstests/web/api.tasks.complete.reaction.test.tstests/web/app.integrations.page.test.tstests/web/app.preferences.page.test.ts- Y otros según
grep -rn "function toIsoSql"
Archivos con sha256Hex duplicada:
tests/web/api.integrations.feeds.test.tstests/web/api.me.preferences.test.tstests/web/api.me.tasks.test.tstests/web/app.integrations.page.test.tstests/web/app.preferences.page.test.ts- Verificar que
src/utils/crypto.tsyapps/web/src/lib/server/crypto.tsno estén duplicados
Pasos de Ejecución
- Crear
src/utils/date.tscon implementación de referencia - Crear
tests/helpers/crypto.tscon implementación de referencia - Buscar y reemplazar imports en todos los archivos afectados
- Eliminar definiciones locales de las funciones
- Ejecutar suite completa de tests:
bun test - Verificar que no hay regresiones
Criterios de Éxito
- ✅ Todos los tests pasan
- ✅ Solo existe 1 definición de
toIsoSqlen el código fuente - ✅ Solo existe 1 definición de
sha256Hexen tests - ✅ Reducción de ~150 líneas de código
🔴 Fase 2: División de group-sync.ts (3-4 horas)
Prioridad: CRÍTICA
Impacto: Alto (mejora legibilidad y testabilidad)
Riesgo: Medio (requiere análisis cuidadoso)
Objetivos
- Dividir archivo monolítico de 1307 líneas en módulos cohesivos
- Separar responsabilidades claramente
- Facilitar testing unitario de cada componente
- Mantener API pública compatible
Análisis Previo Necesario
Archivos a consultar para entender el alcance:
src/services/group-sync.ts(archivo principal a dividir)tests/unit/services/group-sync.test.tstests/unit/services/group-sync.onboarding.test.tstests/unit/services/group-sync.coverage.test.tstests/unit/services/group-sync.gating.test.tstests/unit/services/group-sync.label-update.test.tstests/unit/services/group-sync.sync-members.test.tstests/unit/services/group-sync.members.test.tstests/unit/services/group-sync.fetch-members.test.tstests/unit/services/group-sync.scheduler.test.tstests/unit/services/group-sync.scheduler.gating.test.ts
Identificar en el código:
- Funciones públicas vs privadas
- Responsabilidades principales (sincronización, scheduling, gating, eventos)
- Dependencias entre funciones
- Estado compartido (si existe)