feat(app): personal workspace in the account store, reset on account change
The store reads the personal workspace with the account (ready, loading, inactive, unconfirmed, missing, error; never created in the browser). A change of account drops profile, roles, avatar and workspace at once, and a late answer for the previous account is ignored. generateUuid gives v4 ids on the plain-HTTP LAN origin, where crypto.randomUUID is missing.
This commit is contained in:
1 parent
a0e069959b
commit
ff028ec071
4 files changed
+289
-19
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { describe, it, expect } from 'vitest'
|
||||
import { generateId } from '@/lib/id'
|
||||
import { generateId, generateUuid } from '@/lib/id'
|
||||
|
||||
describe('generateId', () => {
|
||||
it('returns a non-empty string', () => {
|
||||
@@ -12,3 +12,23 @@ describe('generateId', () => {
|
||||
expect(a).not.toBe(b)
|
||||
})
|
||||
})
|
||||
|
||||
describe('generateUuid', () => {
|
||||
const V4 = /^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/
|
||||
|
||||
it('returns a version 4 uuid', () => {
|
||||
expect(generateUuid()).toMatch(V4)
|
||||
})
|
||||
|
||||
it('still returns one where crypto.randomUUID is missing (plain HTTP on the LAN)', () => {
|
||||
const original = crypto.randomUUID
|
||||
Object.defineProperty(crypto, 'randomUUID', { value: undefined, configurable: true })
|
||||
try {
|
||||
const ids = new Set(Array.from({ length: 50 }, () => generateUuid()))
|
||||
expect([...ids].every((id) => V4.test(id))).toBe(true)
|
||||
expect(ids.size).toBe(50)
|
||||
} finally {
|
||||
Object.defineProperty(crypto, 'randomUUID', { value: original, configurable: true })
|
||||
}
|
||||
})
|
||||
})
|
||||
@@ -2,7 +2,9 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
createFakeSupabase,
|
||||
fakeAssetRow,
|
||||
fakeProfile,
|
||||
fakeSession,
|
||||
fakeUser,
|
||||
flush,
|
||||
type FakeSupabase,
|
||||
} from '../helpers/fakeSupabase'
|
||||
@@ -25,6 +27,7 @@ const INITIAL = {
|
||||
urlFailure: null,
|
||||
avatar: null,
|
||||
avatarUrl: null,
|
||||
workspace: { status: 'idle' as const },
|
||||
}
|
||||
|
||||
let fake: FakeSupabase
|
||||
@@ -112,10 +115,18 @@ describe('authStore', () => {
|
||||
expect(fake.storage.download).toHaveBeenCalledTimes(1)
|
||||
await useAuthStore.getState().refreshAvatar()
|
||||
expect(fake.storage.download).toHaveBeenCalledTimes(1)
|
||||
fake.tables.assets.push(fakeAssetRow({ id: 'asset-2', object_key: 'profiles/user-1/asset-2/avatar.webp', created_at: '2026-10-13T12:00:00Z' }))
|
||||
fake.tables.assets.push(
|
||||
fakeAssetRow({
|
||||
id: 'asset-2',
|
||||
object_key: 'profiles/user-1/asset-2/avatar.webp',
|
||||
created_at: '2026-10-13T12:00:00Z',
|
||||
}),
|
||||
)
|
||||
fake.tables.assets[0].status = 'deleted'
|
||||
await useAuthStore.getState().refreshAvatar()
|
||||
expect(fake.storage.download).toHaveBeenLastCalledWith('profiles/user-1/asset-2/avatar.webp')
|
||||
expect(fake.storage.download).toHaveBeenLastCalledWith(
|
||||
'profiles/user-1/asset-2/avatar.webp',
|
||||
)
|
||||
})
|
||||
|
||||
it('clearAvatar drops the URL and the clean row; sign-out clears it too', async () => {
|
||||
@@ -129,7 +140,10 @@ describe('authStore', () => {
|
||||
expect(useAuthStore.getState().avatar?.clean).toBeNull()
|
||||
await useAuthStore.getState().refreshAvatar()
|
||||
fake.emit('SIGNED_OUT', null)
|
||||
expect(useAuthStore.getState()).toMatchObject({ avatar: null, avatarUrl: null })
|
||||
expect(useAuthStore.getState()).toMatchObject({
|
||||
avatar: null,
|
||||
avatarUrl: null,
|
||||
})
|
||||
})
|
||||
|
||||
it('signOut calls the client and resets the state', async () => {
|
||||
@@ -139,4 +153,159 @@ describe('authStore', () => {
|
||||
expect(fake.auth.signOut).toHaveBeenCalled()
|
||||
expect(useAuthStore.getState().status).toBe('signed_out')
|
||||
})
|
||||
|
||||
describe('personal workspace', () => {
|
||||
it('is read with the account and ready by its id', async () => {
|
||||
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
expect(useAuthStore.getState().workspace).toEqual({ status: 'loading' })
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'ready',
|
||||
id: 'ws-1',
|
||||
})
|
||||
})
|
||||
|
||||
it('is missing when RLS returns no row for an active, confirmed account', async () => {
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({ status: 'missing' })
|
||||
})
|
||||
|
||||
it('tells a deactivated profile and an unconfirmed email apart, without asking for the workspace', async () => {
|
||||
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
|
||||
fake.tables.profiles = [fakeProfile({ active: false })]
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({ status: 'inactive' })
|
||||
fake.tables.profiles = [fakeProfile()]
|
||||
fake.emit(
|
||||
'SIGNED_IN',
|
||||
fakeSession(fakeUser({ id: 'user-1', email_confirmed_at: undefined })),
|
||||
)
|
||||
fake.emit(
|
||||
'USER_UPDATED',
|
||||
fakeSession(fakeUser({ email_confirmed_at: undefined })),
|
||||
)
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'unconfirmed',
|
||||
})
|
||||
})
|
||||
|
||||
it('reports a failed read as an error kind and reads again on refresh', async () => {
|
||||
const from = fake.client.from.bind(fake.client)
|
||||
let failing = true
|
||||
;(fake.client as unknown as { from: (t: string) => unknown }).from = (
|
||||
table: string,
|
||||
) =>
|
||||
table === 'workspaces' && failing
|
||||
? {
|
||||
select: () => ({
|
||||
eq: () => ({
|
||||
maybeSingle: async () => ({
|
||||
data: null,
|
||||
error: { code: '', message: 'FetchError' },
|
||||
status: 0,
|
||||
}),
|
||||
}),
|
||||
}),
|
||||
}
|
||||
: from(table as never)
|
||||
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'error',
|
||||
failure: 'network',
|
||||
})
|
||||
failing = false
|
||||
await useAuthStore.getState().refreshWorkspace()
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'ready',
|
||||
id: 'ws-1',
|
||||
})
|
||||
})
|
||||
|
||||
it('drops the previous account at once on a change of account, and ignores its late answers', async () => {
|
||||
fake.tables.workspaces = [
|
||||
{ id: 'ws-1', personal_user_id: 'user-1' },
|
||||
{ id: 'ws-2', personal_user_id: 'user-2' },
|
||||
]
|
||||
fake.tables.profiles = [
|
||||
fakeProfile(),
|
||||
fakeProfile({ id: 'user-2', display_name: 'Bob Local' }),
|
||||
]
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'ready',
|
||||
id: 'ws-1',
|
||||
})
|
||||
|
||||
// Account 1's next load is held back, then answers after account 2 signed in.
|
||||
let release: () => void = () => undefined
|
||||
const held = new Promise<void>((resolve) => (release = resolve))
|
||||
const from = fake.client.from.bind(fake.client)
|
||||
;(fake.client as unknown as { from: (t: string) => unknown }).from = (
|
||||
table: string,
|
||||
) => {
|
||||
const query = from(table as never) as unknown as {
|
||||
maybeSingle: () => Promise<unknown>
|
||||
}
|
||||
const original = query.maybeSingle
|
||||
query.maybeSingle = async () => {
|
||||
const answer = (await original()) as { data: { id?: string } | null }
|
||||
if (table === 'profiles' && answer.data?.id === 'user-1') await held
|
||||
return answer
|
||||
}
|
||||
return query
|
||||
}
|
||||
fake.emit('USER_UPDATED', fakeSession())
|
||||
await flush()
|
||||
fake.emit(
|
||||
'SIGNED_IN',
|
||||
fakeSession(fakeUser({ id: 'user-2', email: 'bob@example.test' })),
|
||||
)
|
||||
const switched = useAuthStore.getState()
|
||||
expect(switched.profile).toBeNull()
|
||||
expect(switched.workspace).toEqual({ status: 'loading' })
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().profile?.display_name).toBe('Bob Local')
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'ready',
|
||||
id: 'ws-2',
|
||||
})
|
||||
release()
|
||||
await flush()
|
||||
await flush()
|
||||
expect(useAuthStore.getState().profile?.display_name).toBe('Bob Local')
|
||||
expect(useAuthStore.getState().workspace).toEqual({
|
||||
status: 'ready',
|
||||
id: 'ws-2',
|
||||
})
|
||||
})
|
||||
|
||||
it('forgets the workspace on sign-out', async () => {
|
||||
fake.tables.workspaces = [{ id: 'ws-1', personal_user_id: 'user-1' }]
|
||||
stop = useAuthStore.getState().start()
|
||||
fake.emit('SIGNED_IN', fakeSession())
|
||||
await flush()
|
||||
await flush()
|
||||
fake.emit('SIGNED_OUT', null)
|
||||
expect(useAuthStore.getState().workspace).toEqual({ status: 'idle' })
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user