Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions src/features/chat/client/MainChatPageClient.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
useMainChatStore,
} from '../model/use-main-chat-store';
import { formatReverseOfferApprovalProgress } from '../model/types';
import { isSupporterForSpot } from '../model/mock';
import { isOwnedSpotRoom, isSupporterForSpot } from '../model/mock';
import {
buildScheduleSubtitle,
findSpotActionItem,
Expand Down Expand Up @@ -289,10 +289,6 @@ function FileListRow({ files }: { files: SharedFile[] }) {
}

/* ── 스팟 컨텍스트 아이템 목록 ────────────────────────────── */
function isOwnedSpotRoom(room: SpotChatRoom) {
return room.spot.authorId === room.currentUserId;
}

function SpotItemList({
room,
onOpenRoom,
Expand Down Expand Up @@ -822,6 +818,9 @@ export function MainChatPageClient({
) : (
<ChatBottomNav
mode="team"
showOwnerActions={
!!selectedSpotRoom && isOwnedSpotRoom(selectedSpotRoom)
}
showReverseOffer={
!!selectedSpotRoom &&
!isOwnedSpotRoom(selectedSpotRoom) &&
Expand Down
6 changes: 5 additions & 1 deletion src/features/chat/model/mock.ts
Original file line number Diff line number Diff line change
Expand Up @@ -554,9 +554,13 @@ export function getChatDirectoryCandidateById(

export function isSupporterForSpot(
room: SpotChatRoom,
userId: string = CHAT_CURRENT_USER_ID,
userId: string = room.currentUserId,
): boolean {
return room.spot.type === 'OFFER'
? room.spot.authorId === userId
: room.spot.authorId !== userId;
}

export function isOwnedSpotRoom(room: SpotChatRoom): boolean {
return room.spot.authorId === room.currentUserId;
}
97 changes: 96 additions & 1 deletion src/features/chat/model/use-main-chat-store.test.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { beforeEach, describe, expect, it } from 'vitest';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import {
PERSONAL_CHAT_CONTEXT_ID,
useMainChatStore,
} from './use-main-chat-store';
import { MOCK_FEED } from '@/features/feed/model/mock';
import { getChatRooms } from './mock';
import { getShareableSpotActionItems } from './spot-action-items';
import { spotsApi } from '@/features/spot/api/spot-api';

describe('useMainChatStore', () => {
beforeEach(() => {
vi.restoreAllMocks();
useMainChatStore.getState().reset();
useMainChatStore.setState({ rooms: getChatRooms() });
});
Expand Down Expand Up @@ -170,6 +172,99 @@ describe('useMainChatStore', () => {
).toHaveLength(1);
});

it('casts a vote through the spot API and syncs room/thread vote state', async () => {
const room = useMainChatStore
.getState()
.rooms.find(
(candidate) =>
candidate.id === 'spot-room-spot-6' &&
candidate.category === 'spot',
);

expect(room?.category).toBe('spot');

if (!room || room.category !== 'spot') {
throw new Error('Expected an owner spot room with votes.');
}

const vote = room.spot.votes[0];
const option = vote.options.find(
(candidate) => !candidate.voterIds.includes(room.currentUserId),
);

expect(option).toBeDefined();

if (!option) {
throw new Error('Expected an unselected vote option.');
}

const apiVote = {
...vote,
options: vote.options.map((candidate) => ({
...candidate,
voterIds:
candidate.id === option.id
? [room.currentUserId]
: candidate.voterIds.filter(
(id) => id !== room.currentUserId,
),
})),
};
const castSpy = vi
.spyOn(spotsApi, 'castVote')
.mockResolvedValue({ data: apiVote });

const updatedRoom = await useMainChatStore
.getState()
.castTeamVote(room.id, vote.id, option.id);

expect(castSpy).toHaveBeenCalledWith(room.spot.id, vote.id, [
option.id,
]);
expect(
updatedRoom?.spot.votes
.find((candidate) => candidate.id === vote.id)
?.options.find((candidate) => candidate.id === option.id)
?.voterIds,
).toContain(room.currentUserId);

const updatedVoteMessage = updatedRoom?.messages.find(
(message) => message.kind === 'vote' && message.vote.id === vote.id,
);

expect(updatedVoteMessage?.kind).toBe('vote');

if (updatedVoteMessage?.kind !== 'vote') {
throw new Error('Expected updated vote message.');
}

expect(
updatedVoteMessage.vote.options.find(
(candidate) => candidate.id === option.id,
)?.voterIds,
).toContain(room.currentUserId);
});

it('keeps owner-only actions blocked for non-owner supporter rooms', async () => {
useMainChatStore.getState().setSelectedContextId('spot-room-spot-2');

await expect(
useMainChatStore.getState().createTeamVote('안건', ['A', 'B']),
).resolves.toBeNull();
await expect(
useMainChatStore.getState().createTeamScheduleVote(),
).resolves.toBeNull();
await expect(
useMainChatStore
.getState()
.createTeamFileShare(
'owner-only.pdf',
12,
'https://example.com/owner-only.pdf',
),
).resolves.toBeNull();
});

it('creates reverse-offer approval counts on room and thread message', () => {
useMainChatStore.getState().setSelectedContextId('spot-room-spot-2');

Expand Down
Loading
Loading