|
| 1 | +import { vi } from 'vitest' |
| 2 | + |
| 3 | +// Importing the tasks module transitively pulls in the root store |
| 4 | +// (lib/models → timezone → @/store); stub it so no Vuex store is built. |
| 5 | +vi.mock('@/store', () => ({ default: {} })) |
| 6 | + |
| 7 | +import tasksStore from '@/store/modules/tasks' |
| 8 | +import peopleStore from '@/store/modules/people' |
| 9 | + |
| 10 | +describe('Tasks store', () => { |
| 11 | + describe('Comment author resolution', () => { |
| 12 | + const studioPerson = { |
| 13 | + id: 'person-studio', |
| 14 | + first_name: 'Studio', |
| 15 | + last_name: 'Member', |
| 16 | + full_name: 'Studio Member (live)', |
| 17 | + role: 'manager', |
| 18 | + has_avatar: false |
| 19 | + } |
| 20 | + |
| 21 | + beforeEach(() => { |
| 22 | + peopleStore.cache.personMap = new Map([['person-studio', studioPerson]]) |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + peopleStore.cache.personMap = new Map() |
| 27 | + }) |
| 28 | + |
| 29 | + test('LOAD_TASK_COMMENTS_END prefers personMap over embedded author', () => { |
| 30 | + const state = { taskComments: {}, taskPreviews: {} } |
| 31 | + const comments = [ |
| 32 | + { |
| 33 | + id: 'comment-studio', |
| 34 | + person_id: 'person-studio', |
| 35 | + created_at: '2026-05-20T10:00:00', |
| 36 | + pinned: false, |
| 37 | + // Embedded author is stale; the live personMap must win. |
| 38 | + person: { id: 'person-studio', full_name: 'Studio Member (stale)' } |
| 39 | + } |
| 40 | + ] |
| 41 | + tasksStore.mutations.LOAD_TASK_COMMENTS_END(state, { |
| 42 | + taskId: 'task-1', |
| 43 | + comments |
| 44 | + }) |
| 45 | + const [comment] = state.taskComments['task-1'] |
| 46 | + expect(comment.person.full_name).toEqual('Studio Member (live)') |
| 47 | + }) |
| 48 | + |
| 49 | + test('LOAD_TASK_COMMENTS_END falls back to the embedded guest author', () => { |
| 50 | + const state = { taskComments: {}, taskPreviews: {} } |
| 51 | + const comments = [ |
| 52 | + { |
| 53 | + id: 'comment-guest', |
| 54 | + person_id: 'person-guest', |
| 55 | + created_at: '2026-05-20T11:00:00', |
| 56 | + pinned: false, |
| 57 | + // Guests are absent from personMap; the API embeds the author. |
| 58 | + person: { |
| 59 | + id: 'person-guest', |
| 60 | + first_name: 'Guest', |
| 61 | + last_name: 'Author', |
| 62 | + full_name: 'Guest Author', |
| 63 | + role: 'client', |
| 64 | + has_avatar: false |
| 65 | + } |
| 66 | + } |
| 67 | + ] |
| 68 | + tasksStore.mutations.LOAD_TASK_COMMENTS_END(state, { |
| 69 | + taskId: 'task-1', |
| 70 | + comments |
| 71 | + }) |
| 72 | + const [comment] = state.taskComments['task-1'] |
| 73 | + expect(comment.person.full_name).toEqual('Guest Author') |
| 74 | + // addAdditionalInformation enriches the embedded author for the avatar. |
| 75 | + expect(comment.person.initials).toEqual('GA') |
| 76 | + expect(comment.person.name).toEqual('Guest Author') |
| 77 | + }) |
| 78 | + |
| 79 | + test('LOAD_TASK_COMMENTS_END resolves embedded reply authors too', () => { |
| 80 | + const state = { taskComments: {}, taskPreviews: {} } |
| 81 | + const comments = [ |
| 82 | + { |
| 83 | + id: 'comment-studio', |
| 84 | + person_id: 'person-studio', |
| 85 | + created_at: '2026-05-20T10:00:00', |
| 86 | + pinned: false, |
| 87 | + person: studioPerson, |
| 88 | + replies: [ |
| 89 | + { |
| 90 | + id: 'reply-guest', |
| 91 | + person_id: 'person-guest', |
| 92 | + person: { |
| 93 | + id: 'person-guest', |
| 94 | + first_name: 'Guest', |
| 95 | + last_name: 'Author', |
| 96 | + full_name: 'Guest Author', |
| 97 | + role: 'client' |
| 98 | + } |
| 99 | + } |
| 100 | + ] |
| 101 | + } |
| 102 | + ] |
| 103 | + tasksStore.mutations.LOAD_TASK_COMMENTS_END(state, { |
| 104 | + taskId: 'task-1', |
| 105 | + comments |
| 106 | + }) |
| 107 | + const [comment] = state.taskComments['task-1'] |
| 108 | + expect(comment.replies[0].person.full_name).toEqual('Guest Author') |
| 109 | + expect(comment.replies[0].person.initials).toEqual('GA') |
| 110 | + }) |
| 111 | + |
| 112 | + test('ADD_REPLY_TO_COMMENT resolves the embedded guest reply author', () => { |
| 113 | + const comment = { id: 'comment-studio', replies: [] } |
| 114 | + const reply = { |
| 115 | + id: 'reply-guest', |
| 116 | + person_id: 'person-guest', |
| 117 | + person: { |
| 118 | + id: 'person-guest', |
| 119 | + first_name: 'Guest', |
| 120 | + last_name: 'Author', |
| 121 | + full_name: 'Guest Author', |
| 122 | + role: 'client' |
| 123 | + } |
| 124 | + } |
| 125 | + tasksStore.mutations.ADD_REPLY_TO_COMMENT({}, { comment, reply }) |
| 126 | + expect(comment.replies[0].person.full_name).toEqual('Guest Author') |
| 127 | + expect(comment.replies[0].person.initials).toEqual('GA') |
| 128 | + }) |
| 129 | + }) |
| 130 | +}) |
0 commit comments