|
| 1 | +/** |
| 2 | + * Route tests for GET /api/gallery (P3.1 community feed v1): |
| 3 | + * |
| 4 | + * - empty state on a fresh HOSTED_DIR |
| 5 | + * - listing over .share.json snapshots joined with .meta.json counters |
| 6 | + * - sorted by sharedAt desc |
| 7 | + * - expired shares excluded; non-expiring kept |
| 8 | + * - legacy shares without a sidecar are not listed |
| 9 | + * |
| 10 | + * Fixtures are real: shares created through the actual POST /share route into |
| 11 | + * the tempdir HOSTED_DIR (src/test/setup.ts), then per-test artifact surgery |
| 12 | + * for deterministic ordering/expiry. No network, no mocks of code under test. |
| 13 | + */ |
| 14 | +import { describe, expect, it, beforeEach } from 'vitest'; |
| 15 | +import Fastify, { type FastifyInstance } from 'fastify'; |
| 16 | +import { readdir, readFile, writeFile, rm } from 'node:fs/promises'; |
| 17 | +import { join } from 'node:path'; |
| 18 | +import { hostedRoutes } from '../routes/hostedRoutes'; |
| 19 | +import { ProjectService } from '../services/projectService'; |
| 20 | + |
| 21 | +const mockLogger = { info: () => {}, warn: () => {}, error: () => {}, debug: () => {} } as any; |
| 22 | +const HOSTED_DIR = process.env.HOSTED_DIR as string; |
| 23 | + |
| 24 | +async function buildApp() { |
| 25 | + const app = Fastify({ logger: false }); |
| 26 | + await app.register(hostedRoutes); |
| 27 | + await app.ready(); |
| 28 | + return app; |
| 29 | +} |
| 30 | + |
| 31 | +async function createAndShare(name: string) { |
| 32 | + const app = await buildApp(); |
| 33 | + const projects = new ProjectService(mockLogger); |
| 34 | + const { id } = await projects.createProject({ name, genre: 'platformer', artStyle: 'pixel' }); |
| 35 | + const res = await app.inject({ method: 'POST', url: `/api/projects/${id}/share` }); |
| 36 | + expect(res.statusCode).toBe(201); |
| 37 | + const token = res.json().hosted.id as string; |
| 38 | + return { app, token }; |
| 39 | +} |
| 40 | + |
| 41 | +async function getGallery(app: FastifyInstance) { |
| 42 | + const res = await app.inject({ method: 'GET', url: '/api/gallery' }); |
| 43 | + expect(res.statusCode).toBe(200); |
| 44 | + return res.json().games as Array<{ |
| 45 | + id: string; |
| 46 | + name: string; |
| 47 | + plays: number; |
| 48 | + remixes: number; |
| 49 | + sharedAt: string; |
| 50 | + url: string; |
| 51 | + }>; |
| 52 | +} |
| 53 | + |
| 54 | +/** Rewrite one share's snapshot timestamp + meta expiry for deterministic fixtures. */ |
| 55 | +async function redateShare(token: string, opts: { sharedAt?: string; expiresAt?: string | null }) { |
| 56 | + if (opts.sharedAt !== undefined) { |
| 57 | + const sharePath = join(HOSTED_DIR, `${token}.share.json`); |
| 58 | + const payload = JSON.parse(await readFile(sharePath, 'utf-8')); |
| 59 | + payload.sharedAt = opts.sharedAt; |
| 60 | + await writeFile(sharePath, JSON.stringify(payload, null, 2)); |
| 61 | + } |
| 62 | + if (opts.expiresAt !== undefined) { |
| 63 | + const metaPath = join(HOSTED_DIR, `${token}.meta.json`); |
| 64 | + const meta = JSON.parse(await readFile(metaPath, 'utf-8')); |
| 65 | + meta.expiresAt = opts.expiresAt; |
| 66 | + await writeFile(metaPath, JSON.stringify(meta, null, 2)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +/** Tests share one tempdir HOSTED_DIR — wipe artifacts between tests. */ |
| 71 | +beforeEach(async () => { |
| 72 | + const entries = await readdir(HOSTED_DIR).catch(() => [] as string[]); |
| 73 | + await Promise.all(entries.map((e) => rm(join(HOSTED_DIR, e), { recursive: true, force: true }))); |
| 74 | +}); |
| 75 | + |
| 76 | +describe('GET /api/gallery — community feed v1', () => { |
| 77 | + it('returns an empty list on a fresh hosted dir', async () => { |
| 78 | + const app = await buildApp(); |
| 79 | + const games = await getGallery(app); |
| 80 | + expect(games).toEqual([]); |
| 81 | + }); |
| 82 | + |
| 83 | + it('lists shared games with name, aggregate counts, sharedAt and playable URL', async () => { |
| 84 | + const { app, token } = await createAndShare('Gallery Fixture Alpha'); |
| 85 | + // One play + one remix recorded through the real counter path. |
| 86 | + await app.inject({ method: 'GET', url: `/share/${token}` }); // serve → plays+1 |
| 87 | + await app.inject({ method: 'GET', url: `/api/share/${token}/remix` }); // payload → remixes+1 |
| 88 | + |
| 89 | + const games = await getGallery(app); |
| 90 | + expect(games).toHaveLength(1); |
| 91 | + const game = games[0]; |
| 92 | + expect(game.id).toBe(token); |
| 93 | + expect(game.name).toBe('Gallery Fixture Alpha'); |
| 94 | + expect(game.plays).toBe(1); |
| 95 | + expect(game.remixes).toBe(1); |
| 96 | + expect(typeof game.sharedAt).toBe('string'); |
| 97 | + expect(game.url).toContain(`/share/${token}`); |
| 98 | + }); |
| 99 | + |
| 100 | + it('sorts by sharedAt descending regardless of share order on disk', async () => { |
| 101 | + const a = await createAndShare('Gallery Order A'); |
| 102 | + const b = await createAndShare('Gallery Order B'); |
| 103 | + const c = await createAndShare('Gallery Order C'); |
| 104 | + // Make on-disk share order oldest-first-newest-last but timestamps say C > A > B. |
| 105 | + await redateShare(a.token, { sharedAt: '2026-08-26T10:00:00.000Z' }); |
| 106 | + await redateShare(b.token, { sharedAt: '2026-08-26T09:00:00.000Z' }); |
| 107 | + await redateShare(c.token, { sharedAt: '2026-08-26T11:00:00.000Z' }); |
| 108 | + void a.app; |
| 109 | + const app = c.app; |
| 110 | + |
| 111 | + const games = await getGallery(app); |
| 112 | + expect(games.map((g) => g.name)).toEqual(['Gallery Order C', 'Gallery Order A', 'Gallery Order B']); |
| 113 | + }); |
| 114 | + |
| 115 | + it('excludes expired shares but keeps non-expiring ones', async () => { |
| 116 | + const live = await createAndShare('Gallery Live'); |
| 117 | + const dead = await createAndShare('Gallery Expired'); |
| 118 | + await redateShare(dead.token, { expiresAt: '2020-01-01T00:00:00.000Z' }); |
| 119 | + void live.app; |
| 120 | + |
| 121 | + const games = await getGallery(live.app); |
| 122 | + expect(games.map((g) => g.name)).toEqual(['Gallery Live']); |
| 123 | + expect(games.some((g) => g.id === dead.token)).toBe(false); |
| 124 | + }); |
| 125 | + |
| 126 | + it('does not list legacy shares without a .share.json sidecar', async () => { |
| 127 | + const { app, token } = await createAndShare('Gallery Legacy'); |
| 128 | + // Strip the sidecar → pre-slice-2 share; still playable, not gallery-worthy. |
| 129 | + const { unlink } = await import('node:fs/promises'); |
| 130 | + await unlink(join(HOSTED_DIR, `${token}.share.json`)); |
| 131 | + |
| 132 | + const games = await getGallery(app); |
| 133 | + expect(games).toEqual([]); |
| 134 | + }); |
| 135 | +}); |
0 commit comments