API and web running with same command

This commit is contained in:
gitea
2026-02-15 20:24:12 +01:00
parent d99b051a07
commit d870b5238a
6 changed files with 468 additions and 11 deletions
+3 -2
View File
@@ -63,9 +63,10 @@
submitting = false;
}
function handleLogout() {
async function handleLogout() {
userMenuOpen = false;
auth.logout();
profile = null;
await auth.logout();
push('/');
}
+112
View File
@@ -265,4 +265,116 @@ describe('decks API', () => {
await expect(decksApi.applySourceUpdate(supabase, 'd1', 'user1')).rejects.toThrow()
})
})
describe('Deck assignment and visibility (integration)', () => {
it('fetchMyDecks returns only decks owned by the given user', async () => {
const myDecks = [
{ id: 'd1', title: 'My Deck', copied_from_deck_id: null, copied_from_version: null, updated_at: '2025-01-01' },
]
nextResults.push(
{ data: myDecks, error: null },
{ data: [], error: null },
{ data: [], error: null }
)
countResults.push({ count: 1, error: null })
const result = await decksApi.fetchMyDecks(supabase, 'user1')
expect(result).toHaveLength(1)
expect(result[0].id).toBe('d1')
expect(result[0].title).toBe('My Deck')
// Supabase mock was queried with owner_id filter; only owned decks in result
expect(supabase.from).toHaveBeenCalledWith('decks')
})
it('fetchMyDecks includes both published and unpublished owned decks', async () => {
const myDecks = [
{ id: 'd1', title: 'Published', published: true, copied_from_deck_id: null, copied_from_version: null, updated_at: '2025-01-01' },
{ id: 'd2', title: 'Unpublished', published: false, copied_from_deck_id: null, copied_from_version: null, updated_at: '2025-01-02' },
]
nextResults.push(
{ data: myDecks, error: null },
{ data: [], error: null },
{ data: [], error: null }
)
countResults.push({ count: 2, error: null }, { count: 1, error: null })
const result = await decksApi.fetchMyDecks(supabase, 'user1')
expect(result).toHaveLength(2)
expect(result.map((d) => d.title)).toEqual(['Published', 'Unpublished'])
expect(result[0].published).toBe(true)
expect(result[1].published).toBe(false)
})
it('fetchPublishedDecks returns only published decks with user_has_this when user owns deck', async () => {
const publishedDecks = [
{ id: 'pub1', title: 'Public Deck', owner_id: 'user1', published: true },
]
nextResults.push(
{ data: publishedDecks, error: null },
{ data: [{ id: 'user1', display_name: 'Me', email: '[email protected]' }], error: null },
{ data: [], error: null },
{ data: [], error: null }
)
countResults.push({ count: 5, error: null })
const result = await decksApi.fetchPublishedDecks(supabase, 'user1')
expect(result).toHaveLength(1)
expect(result[0].published).toBe(true)
expect(result[0].user_has_this).toBe(true)
})
it('fetchPublishedDecks sets user_has_this true when user has a copy (In My decks)', async () => {
const publishedDecks = [
{ id: 'pub1', title: 'Other User Deck', owner_id: 'otherUser', published: true },
]
nextResults.push(
{ data: publishedDecks, error: null },
{ data: [{ id: 'otherUser', display_name: 'Other', email: '[email protected]' }], error: null },
{ data: [], error: null },
{ data: [{ copied_from_deck_id: 'pub1' }], error: null }
)
countResults.push({ count: 3, error: null })
const result = await decksApi.fetchPublishedDecks(supabase, 'user1')
expect(result).toHaveLength(1)
expect(result[0].owner_id).toBe('otherUser')
expect(result[0].user_has_this).toBe(true)
})
it('fetchPublishedDecks sets user_has_this false when user does not own and has no copy', async () => {
const publishedDecks = [
{ id: 'pub1', title: 'Stranger Deck', owner_id: 'stranger', published: true },
]
nextResults.push(
{ data: publishedDecks, error: null },
{ data: [{ id: 'stranger', display_name: 'Stranger', email: '[email protected]' }], error: null },
{ data: [], error: null },
{ data: [], error: null }
)
countResults.push({ count: 2, error: null })
const result = await decksApi.fetchPublishedDecks(supabase, 'user1')
expect(result).toHaveLength(1)
expect(result[0].user_has_this).toBe(false)
})
it('fetchPublishedDecks without userId returns user_has_this false for all', async () => {
const publishedDecks = [
{ id: 'pub1', title: 'Any Deck', owner_id: 'owner1', published: true },
]
nextResults.push(
{ data: publishedDecks, error: null },
{ data: [{ id: 'owner1', display_name: 'Owner', email: '[email protected]' }], error: null },
{ data: [], error: null }
)
countResults.push({ count: 1, error: null })
const result = await decksApi.fetchPublishedDecks(supabase)
expect(result).toHaveLength(1)
expect(result[0].user_has_this).toBe(false)
})
it('togglePublished updates published flag so deck can appear or disappear from community', async () => {
nextResults.push({ error: null })
await decksApi.togglePublished(supabase, 'd1', true)
expect(supabase.from).toHaveBeenCalledWith('decks')
nextResults.push({ error: null })
await decksApi.togglePublished(supabase, 'd1', false)
expect(supabase.from).toHaveBeenCalledWith('decks')
})
})
})