createUser(); $set = $this->createSet($user, 'Bible'); $repository = app(ElementRepository::class); $genesis = $repository->create(new CreateElementDto( set: $set, name: 'Genesis', kind: 'book', parentElement: null, )); $repository->create(new CreateElementDto( set: $set, name: 'Exodus', kind: 'book', parentElement: null, )); $creation = $repository->create(new CreateElementDto( set: $set, name: 'Creation', kind: 'portion', parentElement: $genesis, )); $chapter = $repository->create(new CreateElementDto( set: $set, name: 'Chapter 1', kind: 'chapter', parentElement: $creation, )); $this->createSession($user); $response = $this->credentialedGet("/api/sets/{$set->getId()}"); $response->assertOk()->assertExactJson([ 'set' => [ 'id' => $set->getId(), 'name' => 'Bible', ], 'elements' => [ [ 'id' => $genesis->getId(), 'name' => 'Genesis', 'kind' => 'book', 'children' => [ [ 'id' => $creation->getId(), 'name' => 'Creation', 'kind' => 'portion', 'children' => [ [ 'id' => $chapter->getId(), 'name' => 'Chapter 1', 'kind' => 'chapter', 'children' => [], ], ], ], ], ], [ 'id' => $genesis->getId() + 1, 'name' => 'Exodus', 'kind' => 'book', 'children' => [], ], ], ]); } public function test_it_returns_an_empty_element_layout(): void { $user = $this->createUser(); $set = $this->createSet($user, 'Empty set'); $this->createSession($user); $response = $this->credentialedGet("/api/sets/{$set->getId()}"); $response->assertOk()->assertExactJson([ 'set' => [ 'id' => $set->getId(), 'name' => 'Empty set', ], 'elements' => [], ]); } public function test_it_returns_not_found_for_an_unknown_set(): void { $user = $this->createUser(); $this->createSession($user); $response = $this->credentialedGet('/api/sets/999'); $response->assertNotFound()->assertExactJson([ 'error' => 'set not found', ]); } public function test_it_rejects_an_unauthenticated_request(): void { $response = $this->getJson('/api/sets/1'); $response->assertStatus(401)->assertExactJson([ 'error' => 'unauthenticated', ]); } private function createUser(): User { return app(UserRepository::class)->create(new CreateUserDto( email: new EmailAddress('reader@example.com'), passwordHash: 'hashed-password', )); } private function createSet(User $user, string $name): Set { return app(SetRepository::class)->create(new CreateSetDto( name: $name, creator: $user, )); } private function createSession(User $user): void { $createdAt = new DateTimeImmutable( '2026-08-03T12:00:00', new DateTimeZone('UTC'), ); app(SessionRepository::class)->create(new CreateSessionDto( token: 'valid-token', user: $user, createdAt: $createdAt, expiresAt: $createdAt->modify('+7 days'), )); } private function credentialedGet(string $uri): TestResponse { return $this->withCredentials() ->withUnencryptedCookie( AuthMiddleware::COOKIE_NAME, 'valid-token', )->getJson($uri); } }