Developer Utility Scripts
Relevant source files
The following files were used as context for generating this wiki page:
- scripts/setup-supabase-complete.js
- scripts/cleanup-users.js
- scripts/validate-demo-data.js
- scripts/test-all-modules.js
- scripts/test-requests-crud.js
- scripts/test-storage-crud.js
- scripts/check-requests-schema.js
- scripts/test-chat-module.js
- scripts/create-storage-bucket.js
- scripts/setup-storage-policies.js
The scripts/ directory contains a suite of Node.js utility scripts designed to automate development, testing, and maintenance tasks for the ElmecV2 application. These scripts interact directly with the Supabase backend to manage database schemas, configure storage, run automated tests, and ensure data integrity. They are essential tools for maintaining a consistent and reliable development environment, validating backend functionality, and preparing the application for deployment.
The scripts are typically executed via npm commands defined in package.json, such as npm run test-supabase or npm run setup-supabase. They use the @supabase/supabase-js library to communicate with the project's Supabase instance, relying on environment variables for credentials.
Sources: STATUS-DEPLOY.md, README.md, REPORTE-EJECUCION-PRUEBAS.md
Scripts Overview
The following table summarizes the key utility scripts available in the project.
| Script File | Purpose | Category |
|---|---|---|
setup-supabase-complete.js | Runs a full setup sequence for the Supabase environment. | Setup |
create-storage-bucket.js | Creates the request-files bucket in Supabase Storage. | Setup |
setup-storage-policies.js | Applies Row Level Security (RLS) policies to the storage bucket. | Setup |
test-all-modules.js | Executes a comprehensive suite of backend tests for all major application modules. | Testing |
test-requests-crud.js | Performs CRUD (Create, Read, Update, Delete) tests specifically for the requests table. | Testing |
test-chat-module.js | Validates the functionality of the chat system, including rooms and messages. | Testing |
test-storage-crud.js | Performs CRUD tests for file operations in Supabase Storage. | Testing |
check-requests-schema.js | Verifies that the requests table in the database has all the required columns. | Validation |
validate-demo-data.js | Ensures that essential demo data (like specific users) exists in the database. | Validation |
cleanup-users.js | Removes test users from the Supabase Auth and users table. | Maintenance |
Sources: REPORTE_FINAL_VALIDACION.md, REPORTE-EJECUCION-PRUEBAS.md, REPORTE_CHAT_MODULE.md
Setup & Configuration Scripts
These scripts are used to initialize and configure the Supabase backend environment.
Supabase Full Setup (setup-supabase-complete.js)
This script orchestrates the complete setup of the Supabase project, making it ready for development and testing. It is likely invoked by the npm run setup-supabase command.
The diagram below illustrates the sequence of actions performed by the setup script.
This diagram shows the high-level workflow of the complete Supabase setup script.
Sources: STATUS-DEPLOY.md, RESUMEN_CONFIGURACION.md
Storage Configuration (create-storage-bucket.js, setup-storage-policies.js)
These scripts prepare the Supabase Storage for handling file uploads related to user requests.
create-storage-bucket.js: Creates a public bucket namedrequest-fileswith a 5MB file size limit.setup-storage-policies.js: Applies four essential RLS policies to thestorage.objectstable for therequest-filesbucket, ensuring proper access control.
The policies configured are:
- Public read access (SELECT): Allows anyone to read/download files.
- Authenticated users can upload (INSERT): Allows logged-in users to upload files.
- Users can update own files (UPDATE): Allows users to update files they own.
- Users can delete own files (DELETE): Allows users to delete files they own.
Sources: RESUMEN_FINAL.md, RESUMEN_CONFIGURACION.md
Testing & Validation Scripts
This category includes scripts for running automated tests and validating the state of the database.
Master Test Script (test-all-modules.js)
This is the main testing script that runs a battery of tests against the backend APIs for various application modules. It validates core functionalities like authentication, CRUD operations, and business logic.
The script covers the following modules:
- Authentication
- Requests (Solicitudes)
- Chat
- Notifications
- Directory
During execution, it was found that some tests initially failed due to mismatches between the test data and database constraints (e.g., using an invalid status like 'cerrado' for a request). These issues were corrected within the script to align with valid enum values like 'resuelto'.
The diagram below shows the structure of the test execution.
This diagram illustrates how the main test script invokes test suites for different application modules.
Sources: REPORTE-EJECUCION-PRUEBAS.md
Module-Specific CRUD Tests
The repository contains several scripts dedicated to testing the CRUD functionality of specific modules.
Requests CRUD (test-requests-crud.js)
This script performs 16 tests covering the full lifecycle of a request record. It verifies creation, reading (with filters), updating, and deletion. It also checks that relationships with the users table (for usuario_id and agente_id) are correctly resolved.
The following sequence diagram shows the interactions during the test.
This diagram shows the sequence of API calls made by the test-requests-crud.js script to validate the requests module.
Sources: REPORTE_FINAL_VALIDACION.md, REPORTE_PRUEBAS_REQUESTS_CRUD.md
Other Test Scripts
test-chat-module.js: Contains 30 tests for the chat system. It validates the creation of chat rooms, sending messages, and other core logic. Some tests were noted to fail due to a discrepancy between the application code's expectations and the actual database schema.test-storage-crud.js: A suite of 13 tests validating file operations (upload, read, delete) in therequest-filesbucket.
Sources: REPORTE_CHAT_MODULE.md, RESUMEN_FINAL.md
Schema and Data Validation
These scripts ensure the database is in the correct state for the application to run.
check-requests-schema.js: This utility checks therequeststable to confirm the presence of all 16 required columns, includingarchivos,feedback, andrating. This is crucial for preventing runtime errors if a migration has not been applied.validate-demo-data.js: This script verifies that essential data, such as the predefined admin, agent, and client users, exists in the database. This is important for testing and demonstration purposes.
The output of check-requests-schema.js confirms the structure:
Columnas disponibles en la tabla requests:
==================================================
agente_id (object)
archivos (object) # ✅ NUEVA
created_at (string)
estatus (string)
...
rating (number) # ✅ NUEVA
...
usuario_id (string)
Total de columnas: 16
Sources: REPORTE_FINAL_VALIDACION.md, REPORTE_PRUEBAS_REQUESTS_CRUD.md
Maintenance Scripts
User Cleanup (cleanup-users.js)
This script is designed for environment maintenance. It identifies and removes test users from both the Supabase auth.users table and the public users table, helping to keep the development database clean. The logic for identifying which users to delete is contained within this script.
Conclusion
The developer utility scripts are a cornerstone of the ElmecV2 project's development and quality assurance process. They provide a powerful, automated framework for environment setup, backend validation, and data management. By using these scripts, developers can ensure a consistent and functional backend, catch schema-related bugs early, and maintain a high level of confidence in the application's core services before and after deployment.