|
| 1 | +import { createPinia, setActivePinia } from "pinia"; |
| 2 | + |
| 3 | +vi.mock("~/stores/SearchStore", () => ({ |
| 4 | + useSearchStore: vi.fn(), |
| 5 | +})); |
| 6 | +vi.mock("~/stores/MatchmakingStore", () => ({ |
| 7 | + useMatchmakingStore: vi.fn(), |
| 8 | +})); |
| 9 | +vi.mock("~/stores/NotificationStore", () => ({ |
| 10 | + useNotificationStore: vi.fn(), |
| 11 | +})); |
| 12 | +vi.mock("~/stores/ApplicationSettingsStore", () => ({ |
| 13 | + useApplicationSettingsStore: vi.fn(), |
| 14 | +})); |
| 15 | +vi.mock("~/graphql/getGraphqlClient", () => ({ |
| 16 | + default: vi.fn(), |
| 17 | +})); |
| 18 | +vi.mock("~/web-sockets/Socket", () => ({ |
| 19 | + default: { connect: vi.fn() }, |
| 20 | +})); |
| 21 | +vi.mock("~/graphql/graphqlGen", () => ({ |
| 22 | + generateQuery: vi.fn(), |
| 23 | + generateSubscription: vi.fn(), |
| 24 | +})); |
| 25 | +vi.mock("~/graphql/meGraphql", () => ({ |
| 26 | + meFields: {}, |
| 27 | +})); |
| 28 | + |
| 29 | +// Provide Nuxt auto-imports as globals |
| 30 | +(globalThis as any).useSearchStore = vi.fn(); |
| 31 | +(globalThis as any).useMatchmakingStore = vi.fn(); |
| 32 | +(globalThis as any).useNotificationStore = vi.fn(); |
| 33 | +(globalThis as any).useApplicationSettingsStore = vi.fn(); |
| 34 | +(globalThis as any).useMatchLobbyStore = vi.fn(() => ({ |
| 35 | + subscribeToMyMatches: vi.fn(), |
| 36 | + subscribeToLiveMatches: vi.fn(), |
| 37 | + subscribeToLiveTournaments: vi.fn(), |
| 38 | + subscribeToOpenRegistrationTournaments: vi.fn(), |
| 39 | + subscribeToOpenMatches: vi.fn(), |
| 40 | + subscribeToChatTournaments: vi.fn(), |
| 41 | + subscribeToManagingMatches: vi.fn(), |
| 42 | + subscribeToManagingTournaments: vi.fn(), |
| 43 | +})); |
| 44 | +(globalThis as any).useNuxtApp = vi.fn(() => ({ $wsClient: { terminate: vi.fn(), on: vi.fn() } })); |
| 45 | + |
| 46 | +import { useAuthStore } from "./AuthStore"; |
| 47 | +import { e_player_roles_enum } from "~/generated/zeus"; |
| 48 | + |
| 49 | +describe("AuthStore", () => { |
| 50 | + beforeEach(() => { |
| 51 | + setActivePinia(createPinia()); |
| 52 | + }); |
| 53 | + |
| 54 | + describe("isRoleAbove", () => { |
| 55 | + it("returns false when me is null", () => { |
| 56 | + const store = useAuthStore(); |
| 57 | + expect(store.isRoleAbove(e_player_roles_enum.user)).toBe(false); |
| 58 | + }); |
| 59 | + |
| 60 | + it("returns true when user role >= target role", () => { |
| 61 | + const store = useAuthStore(); |
| 62 | + store.me = { role: e_player_roles_enum.administrator } as any; |
| 63 | + expect(store.isRoleAbove(e_player_roles_enum.match_organizer)).toBe(true); |
| 64 | + }); |
| 65 | + |
| 66 | + it("returns false when user role < target role", () => { |
| 67 | + const store = useAuthStore(); |
| 68 | + store.me = { role: e_player_roles_enum.user } as any; |
| 69 | + expect(store.isRoleAbove(e_player_roles_enum.administrator)).toBe(false); |
| 70 | + }); |
| 71 | + |
| 72 | + it("returns true for same role", () => { |
| 73 | + const store = useAuthStore(); |
| 74 | + store.me = { role: e_player_roles_enum.streamer } as any; |
| 75 | + expect(store.isRoleAbove(e_player_roles_enum.streamer)).toBe(true); |
| 76 | + }); |
| 77 | + |
| 78 | + it("respects full hierarchy: user < verified_user < streamer < match_organizer < tournament_organizer < administrator", () => { |
| 79 | + const store = useAuthStore(); |
| 80 | + const hierarchy = [ |
| 81 | + e_player_roles_enum.user, |
| 82 | + e_player_roles_enum.verified_user, |
| 83 | + e_player_roles_enum.streamer, |
| 84 | + e_player_roles_enum.match_organizer, |
| 85 | + e_player_roles_enum.tournament_organizer, |
| 86 | + e_player_roles_enum.administrator, |
| 87 | + ]; |
| 88 | + |
| 89 | + for (let i = 0; i < hierarchy.length; i++) { |
| 90 | + store.me = { role: hierarchy[i] } as any; |
| 91 | + for (let j = 0; j < hierarchy.length; j++) { |
| 92 | + expect(store.isRoleAbove(hierarchy[j])).toBe(i >= j); |
| 93 | + } |
| 94 | + } |
| 95 | + }); |
| 96 | + }); |
| 97 | + |
| 98 | + describe("computed role checks", () => { |
| 99 | + it("isAdmin true only for administrator", () => { |
| 100 | + const store = useAuthStore(); |
| 101 | + store.me = { role: e_player_roles_enum.administrator } as any; |
| 102 | + expect(store.isAdmin).toBe(true); |
| 103 | + store.me = { role: e_player_roles_enum.user } as any; |
| 104 | + expect(store.isAdmin).toBe(false); |
| 105 | + }); |
| 106 | + |
| 107 | + it("isUser true only for user role", () => { |
| 108 | + const store = useAuthStore(); |
| 109 | + store.me = { role: e_player_roles_enum.user } as any; |
| 110 | + expect(store.isUser).toBe(true); |
| 111 | + store.me = { role: e_player_roles_enum.administrator } as any; |
| 112 | + expect(store.isUser).toBe(false); |
| 113 | + }); |
| 114 | + |
| 115 | + it("isMatchOrganizer true only for match_organizer", () => { |
| 116 | + const store = useAuthStore(); |
| 117 | + store.me = { role: e_player_roles_enum.match_organizer } as any; |
| 118 | + expect(store.isMatchOrganizer).toBe(true); |
| 119 | + store.me = { role: e_player_roles_enum.user } as any; |
| 120 | + expect(store.isMatchOrganizer).toBe(false); |
| 121 | + }); |
| 122 | + }); |
| 123 | +}); |
0 commit comments