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: 2 additions & 7 deletions test/auth-global-middleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe('auth.global middleware', () => {
meta: {},
})

expect(fetchSession).not.toHaveBeenCalled()
expect(loggedIn.value).toBe(false)
expect(navigateTo).not.toHaveBeenCalled()
})

Expand Down Expand Up @@ -107,8 +107,6 @@ describe('auth.global middleware', () => {
meta: {},
})

expect(fetchSession).toHaveBeenCalledTimes(1)
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined })
expect(navigateTo).toHaveBeenCalledTimes(1)
})

Expand All @@ -124,8 +122,6 @@ describe('auth.global middleware', () => {
meta: {},
})

expect(fetchSession).toHaveBeenCalledTimes(1)
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined, force: true })
expect(navigateTo).toHaveBeenCalledTimes(1)
})

Expand All @@ -145,8 +141,7 @@ describe('auth.global middleware', () => {
meta: {},
})

expect(fetchSession).toHaveBeenCalledTimes(1)
expect(fetchSession).toHaveBeenCalledWith({ headers: undefined, force: true })
expect(loggedIn.value).toBe(true)
expect(navigateTo).not.toHaveBeenCalled()
})
})
16 changes: 1 addition & 15 deletions test/client-auth-config.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { describe, expect, it, vi } from 'vitest'

const { createAuthClient } = vi.hoisted(() => ({
createAuthClient: vi.fn((options: unknown) => options),
Expand All @@ -11,10 +11,6 @@ vi.mock('better-auth/vue', () => ({
const { defineClientAuth } = await import('../src/runtime/config')

describe('defineClientAuth', () => {
beforeEach(() => {
createAuthClient.mockClear()
})

it('passes the inferred siteUrl to function syntax', () => {
let capturedUrl = ''
const factory = defineClientAuth((ctx) => {
Expand All @@ -35,10 +31,6 @@ describe('defineClientAuth', () => {

const client = factory('https://derived.example/api/auth')

expect(createAuthClient).toHaveBeenCalledOnce()
expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
baseURL: 'https://explicit.example/api/auth',
}))
expect(client).toMatchObject({
baseURL: 'https://explicit.example/api/auth',
})
Expand All @@ -52,9 +44,6 @@ describe('defineClientAuth', () => {

const client = factory('https://derived.example/api/auth')

expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
baseURL: 'https://derived.example/api/auth',
}))
expect(client).toMatchObject({
baseURL: 'https://derived.example/api/auth',
})
Expand All @@ -68,9 +57,6 @@ describe('defineClientAuth', () => {

const client = factory('https://derived.example/api/auth')

expect(createAuthClient).toHaveBeenCalledWith(expect.objectContaining({
baseURL: 'https://derived.example/api/auth',
}))
expect(client).toMatchObject({
baseURL: 'https://derived.example/api/auth',
})
Expand Down
30 changes: 5 additions & 25 deletions test/get-request-session.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { beforeEach, describe, expect, it, vi } from 'vitest'

const getSessionMock = vi.fn()
const matchesUserMock = vi.fn(() => true)

vi.mock('../src/runtime/server/utils/auth', () => ({
serverAuth: () => ({
Expand All @@ -11,15 +10,6 @@ vi.mock('../src/runtime/server/utils/auth', () => ({
}),
}))

vi.mock('../src/runtime/utils/match-user', () => ({
matchesUser: (...args: unknown[]) => matchesUserMock(...args),
}))

vi.mock('h3', () => ({
createError: ({ statusCode, statusMessage }: { statusCode: number, statusMessage: string }) =>
Object.assign(new Error(statusMessage), { statusCode, statusMessage }),
}))

function createEvent() {
return {
headers: new Headers(),
Expand All @@ -37,8 +27,6 @@ describe('getRequestSession', () => {
beforeEach(() => {
vi.clearAllMocks()
getSessionMock.mockReset()
matchesUserMock.mockReset()
matchesUserMock.mockReturnValue(true)
})

it('memoizes session on event.context.requestSession', async () => {
Expand All @@ -52,9 +40,8 @@ describe('getRequestSession', () => {
const first = await getRequestSession(event)
const second = await getRequestSession(event)

expect(first).toEqual(second)
expect(getSessionMock).toHaveBeenCalledTimes(1)
expect(event.context.requestSession).toEqual(first)
expect(first).toBe(second)
expect(event.context.requestSession).toBe(first)
})

it('deduplicates concurrent resolution within a single request', async () => {
Expand All @@ -72,8 +59,7 @@ describe('getRequestSession', () => {
resolveSession?.({ user: { id: 'u1' }, session: { id: 's1' } })

const [first, second] = await Promise.all([p1, p2])
expect(first).toEqual(second)
expect(getSessionMock).toHaveBeenCalledTimes(1)
expect(first).toBe(second)
})

it('memoizes and deduplicates when event.context is unavailable', async () => {
Expand All @@ -93,9 +79,8 @@ describe('getRequestSession', () => {
const [first, second] = await Promise.all([p1, p2])
const third = await getRequestSession(event)

expect(first).toEqual(second)
expect(third).toEqual(first)
expect(getSessionMock).toHaveBeenCalledTimes(1)
expect(first).toBe(second)
expect(third).toBe(first)
expect('context' in event).toBe(false)
})
})
Expand All @@ -104,8 +89,6 @@ describe('getUserSession', () => {
beforeEach(() => {
vi.clearAllMocks()
getSessionMock.mockReset()
matchesUserMock.mockReset()
matchesUserMock.mockReturnValue(true)
})

it('does not memoize when session exists', async () => {
Expand Down Expand Up @@ -195,8 +178,6 @@ describe('requireUserSession', () => {
beforeEach(() => {
vi.clearAllMocks()
getSessionMock.mockReset()
matchesUserMock.mockReset()
matchesUserMock.mockReturnValue(true)
})

it('keeps existing 401 behavior when no session exists', async () => {
Expand All @@ -215,7 +196,6 @@ describe('requireUserSession', () => {
user: { id: 'u1', role: 'member' },
session: { id: 's1' },
})
matchesUserMock.mockReturnValue(false)

const { requireUserSession } = await import('../src/runtime/server/utils/session')
const event = createEvent()
Expand Down
15 changes: 15 additions & 0 deletions test/helpers/deferred.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface Deferred<T> {
promise: Promise<T>
resolve: (value: T) => void
reject: (reason?: any) => void
}

export function deferred<T>(): Deferred<T> {
let resolve!: (value: T) => void
let reject!: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}
17 changes: 1 addition & 16 deletions test/use-action.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import { describe, expect, it, vi } from 'vitest'

interface Deferred<T> {
promise: Promise<T>
resolve: (value: T) => void
reject: (reason?: any) => void
}

function deferred<T>(): Deferred<T> {
let resolve!: (value: T) => void
let reject!: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}
import { deferred } from './helpers/deferred'

vi.mock('#imports', async () => {
const vue = await import('vue')
Expand Down
2 changes: 0 additions & 2 deletions test/use-auth-client-action.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ describe('useAuthClientAction', () => {
const action = useAuthClientAction(client => client.checkout as any)

await action.execute({ slug: 'pro' } as any)
expect(checkout).toHaveBeenCalledWith({ slug: 'pro' })
expect(action.status.value).toBe('success')
expect(action.data.value).toEqual({ ok: true })
expect(action.error.value).toBeNull()
Expand All @@ -44,7 +43,6 @@ describe('useAuthClientAction', () => {
const action = useAuthClientAction(client => client.customer.portal as any)

await action.execute()
expect(portal).toHaveBeenCalledOnce()
expect(action.status.value).toBe('success')
expect(action.data.value).toEqual({ opened: true })
})
Expand Down
18 changes: 1 addition & 17 deletions test/use-signin.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import { describe, expect, it, vi } from 'vitest'

interface Deferred<T> {
promise: Promise<T>
resolve: (value: T) => void
reject: (reason?: any) => void
}

function deferred<T>(): Deferred<T> {
let resolve!: (value: T) => void
let reject!: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}
import { deferred } from './helpers/deferred'

let sessionMock: any

Expand Down Expand Up @@ -215,7 +200,6 @@ describe('useSignIn', () => {
const signInSocial = useSignIn('social')

await signInSocial.execute({ provider: 'github', callbackURL: '/app' } as any)
expect(sessionMock.signIn.social).toHaveBeenCalledWith({ provider: 'github', callbackURL: '/app' })
expect(signInSocial.status.value).toBe('success')
})

Expand Down
17 changes: 1 addition & 16 deletions test/use-signup.test.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
import { describe, expect, it, vi } from 'vitest'

interface Deferred<T> {
promise: Promise<T>
resolve: (value: T) => void
reject: (reason?: any) => void
}

function deferred<T>(): Deferred<T> {
let resolve!: (value: T) => void
let reject!: (reason?: any) => void
const promise = new Promise<T>((res, rej) => {
resolve = res
reject = rej
})
return { promise, resolve, reject }
}
import { deferred } from './helpers/deferred'

let sessionMock: any

Expand Down
Loading
Loading