Spaces:
Sleeping
Sleeping
File size: 46,585 Bytes
1f5ea39 | 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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 | /**
* Journey API integration tests.
* Covers JOURNEY-INT-001 through JOURNEY-INT-020.
*/
import { describe, it, expect, vi, beforeAll, beforeEach, afterAll } from 'vitest';
import request from 'supertest';
import type { Application } from 'express';
import type { INestApplication } from '@nestjs/common';
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Step 1: Bare in-memory DB β schema applied in beforeAll after mocks register
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
const { testDb, dbMock } = vi.hoisted(() => {
const Database = require('better-sqlite3');
const db = new Database(':memory:');
db.exec('PRAGMA journal_mode = WAL');
db.exec('PRAGMA foreign_keys = ON');
db.exec('PRAGMA busy_timeout = 5000');
const mock = {
db,
closeDb: () => {},
reinitialize: () => {},
getPlaceWithTags: (placeId: number) => {
const place: any = db.prepare(`
SELECT p.*, c.name as category_name, c.color as category_color, c.icon as category_icon
FROM places p LEFT JOIN categories c ON p.category_id = c.id WHERE p.id = ?
`).get(placeId);
if (!place) return null;
const tags = db.prepare(`SELECT t.* FROM tags t JOIN place_tags pt ON t.id = pt.tag_id WHERE pt.place_id = ?`).all(placeId);
return { ...place, category: place.category_id ? { id: place.category_id, name: place.category_name, color: place.category_color, icon: place.category_icon } : null, tags };
},
canAccessTrip: (tripId: any, userId: number) =>
db.prepare(`SELECT t.id, t.user_id FROM trips t LEFT JOIN trip_members m ON m.trip_id = t.id AND m.user_id = ? WHERE t.id = ? AND (t.user_id = ? OR m.user_id IS NOT NULL)`).get(userId, tripId, userId),
isOwner: (tripId: any, userId: number) =>
!!db.prepare('SELECT id FROM trips WHERE id = ? AND user_id = ?').get(tripId, userId),
};
return { testDb: db, dbMock: mock };
});
vi.mock('../../src/db/database', () => dbMock);
vi.mock('../../src/config', () => ({
JWT_SECRET: 'test-jwt-secret-for-trek-testing-only',
ENCRYPTION_KEY: 'a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2a3b4c5d6a7b8c9d0e1f2',
updateJwtSecret: () => {},
SESSION_DURATION: '24h',
SESSION_DURATION_MS: 86400000,
SESSION_DURATION_SECONDS: 86400,
DEFAULT_LANGUAGE: 'en',
}));
vi.mock('../../src/websocket', () => ({
broadcast: vi.fn(),
broadcastToUser: vi.fn(),
setupWebSocket: vi.fn(),
getOnlineUserIds: vi.fn(() => []),
}));
vi.mock('../../src/services/memories/immichService', () => ({
uploadToImmich: vi.fn(async () => null),
getImmichCredentials: vi.fn(() => null),
}));
import { buildApp } from '../../src/bootstrap';
import { createTables } from '../../src/db/schema';
import { runMigrations } from '../../src/db/migrations';
import { resetTestDb, resetRateLimits } from '../helpers/test-db';
import {
createUser,
createAdmin,
createTrip,
createJourney,
createJourneyEntry,
addJourneyContributor,
} from '../helpers/factories';
import { authCookie } from '../helpers/auth';
import { invalidatePermissionsCache } from '../../src/services/permissions';
let nestApp: INestApplication;
let app: Application;
beforeAll(async () => {
createTables(testDb);
runMigrations(testDb);
nestApp = await buildApp();
app = nestApp.getHttpAdapter().getInstance();
});
beforeEach(() => {
resetTestDb(testDb);
resetRateLimits(nestApp);
invalidatePermissionsCache();
// Enable the journey addon
testDb.prepare(
"INSERT OR REPLACE INTO addons (id, name, description, type, icon, enabled, sort_order) VALUES ('journey', 'Journey', 'Travel journal', 'global', 'Compass', 1, 35)"
).run();
});
afterAll(async () => {
await nestApp.close();
testDb.close();
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// List journeys (JOURNEY-INT-001, 002)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('List journeys', () => {
it('JOURNEY-INT-001 β GET /api/journeys returns 200 with empty list initially', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.get('/api/journeys')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.journeys).toEqual([]);
});
it('JOURNEY-INT-002 β GET /api/journeys returns 401 without auth', async () => {
const res = await request(app).get('/api/journeys');
expect(res.status).toBe(401);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Create journey (JOURNEY-INT-003)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Create journey', () => {
it('JOURNEY-INT-003 β POST /api/journeys creates a journey', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.post('/api/journeys')
.set('Cookie', authCookie(user.id))
.send({ title: 'Japan 2026', subtitle: 'Cherry blossom season' });
expect(res.status).toBe(201);
expect(res.body.title).toBe('Japan 2026');
expect(res.body.subtitle).toBe('Cherry blossom season');
expect(res.body.id).toBeDefined();
// Should appear in listing now
const list = await request(app)
.get('/api/journeys')
.set('Cookie', authCookie(user.id));
expect(list.body.journeys).toHaveLength(1);
expect(list.body.journeys[0].title).toBe('Japan 2026');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Get journey detail (JOURNEY-INT-004, 005)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Get journey detail', () => {
it('JOURNEY-INT-004 β GET /api/journeys/:id returns full detail', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id, { title: 'Iceland' });
const res = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.title).toBe('Iceland');
expect(res.body.entries).toBeDefined();
expect(res.body.contributors).toBeDefined();
expect(res.body.stats).toBeDefined();
});
it('JOURNEY-INT-005 β GET /api/journeys/:id returns 404 for non-existent', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.get('/api/journeys/99999')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Update journey (JOURNEY-INT-006)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Update journey', () => {
it('JOURNEY-INT-006 β PATCH /api/journeys/:id updates journey', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id, { title: 'Draft' });
const res = await request(app)
.patch(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id))
.send({ title: 'Updated Title', subtitle: 'New subtitle' });
expect(res.status).toBe(200);
expect(res.body.title).toBe('Updated Title');
expect(res.body.subtitle).toBe('New subtitle');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Delete journey (JOURNEY-INT-007)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Delete journey', () => {
it('JOURNEY-INT-007 β DELETE /api/journeys/:id deletes journey', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.delete(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
// Verify it's gone
const get = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id));
expect(get.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Journey trips (JOURNEY-INT-008, 009)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey trips', () => {
it('JOURNEY-INT-008 β POST /api/journeys/:id/trips links a trip', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const trip = createTrip(testDb, user.id, { title: 'Paris', start_date: '2026-06-01', end_date: '2026-06-05' });
const res = await request(app)
.post(`/api/journeys/${journey.id}/trips`)
.set('Cookie', authCookie(user.id))
.send({ trip_id: trip.id });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
// Verify trip appears in journey detail
const detail = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id));
expect(detail.body.trips).toHaveLength(1);
expect(detail.body.trips[0].trip_id).toBe(trip.id);
});
it('JOURNEY-INT-009 β DELETE /api/journeys/:id/trips/:tripId unlinks trip', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const trip = createTrip(testDb, user.id, { title: 'Rome', start_date: '2026-07-01', end_date: '2026-07-03' });
// Link via API first (avoids factory column mismatch)
await request(app)
.post(`/api/journeys/${journey.id}/trips`)
.set('Cookie', authCookie(user.id))
.send({ trip_id: trip.id });
const res = await request(app)
.delete(`/api/journeys/${journey.id}/trips/${trip.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Journey entries (JOURNEY-INT-010, 011, 012)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey entries', () => {
it('JOURNEY-INT-010 β POST /api/journeys/:id/entries creates an entry', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/entries`)
.set('Cookie', authCookie(user.id))
.send({
title: 'First day in Tokyo',
story: 'Arrived at Narita airport.',
entry_date: '2026-04-01',
entry_time: '14:00',
location_name: 'Narita Airport',
});
expect(res.status).toBe(201);
expect(res.body.title).toBe('First day in Tokyo');
expect(res.body.entry_date).toBe('2026-04-01');
expect(res.body.id).toBeDefined();
});
it('JOURNEY-INT-011 β PATCH /api/journeys/entries/:id updates entry', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, {
title: 'Original',
entry_date: '2026-04-01',
});
const res = await request(app)
.patch(`/api/journeys/entries/${entry.id}`)
.set('Cookie', authCookie(user.id))
.send({ title: 'Updated entry title', story: 'Now with a story' });
expect(res.status).toBe(200);
expect(res.body.title).toBe('Updated entry title');
expect(res.body.story).toBe('Now with a story');
});
it('JOURNEY-INT-012 β DELETE /api/journeys/entries/:id deletes entry', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, {
title: 'To delete',
entry_date: '2026-04-02',
});
const res = await request(app)
.delete(`/api/journeys/entries/${entry.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Contributors (JOURNEY-INT-013, 014)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey contributors', () => {
it('JOURNEY-INT-013 β POST /api/journeys/:id/contributors adds a contributor', async () => {
const { user: owner } = createUser(testDb);
const { user: contributor } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/contributors`)
.set('Cookie', authCookie(owner.id))
.send({ user_id: contributor.id, role: 'editor' });
expect(res.status).toBe(201);
expect(res.body.success).toBe(true);
// Contributor should now be able to access the journey
const detail = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(contributor.id));
expect(detail.status).toBe(200);
expect(detail.body.title).toBeDefined();
});
it('JOURNEY-INT-014 β DELETE /api/journeys/:id/contributors/:userId removes contributor', async () => {
const { user: owner } = createUser(testDb);
const { user: contributor } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, contributor.id, 'editor');
const res = await request(app)
.delete(`/api/journeys/${journey.id}/contributors/${contributor.id}`)
.set('Cookie', authCookie(owner.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
// Contributor should no longer access the journey
const detail = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(contributor.id));
expect(detail.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Share link (JOURNEY-INT-015, 016, 017)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey share link', () => {
it('JOURNEY-INT-015 β GET /api/journeys/:id/share-link returns null initially', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.get(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.link).toBeNull();
});
it('JOURNEY-INT-016 β POST /api/journeys/:id/share-link creates a share link', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id))
.send({ share_timeline: true, share_gallery: true, share_map: false });
expect(res.status).toBe(200);
expect(res.body.token).toBeDefined();
expect(typeof res.body.token).toBe('string');
expect(res.body.created).toBe(true);
// GET should now return the link
const get = await request(app)
.get(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id));
expect(get.body.link).not.toBeNull();
expect(get.body.link.token).toBe(res.body.token);
expect(get.body.link.share_timeline).toBe(true);
expect(get.body.link.share_gallery).toBe(true);
expect(get.body.link.share_map).toBe(false);
});
it('JOURNEY-INT-017 β DELETE /api/journeys/:id/share-link deletes the share link', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
// Create first
await request(app)
.post(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id))
.send({ share_timeline: true, share_gallery: true, share_map: true });
// Delete
const res = await request(app)
.delete(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
// Verify it's gone
const get = await request(app)
.get(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id));
expect(get.body.link).toBeNull();
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Permission checks (JOURNEY-INT-018, 019)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey permissions', () => {
it('JOURNEY-INT-018 β contributor (viewer) can read but non-member cannot', async () => {
const { user: owner } = createUser(testDb);
const { user: viewer } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const journey = createJourney(testDb, owner.id, { title: 'Private Journey' });
addJourneyContributor(testDb, journey.id, viewer.id, 'viewer');
// Viewer can read
const viewerRes = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(viewer.id));
expect(viewerRes.status).toBe(200);
expect(viewerRes.body.title).toBe('Private Journey');
// Outsider cannot
const outsiderRes = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(outsider.id));
expect(outsiderRes.status).toBe(404);
});
it('JOURNEY-INT-019 β non-owner cannot delete a journey', async () => {
const { user: owner } = createUser(testDb);
const { user: editor } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, editor.id, 'editor');
// Editor can read
const readRes = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(editor.id));
expect(readRes.status).toBe(200);
// Editor cannot delete β only owner can
const delRes = await request(app)
.delete(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(editor.id));
expect(delRes.status).toBe(404);
// Journey still exists
const verify = await request(app)
.get(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(owner.id));
expect(verify.status).toBe(200);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Suggestions (JOURNEY-INT-020)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey suggestions', () => {
it('JOURNEY-INT-020 β GET /api/journeys/suggestions returns trip suggestions', async () => {
const { user } = createUser(testDb);
// Create a recent trip so it shows up in suggestions
createTrip(testDb, user.id, {
title: 'Recent Trip',
start_date: '2026-03-01',
end_date: '2026-03-05',
});
const res = await request(app)
.get('/api/journeys/suggestions')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.trips).toBeDefined();
expect(Array.isArray(res.body.trips)).toBe(true);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Available trips (JOURNEY-INT-021)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Available trips', () => {
it('JOURNEY-INT-021 β GET /api/journeys/available-trips returns user trips', async () => {
const { user } = createUser(testDb);
createTrip(testDb, user.id, { title: 'My Trip', start_date: '2026-05-01', end_date: '2026-05-03' });
const res = await request(app)
.get('/api/journeys/available-trips')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.trips).toBeDefined();
expect(Array.isArray(res.body.trips)).toBe(true);
expect(res.body.trips.length).toBeGreaterThanOrEqual(1);
expect(res.body.trips[0].title).toBe('My Trip');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Create journey validation (JOURNEY-INT-022)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Create journey validation', () => {
it('JOURNEY-INT-022 β POST /api/journeys returns 400 without title', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.post('/api/journeys')
.set('Cookie', authCookie(user.id))
.send({ subtitle: 'No title provided' });
expect(res.status).toBe(400);
expect(res.body.error).toBe('Title is required');
});
it('JOURNEY-INT-023 β POST /api/journeys returns 400 for blank title', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.post('/api/journeys')
.set('Cookie', authCookie(user.id))
.send({ title: ' ' });
expect(res.status).toBe(400);
expect(res.body.error).toBe('Title is required');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Provider photos (JOURNEY-INT-024, 025, 026)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Provider photos', () => {
it('JOURNEY-INT-024 β POST /api/journeys/entries/:id/provider-photos creates provider photo', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'immich', asset_id: 'abc-123', caption: 'Nice view' });
expect(res.status).toBe(201);
expect(res.body.provider).toBe('immich');
expect(res.body.asset_id).toBe('abc-123');
expect(res.body.caption).toBe('Nice view');
});
it('JOURNEY-INT-025 β POST /api/journeys/entries/:id/provider-photos returns 400 without required fields', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ caption: 'Missing provider and asset_id' });
expect(res.status).toBe(400);
expect(res.body.error).toBe('provider and asset_id required');
});
it('JOURNEY-INT-026 β POST /api/journeys/entries/:id/provider-photos returns 403 for viewer', async () => {
const { user: owner } = createUser(testDb);
const { user: viewer } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, viewer.id, 'viewer');
const entry = createJourneyEntry(testDb, journey.id, owner.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(viewer.id))
.send({ provider: 'immich', asset_id: 'xyz-456' });
expect(res.status).toBe(403);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Link photo to entry (JOURNEY-INT-027, 028)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Link photo to entry', () => {
it('JOURNEY-INT-027 β POST /api/journeys/entries/:id/link-photo moves photo', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry1 = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const entry2 = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-02' });
// Add a provider photo to entry1
const photoRes = await request(app)
.post(`/api/journeys/entries/${entry1.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'immich', asset_id: 'link-test-asset' });
// Link it to entry2
const res = await request(app)
.post(`/api/journeys/entries/${entry2.id}/link-photo`)
.set('Cookie', authCookie(user.id))
.send({ photo_id: photoRes.body.id });
expect(res.status).toBe(201);
expect(res.body.entry_id).toBe(entry2.id);
});
it('JOURNEY-INT-028 β POST /api/journeys/entries/:id/link-photo returns 400 without photo_id', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/link-photo`)
.set('Cookie', authCookie(user.id))
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toBe('journey_photo_id required');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Update photo (JOURNEY-INT-029, 030)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Update photo', () => {
it('JOURNEY-INT-029 β PATCH /api/journeys/photos/:id updates caption and sort_order', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
// Add a provider photo first
const photoRes = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'immich', asset_id: 'update-test-asset' });
const res = await request(app)
.patch(`/api/journeys/photos/${photoRes.body.id}`)
.set('Cookie', authCookie(user.id))
.send({ caption: 'Updated caption', sort_order: 5 });
expect(res.status).toBe(200);
expect(res.body.caption).toBe('Updated caption');
expect(res.body.sort_order).toBe(5);
});
it('JOURNEY-INT-030 β PATCH /api/journeys/photos/:id returns 404 for non-existent photo', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.patch('/api/journeys/photos/99999')
.set('Cookie', authCookie(user.id))
.send({ caption: 'No photo here' });
expect(res.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Delete photo via route (JOURNEY-INT-031, 032)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Delete photo (route)', () => {
it('JOURNEY-INT-031 β DELETE /api/journeys/photos/:id deletes photo', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const photoRes = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'immich', asset_id: 'del-test-asset' });
const res = await request(app)
.delete(`/api/journeys/photos/${photoRes.body.id}`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
it('JOURNEY-INT-032 β DELETE /api/journeys/photos/:id returns 404 for non-existent', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.delete('/api/journeys/photos/99999')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Journey entries sub-routes (JOURNEY-INT-033, 034)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Journey entries sub-routes', () => {
it('JOURNEY-INT-033 β GET /api/journeys/:id/entries returns entries list', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
createJourneyEntry(testDb, journey.id, user.id, { title: 'Day 1', entry_date: '2026-04-01' });
createJourneyEntry(testDb, journey.id, user.id, { title: 'Day 2', entry_date: '2026-04-02' });
const res = await request(app)
.get(`/api/journeys/${journey.id}/entries`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(200);
expect(res.body.entries).toHaveLength(2);
});
it('JOURNEY-INT-034 β GET /api/journeys/:id/entries returns 404 for inaccessible journey', async () => {
const { user: owner } = createUser(testDb);
const { user: outsider } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
const res = await request(app)
.get(`/api/journeys/${journey.id}/entries`)
.set('Cookie', authCookie(outsider.id));
expect(res.status).toBe(404);
});
it('JOURNEY-INT-035 β POST /api/journeys/:id/entries returns 400 without entry_date', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/entries`)
.set('Cookie', authCookie(user.id))
.send({ title: 'Missing date' });
expect(res.status).toBe(400);
expect(res.body.error).toBe('entry_date is required');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Update entry edge cases (JOURNEY-INT-036, 037)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Update entry edge cases', () => {
it('JOURNEY-INT-036 β PATCH /api/journeys/entries/:id returns 404 for non-existent entry', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.patch('/api/journeys/entries/99999')
.set('Cookie', authCookie(user.id))
.send({ title: 'Does not exist' });
expect(res.status).toBe(404);
});
it('JOURNEY-INT-037 β DELETE /api/journeys/entries/:id returns 404 for non-existent entry', async () => {
const { user } = createUser(testDb);
const res = await request(app)
.delete('/api/journeys/entries/99999')
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(404);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Trip link validation (JOURNEY-INT-038, 039)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Trip link validation', () => {
it('JOURNEY-INT-038 β POST /api/journeys/:id/trips returns 400 without trip_id', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/trips`)
.set('Cookie', authCookie(user.id))
.send({});
expect(res.status).toBe(400);
expect(res.body.error).toBe('trip_id required');
});
it('JOURNEY-INT-039 β DELETE /api/journeys/:id/trips/:tripId returns 403 for non-owner', async () => {
const { user: owner } = createUser(testDb);
const { user: editor } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, editor.id, 'editor');
const trip = createTrip(testDb, owner.id, { title: 'Link Trip', start_date: '2026-06-01', end_date: '2026-06-03' });
await request(app)
.post(`/api/journeys/${journey.id}/trips`)
.set('Cookie', authCookie(owner.id))
.send({ trip_id: trip.id });
const res = await request(app)
.delete(`/api/journeys/${journey.id}/trips/${trip.id}`)
.set('Cookie', authCookie(editor.id));
expect(res.status).toBe(403);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Contributor routes (JOURNEY-INT-040, 041, 042)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Contributor route validation', () => {
it('JOURNEY-INT-040 β POST /api/journeys/:id/contributors returns 400 without user_id', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.post(`/api/journeys/${journey.id}/contributors`)
.set('Cookie', authCookie(user.id))
.send({ role: 'editor' });
expect(res.status).toBe(400);
expect(res.body.error).toBe('user_id required');
});
it('JOURNEY-INT-041 β PATCH /api/journeys/:id/contributors/:userId updates role', async () => {
const { user: owner } = createUser(testDb);
const { user: contrib } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, contrib.id, 'viewer');
const res = await request(app)
.patch(`/api/journeys/${journey.id}/contributors/${contrib.id}`)
.set('Cookie', authCookie(owner.id))
.send({ role: 'editor' });
expect(res.status).toBe(200);
expect(res.body.success).toBe(true);
});
it('JOURNEY-INT-042 β PATCH /api/journeys/:id/contributors/:userId returns 403 for non-owner', async () => {
const { user: owner } = createUser(testDb);
const { user: editor } = createUser(testDb);
const { user: target } = createUser(testDb);
const journey = createJourney(testDb, owner.id);
addJourneyContributor(testDb, journey.id, editor.id, 'editor');
addJourneyContributor(testDb, journey.id, target.id, 'viewer');
const res = await request(app)
.patch(`/api/journeys/${journey.id}/contributors/${target.id}`)
.set('Cookie', authCookie(editor.id))
.send({ role: 'editor' });
expect(res.status).toBe(403);
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Share link with update (JOURNEY-INT-043, 044)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Share link update', () => {
it('JOURNEY-INT-043 β POST /api/journeys/:id/share-link updates existing share link permissions', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
// Create initial share link
const create = await request(app)
.post(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id))
.send({ share_timeline: true, share_gallery: true, share_map: true });
expect(create.body.created).toBe(true);
// Update permissions (same endpoint creates or updates)
const update = await request(app)
.post(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id))
.send({ share_timeline: true, share_gallery: false, share_map: false });
expect(update.status).toBe(200);
expect(update.body.token).toBe(create.body.token);
expect(update.body.created).toBe(false);
// Verify updated permissions
const get = await request(app)
.get(`/api/journeys/${journey.id}/share-link`)
.set('Cookie', authCookie(user.id));
expect(get.body.link.share_timeline).toBe(true);
expect(get.body.link.share_gallery).toBe(false);
expect(get.body.link.share_map).toBe(false);
});
it('JOURNEY-INT-044 β journey PATCH /:id can update status', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const res = await request(app)
.patch(`/api/journeys/${journey.id}`)
.set('Cookie', authCookie(user.id))
.send({ status: 'archived' });
expect(res.status).toBe(200);
expect(res.body.status).toBe('archived');
});
});
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
// Provider photos passphrase (JOURNEY-INT-046, 047)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Provider photos β passphrase persistence', () => {
it('JOURNEY-INT-046 β single mode with passphrase persists encrypted passphrase on trek_photos', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'synologyphotos', asset_id: 'shared-asset-1', passphrase: 'pp-test' });
expect(res.status).toBe(201);
const row = testDb.prepare('SELECT passphrase FROM trek_photos WHERE provider = ? AND asset_id = ? AND owner_id = ?')
.get('synologyphotos', 'shared-asset-1', user.id) as { passphrase: string | null } | undefined;
expect(row?.passphrase).not.toBeNull();
expect(typeof row?.passphrase).toBe('string');
});
it('JOURNEY-INT-047 β batch mode with passphrase persists encrypted passphrase on all trek_photos rows', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-02' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/provider-photos`)
.set('Cookie', authCookie(user.id))
.send({ provider: 'synologyphotos', asset_ids: ['batch-asset-1', 'batch-asset-2'], passphrase: 'pp-batch' });
expect(res.status).toBe(201);
expect(res.body.added).toBe(2);
for (const assetId of ['batch-asset-1', 'batch-asset-2']) {
const row = testDb.prepare('SELECT passphrase FROM trek_photos WHERE provider = ? AND asset_id = ? AND owner_id = ?')
.get('synologyphotos', assetId, user.id) as { passphrase: string | null } | undefined;
expect(row?.passphrase).not.toBeNull();
}
});
});
// Photo upload without files (JOURNEY-INT-045)
// βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
describe('Photo upload validation', () => {
it('JOURNEY-INT-045 β POST /api/journeys/entries/:id/photos returns 400 without files', async () => {
const { user } = createUser(testDb);
const journey = createJourney(testDb, user.id);
const entry = createJourneyEntry(testDb, journey.id, user.id, { entry_date: '2026-04-01' });
const res = await request(app)
.post(`/api/journeys/entries/${entry.id}/photos`)
.set('Cookie', authCookie(user.id));
expect(res.status).toBe(400);
expect(res.body.error).toBe('No files uploaded');
});
});
|