| 1 | import test from "ava";
|
| 2 | import request from "supertest";
|
| 3 | import { createApp } from "../../src/create-app.js";
|
| 4 | import { setupTestDb, cleanupTestDb } from "../test-helper.js";
|
| 5 |
|
| 6 | let app;
|
| 7 | let db;
|
| 8 | let userId;
|
| 9 |
|
| 10 | test.before(() => {
|
| 11 | db = setupTestDb();
|
| 12 | app = createApp(db);
|
| 13 | });
|
| 14 |
|
| 15 | test.beforeEach(async () => {
|
| 16 | cleanupTestDb();
|
| 17 |
|
| 18 | // Create a test user with unique email
|
| 19 | const uniqueEmail = `mood_${Date.now()}_${Math.random()}@example.com`;
|
| 20 | const res = await request(app)
|
| 21 | .post("/users")
|
| 22 | .send({
|
| 23 | email: uniqueEmail,
|
| 24 | password: "senha123",
|
| 25 | first_name: "Mood",
|
| 26 | last_name: "Tester"
|
| 27 | });
|
| 28 |
|
| 29 | if (res.status !== 201) {
|
| 30 | throw new Error(`Failed to create test user: ${JSON.stringify(res.body)}`);
|
| 31 | }
|
| 32 |
|
| 33 | userId = res.body.id;
|
| 34 | });
|
| 35 |
|
| 36 | test("Criar registro de humor com todos os campos obrigatórios", async (t) => {
|
| 37 | const now = new Date().toISOString();
|
| 38 |
|
| 39 | const res = await request(app)
|
| 40 | .post("/mood")
|
| 41 | .send({
|
| 42 | user_id: userId,
|
| 43 | stress_level: 3,
|
| 44 | anxiety_level: 4,
|
| 45 | energy_level: 7,
|
| 46 | recorded_at: now
|
| 47 | });
|
| 48 |
|
| 49 | t.is(res.status, 201);
|
| 50 | t.truthy(res.body.id);
|
| 51 | });
|
| 52 |
|
| 53 | test("Criar registro de humor com avaliação e descrição", async (t) => {
|
| 54 | const now = new Date().toISOString();
|
| 55 |
|
| 56 | const res = await request(app)
|
| 57 | .post("/mood")
|
| 58 | .send({
|
| 59 | user_id: userId,
|
| 60 | rating: 8,
|
| 61 | stress_level: 2,
|
| 62 | anxiety_level: 3,
|
| 63 | energy_level: 9,
|
| 64 | title: "Dia produtivo",
|
| 65 | description: "Consegui terminar todas as tarefas",
|
| 66 | recorded_at: now
|
| 67 | });
|
| 68 |
|
| 69 | t.is(res.status, 201);
|
| 70 |
|
| 71 | // Verify data was saved correctly
|
| 72 | const mood = db.prepare("SELECT * FROM mood WHERE id = ?").get(res.body.id);
|
| 73 | t.is(mood.rating, 8);
|
| 74 | t.is(mood.stress_level, 2);
|
| 75 | t.is(mood.title, "Dia produtivo");
|
| 76 | });
|
| 77 |
|
| 78 | test("Criar registro de humor com componentes emocionais", async (t) => {
|
| 79 | const now = new Date().toISOString();
|
| 80 |
|
| 81 | const res = await request(app)
|
| 82 | .post("/mood")
|
| 83 | .send({
|
| 84 | user_id: userId,
|
| 85 | stress_level: 5,
|
| 86 | anxiety_level: 6,
|
| 87 | energy_level: 4,
|
| 88 | recorded_at: now,
|
| 89 | mood_components: [
|
| 90 | { emotion: "joy", intensity: 7 },
|
| 91 | { emotion: "anxiety", intensity: 6 },
|
| 92 | { emotion: "trust", intensity: 8 }
|
| 93 | ]
|
| 94 | });
|
| 95 |
|
| 96 | t.is(res.status, 201);
|
| 97 |
|
| 98 | // Verify components were saved
|
| 99 | const components = db.prepare("SELECT * FROM mood_components WHERE mood_id = ?").all(res.body.id);
|
| 100 | t.is(components.length, 3);
|
| 101 |
|
| 102 | const emotions = components.map(c => c.emotion);
|
| 103 | t.true(emotions.includes('joy'));
|
| 104 | t.true(emotions.includes('anxiety'));
|
| 105 | t.true(emotions.includes('trust'));
|
| 106 | });
|
| 107 |
|
| 108 | test("Componentes emocionais devem aceitar maiúsculas e converter para minúsculas", async (t) => {
|
| 109 | const now = new Date().toISOString();
|
| 110 |
|
| 111 | const res = await request(app)
|
| 112 | .post("/mood")
|
| 113 | .send({
|
| 114 | user_id: userId,
|
| 115 | stress_level: 5,
|
| 116 | anxiety_level: 6,
|
| 117 | energy_level: 4,
|
| 118 | recorded_at: now,
|
| 119 | mood_components: [
|
| 120 | { emotion: "JOY", intensity: 7 },
|
| 121 | { emotion: "AnXiEtY", intensity: 6 }
|
| 122 | ]
|
| 123 | });
|
| 124 |
|
| 125 | t.is(res.status, 201);
|
| 126 |
|
| 127 | // Verify emotions were lowercased
|
| 128 | const components = db.prepare("SELECT emotion FROM mood_components WHERE mood_id = ? ORDER BY emotion ASC").all(res.body.id);
|
| 129 | t.is(components[0].emotion, 'anxiety');
|
| 130 | t.is(components[1].emotion, 'joy');
|
| 131 | });
|
| 132 |
|
| 133 | test("Falha ao criar registro de humor sem ID de usuário", async (t) => {
|
| 134 | const now = new Date().toISOString();
|
| 135 |
|
| 136 | const res = await request(app)
|
| 137 | .post("/mood")
|
| 138 | .send({
|
| 139 | stress_level: 3,
|
| 140 | anxiety_level: 4,
|
| 141 | energy_level: 7,
|
| 142 | recorded_at: now
|
| 143 | });
|
| 144 |
|
| 145 | t.is(res.status, 400);
|
| 146 | t.is(res.body.error, "Campos faltantes");
|
| 147 | t.true(res.body.missing.includes('user_id'));
|
| 148 | });
|
| 149 |
|
| 150 | test("Falha ao criar registro de humor sem nível de stress", async (t) => {
|
| 151 | const now = new Date().toISOString();
|
| 152 |
|
| 153 | const res = await request(app)
|
| 154 | .post("/mood")
|
| 155 | .send({
|
| 156 | user_id: userId,
|
| 157 | anxiety_level: 4,
|
| 158 | energy_level: 7,
|
| 159 | recorded_at: now
|
| 160 | });
|
| 161 |
|
| 162 | t.is(res.status, 400);
|
| 163 | t.is(res.body.error, "Campos faltantes");
|
| 164 | t.true(res.body.missing.includes('stress_level'));
|
| 165 | });
|
| 166 |
|
| 167 | test("Falha ao criar registro de humor sem nível de ansiedade", async (t) => {
|
| 168 | const now = new Date().toISOString();
|
| 169 |
|
| 170 | const res = await request(app)
|
| 171 | .post("/mood")
|
| 172 | .send({
|
| 173 | user_id: userId,
|
| 174 | stress_level: 3,
|
| 175 | energy_level: 7,
|
| 176 | recorded_at: now
|
| 177 | });
|
| 178 |
|
| 179 | t.is(res.status, 400);
|
| 180 | t.is(res.body.error, "Campos faltantes");
|
| 181 | t.true(res.body.missing.includes('anxiety_level'));
|
| 182 | });
|
| 183 |
|
| 184 | test("Falha ao criar registro de humor sem nível de energia", async (t) => {
|
| 185 | const now = new Date().toISOString();
|
| 186 |
|
| 187 | const res = await request(app)
|
| 188 | .post("/mood")
|
| 189 | .send({
|
| 190 | user_id: userId,
|
| 191 | stress_level: 3,
|
| 192 | anxiety_level: 4,
|
| 193 | recorded_at: now
|
| 194 | });
|
| 195 |
|
| 196 | t.is(res.status, 400);
|
| 197 | t.is(res.body.error, "Campos faltantes");
|
| 198 | t.true(res.body.missing.includes('energy_level'));
|
| 199 | });
|
| 200 |
|
| 201 | test("Falha ao criar registro de humor sem data de registro", async (t) => {
|
| 202 | const res = await request(app)
|
| 203 | .post("/mood")
|
| 204 | .send({
|
| 205 | user_id: userId,
|
| 206 | stress_level: 3,
|
| 207 | anxiety_level: 4,
|
| 208 | energy_level: 7
|
| 209 | });
|
| 210 |
|
| 211 | t.is(res.status, 400);
|
| 212 | t.is(res.body.error, "Campos faltantes");
|
| 213 | t.true(res.body.missing.includes('recorded_at'));
|
| 214 | });
|
| 215 |
|
| 216 | test("Falha ao criar registro de humor com nível de estresse inválido (menor que 1)", async (t) => {
|
| 217 | const now = new Date().toISOString();
|
| 218 |
|
| 219 | const res = await request(app)
|
| 220 | .post("/mood")
|
| 221 | .send({
|
| 222 | user_id: userId,
|
| 223 | stress_level: 0,
|
| 224 | anxiety_level: 4,
|
| 225 | energy_level: 7,
|
| 226 | recorded_at: now
|
| 227 | });
|
| 228 |
|
| 229 | t.is(res.status, 400);
|
| 230 | t.is(res.body.error, "stress_level deve ser entre 1 e 10");
|
| 231 | });
|
| 232 |
|
| 233 | test("Falha ao criar registro de humor com nível de estresse inválido (maior que 10)", async (t) => {
|
| 234 | const now = new Date().toISOString();
|
| 235 |
|
| 236 | const res = await request(app)
|
| 237 | .post("/mood")
|
| 238 | .send({
|
| 239 | user_id: userId,
|
| 240 | stress_level: 11,
|
| 241 | anxiety_level: 4,
|
| 242 | energy_level: 7,
|
| 243 | recorded_at: now
|
| 244 | });
|
| 245 |
|
| 246 | t.is(res.status, 400);
|
| 247 | t.is(res.body.error, "stress_level deve ser entre 1 e 10");
|
| 248 | });
|
| 249 |
|
| 250 | test("Falha ao criar registro de humor com nível de ansiedade inválido", async (t) => {
|
| 251 | const now = new Date().toISOString();
|
| 252 |
|
| 253 | const res = await request(app)
|
| 254 | .post("/mood")
|
| 255 | .send({
|
| 256 | user_id: userId,
|
| 257 | stress_level: 3,
|
| 258 | anxiety_level: 15,
|
| 259 | energy_level: 7,
|
| 260 | recorded_at: now
|
| 261 | });
|
| 262 |
|
| 263 | t.is(res.status, 400);
|
| 264 | t.is(res.body.error, "anxiety_level deve ser entre 1 e 10");
|
| 265 | });
|
| 266 |
|
| 267 | test("Falha ao criar registro de humor com nível de energia inválido", async (t) => {
|
| 268 | const now = new Date().toISOString();
|
| 269 |
|
| 270 | const res = await request(app)
|
| 271 | .post("/mood")
|
| 272 | .send({
|
| 273 | user_id: userId,
|
| 274 | stress_level: 3,
|
| 275 | anxiety_level: 4,
|
| 276 | energy_level: -5,
|
| 277 | recorded_at: now
|
| 278 | });
|
| 279 |
|
| 280 | t.is(res.status, 400);
|
| 281 | t.is(res.body.error, "energy_level deve ser entre 1 e 10");
|
| 282 | });
|
| 283 |
|
| 284 | test("Falha ao criar registro de humor com avaliação inválido", async (t) => {
|
| 285 | const now = new Date().toISOString();
|
| 286 |
|
| 287 | const res = await request(app)
|
| 288 | .post("/mood")
|
| 289 | .send({
|
| 290 | user_id: userId,
|
| 291 | rating: 20,
|
| 292 | stress_level: 3,
|
| 293 | anxiety_level: 4,
|
| 294 | energy_level: 7,
|
| 295 | recorded_at: now
|
| 296 | });
|
| 297 |
|
| 298 | t.is(res.status, 400);
|
| 299 | t.is(res.body.error, "rating deve ser entre 1 e 10");
|
| 300 | });
|
| 301 |
|
| 302 | test("Falha ao criar registro de humor com data inválida", async (t) => {
|
| 303 | const res = await request(app)
|
| 304 | .post("/mood")
|
| 305 | .send({
|
| 306 | user_id: userId,
|
| 307 | stress_level: 3,
|
| 308 | anxiety_level: 4,
|
| 309 | energy_level: 7,
|
| 310 | recorded_at: "data-invalida"
|
| 311 | });
|
| 312 |
|
| 313 | t.is(res.status, 400);
|
| 314 | t.is(res.body.error, "recorded_at deve ser uma data válida");
|
| 315 | });
|
| 316 |
|
| 317 | test("Falha ao criar registro de humor com data no futuro", async (t) => {
|
| 318 | const futureDate = new Date();
|
| 319 | futureDate.setDate(futureDate.getDate() + 1);
|
| 320 |
|
| 321 | const res = await request(app)
|
| 322 | .post("/mood")
|
| 323 | .send({
|
| 324 | user_id: userId,
|
| 325 | stress_level: 3,
|
| 326 | anxiety_level: 4,
|
| 327 | energy_level: 7,
|
| 328 | recorded_at: futureDate.toISOString()
|
| 329 | });
|
| 330 |
|
| 331 | t.is(res.status, 400);
|
| 332 | t.is(res.body.error, "recorded_at não pode ser no futuro");
|
| 333 | });
|
| 334 |
|
| 335 | test("Falha ao criar registro de humor com emoção inválida", async (t) => {
|
| 336 | const now = new Date().toISOString();
|
| 337 |
|
| 338 | const res = await request(app)
|
| 339 | .post("/mood")
|
| 340 | .send({
|
| 341 | user_id: userId,
|
| 342 | stress_level: 3,
|
| 343 | anxiety_level: 4,
|
| 344 | energy_level: 7,
|
| 345 | recorded_at: now,
|
| 346 | mood_components: [
|
| 347 | { emotion: "invalid_emotion", intensity: 5 }
|
| 348 | ]
|
| 349 | });
|
| 350 |
|
| 351 | t.is(res.status, 400);
|
| 352 | t.regex(res.body.error, /Invalid emotion/);
|
| 353 | });
|
| 354 |
|
| 355 | test("Falha ao criar registro de humor com intensidade inválida (menor que 1)", async (t) => {
|
| 356 | const now = new Date().toISOString();
|
| 357 |
|
| 358 | const res = await request(app)
|
| 359 | .post("/mood")
|
| 360 | .send({
|
| 361 | user_id: userId,
|
| 362 | stress_level: 3,
|
| 363 | anxiety_level: 4,
|
| 364 | energy_level: 7,
|
| 365 | recorded_at: now,
|
| 366 | mood_components: [
|
| 367 | { emotion: "joy", intensity: 0 }
|
| 368 | ]
|
| 369 | });
|
| 370 |
|
| 371 | t.is(res.status, 400);
|
| 372 | t.regex(res.body.error, /Invalid intensity/);
|
| 373 | });
|
| 374 |
|
| 375 | test("Falha ao criar registro de humor com intensidade inválida (maior que 10)", async (t) => {
|
| 376 | const now = new Date().toISOString();
|
| 377 |
|
| 378 | const res = await request(app)
|
| 379 | .post("/mood")
|
| 380 | .send({
|
| 381 | user_id: userId,
|
| 382 | stress_level: 3,
|
| 383 | anxiety_level: 4,
|
| 384 | energy_level: 7,
|
| 385 | recorded_at: now,
|
| 386 | mood_components: [
|
| 387 | { emotion: "sad", intensity: 11 }
|
| 388 | ]
|
| 389 | });
|
| 390 |
|
| 391 | t.is(res.status, 400);
|
| 392 | t.regex(res.body.error, /Invalid intensity/);
|
| 393 | });
|
| 394 |
|
| 395 | test("Falha ao criar registro de humor com emoções duplicadas", async (t) => {
|
| 396 | const now = new Date().toISOString();
|
| 397 |
|
| 398 | const res = await request(app)
|
| 399 | .post("/mood")
|
| 400 | .send({
|
| 401 | user_id: userId,
|
| 402 | stress_level: 3,
|
| 403 | anxiety_level: 4,
|
| 404 | energy_level: 7,
|
| 405 | recorded_at: now,
|
| 406 | mood_components: [
|
| 407 | { emotion: "joy", intensity: 7 },
|
| 408 | { emotion: "joy", intensity: 5 }
|
| 409 | ]
|
| 410 | });
|
| 411 |
|
| 412 | t.is(res.status, 400);
|
| 413 | t.regex(res.body.error, /Duplicate emotion/);
|
| 414 | });
|
| 415 |
|
| 416 | test("Falha ao criar registro de humor com componente sem emoção de referencia", async (t) => {
|
| 417 | const now = new Date().toISOString();
|
| 418 |
|
| 419 | const res = await request(app)
|
| 420 | .post("/mood")
|
| 421 | .send({
|
| 422 | user_id: userId,
|
| 423 | stress_level: 3,
|
| 424 | anxiety_level: 4,
|
| 425 | energy_level: 7,
|
| 426 | recorded_at: now,
|
| 427 | mood_components: [
|
| 428 | { intensity: 5 }
|
| 429 | ]
|
| 430 | });
|
| 431 |
|
| 432 | t.is(res.status, 400);
|
| 433 | t.regex(res.body.error, /must have emotion and intensity/);
|
| 434 | });
|
| 435 |
|
| 436 | test("Falha ao criar registro de humor com componente sem intensidade", async (t) => {
|
| 437 | const now = new Date().toISOString();
|
| 438 |
|
| 439 | const res = await request(app)
|
| 440 | .post("/mood")
|
| 441 | .send({
|
| 442 | user_id: userId,
|
| 443 | stress_level: 3,
|
| 444 | anxiety_level: 4,
|
| 445 | energy_level: 7,
|
| 446 | recorded_at: now,
|
| 447 | mood_components: [
|
| 448 | { emotion: "joy" }
|
| 449 | ]
|
| 450 | });
|
| 451 |
|
| 452 | t.is(res.status, 400);
|
| 453 | t.regex(res.body.error, /must have emotion and intensity/);
|
| 454 | });
|
| 455 |
|
| 456 | test("Criar registro de humor com lista vazia de componentes deve funcionar", async (t) => {
|
| 457 | const now = new Date().toISOString();
|
| 458 |
|
| 459 | const res = await request(app)
|
| 460 | .post("/mood")
|
| 461 | .send({
|
| 462 | user_id: userId,
|
| 463 | stress_level: 3,
|
| 464 | anxiety_level: 4,
|
| 465 | energy_level: 7,
|
| 466 | recorded_at: now,
|
| 467 | mood_components: []
|
| 468 | });
|
| 469 |
|
| 470 | t.is(res.status, 201);
|
| 471 |
|
| 472 | // Verify no components were created
|
| 473 | const components = db.prepare("SELECT * FROM mood_components WHERE mood_id = ?").all(res.body.id);
|
| 474 | t.is(components.length, 0);
|
| 475 | });
|
| 476 |
|
| 477 | test("Descrição e título devem ser sanitizados (truncados em 500 caracteres)", async (t) => {
|
| 478 | const now = new Date().toISOString();
|
| 479 | const longText = "A".repeat(600);
|
| 480 |
|
| 481 | const res = await request(app)
|
| 482 | .post("/mood")
|
| 483 | .send({
|
| 484 | user_id: userId,
|
| 485 | stress_level: 3,
|
| 486 | anxiety_level: 4,
|
| 487 | energy_level: 7,
|
| 488 | recorded_at: now,
|
| 489 | title: longText,
|
| 490 | description: longText
|
| 491 | });
|
| 492 |
|
| 493 | t.is(res.status, 201);
|
| 494 |
|
| 495 | const mood = db.prepare("SELECT title, description FROM mood WHERE id = ?").get(res.body.id);
|
| 496 | t.is(mood.title.length, 500);
|
| 497 | t.is(mood.description.length, 500);
|
| 498 | });
|