@@ -10,6 +10,7 @@ import {
1010 enablePasswordLoginForTests ,
1111} from './server-test-helper' ;
1212import { TEST_PASSWORDS } from './test-credentials' ;
13+ import { yjsService } from '../src/services/yjs.service' ;
1314
1415describe ( 'Document Routes' , ( ) => {
1516 let ownerUserId : string ;
@@ -63,6 +64,10 @@ describe('Document Routes', () => {
6364 expect ( response . status ) . toBe ( 201 ) ;
6465 const project = ( await json ( ) ) as { slug : string } ;
6566 projectSlug = project . slug ;
67+
68+ // Seed a real element into the project's Yjs elements document so the
69+ // GET /docs/:docId route has real data to return.
70+ await seedElement ( ownerUsername , projectSlug , 'doc-test-element' , 'Chapter One' ) ;
6671 } ) ;
6772
6873 afterAll ( async ( ) => {
@@ -118,13 +123,27 @@ describe('Document Routes', () => {
118123 expect ( response . status ) . toBe ( 404 ) ;
119124 } ) ;
120125
121- it ( 'should return 200 for valid project and docId ' , async ( ) => {
126+ it ( 'should return 200 with real element metadata for a known document ' , async ( ) => {
122127 const { response, json } = await client . request (
123- `/api/v1/projects/${ ownerUsername } /${ projectSlug } /docs/some- doc`
128+ `/api/v1/projects/${ ownerUsername } /${ projectSlug } /docs/doc-test-element `
124129 ) ;
125130 expect ( response . status ) . toBe ( 200 ) ;
126- const data = ( await json ( ) ) as { id : string } ;
127- expect ( data ) . toHaveProperty ( 'id' , 'some-doc' ) ;
131+ const data = ( await json ( ) ) as {
132+ id : string ;
133+ name : string ;
134+ type : string ;
135+ } ;
136+ // Real element data is returned rather than a fabricated placeholder
137+ expect ( data ) . toHaveProperty ( 'id' , 'doc-test-element' ) ;
138+ expect ( data ) . toHaveProperty ( 'name' , 'Chapter One' ) ;
139+ expect ( data ) . toHaveProperty ( 'type' , 'ITEM' ) ;
140+ } ) ;
141+
142+ it ( 'should return 404 for a non-existent document id' , async ( ) => {
143+ const { response } = await client . request (
144+ `/api/v1/projects/${ ownerUsername } /${ projectSlug } /docs/nonexistent-doc`
145+ ) ;
146+ expect ( response . status ) . toBe ( 404 ) ;
128147 } ) ;
129148 } ) ;
130149
@@ -169,3 +188,32 @@ describe('Document Routes', () => {
169188 } ) ;
170189 } ) ;
171190} ) ;
191+
192+ /**
193+ * Seed a single ITEM element into the project's Yjs elements document.
194+ */
195+ async function seedElement (
196+ username : string ,
197+ slug : string ,
198+ id : string ,
199+ name : string
200+ ) : Promise < void > {
201+ const docId = `${ username } :${ slug } :elements/` ;
202+ const sharedDoc = await yjsService . getDocument ( docId ) ;
203+ sharedDoc . doc . transact ( ( ) => {
204+ const elements = sharedDoc . doc . getArray < Record < string , unknown > > ( 'elements' ) ;
205+ elements . push ( [
206+ {
207+ id,
208+ name,
209+ type : 'ITEM' ,
210+ parentId : null ,
211+ order : 0 ,
212+ level : 0 ,
213+ expandable : false ,
214+ version : 1 ,
215+ metadata : { } ,
216+ } ,
217+ ] ) ;
218+ } ) ;
219+ }
0 commit comments