nest-graph-inspector 0.12.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -285,15 +285,18 @@ describe(HttpOutputAdapter.name, () => {
285
285
  },
286
286
  body,
287
287
  });
288
- const install = async (layoutFilePath?: string) => {
288
+ const install = async (saveAs?: 'file' | 'runtime') => {
289
289
  const port = await availablePort();
290
- await adapter.execute({} as never, {
291
- type: 'http',
292
- host: '127.0.0.1',
293
- port,
294
- path: '/graph',
295
- ...(layoutFilePath ? { layoutFilePath } : {}),
296
- });
290
+ await adapter.execute(
291
+ {} as never,
292
+ {
293
+ type: 'http',
294
+ host: '127.0.0.1',
295
+ port,
296
+ path: '/graph',
297
+ ...(saveAs ? { layout: { saveAs } } : {}),
298
+ } as never,
299
+ );
297
300
  origin = `http://127.0.0.1:${port}`;
298
301
  };
299
302
  const expectNoFile = (filePath: string) =>
@@ -301,6 +304,7 @@ describe(HttpOutputAdapter.name, () => {
301
304
 
302
305
  beforeEach(async () => {
303
306
  layoutDir = await mkdtemp(join(tmpdir(), 'graph-layout-'));
307
+ jest.spyOn(process, 'cwd').mockReturnValue(layoutDir);
304
308
  });
305
309
 
306
310
  afterEach(async () => {
@@ -308,247 +312,315 @@ describe(HttpOutputAdapter.name, () => {
308
312
  await rm(layoutDir, { recursive: true, force: true });
309
313
  });
310
314
 
311
- it('serves an empty layout from both GET paths until one is saved', async () => {
312
- await install(join(layoutDir, 'layout.json'));
315
+ describe("saveAs: 'file' (default)", () => {
316
+ const muse = () => join(layoutDir, '.muse');
317
+
318
+ it('serves an empty layout from both GET paths until one is saved', async () => {
319
+ await install();
320
+
321
+ for (const path of ['/graph/layout.json', '/graph/layout']) {
322
+ const response = await get(`${origin}${path}`);
323
+
324
+ expect(response.statusCode).toBe(200);
325
+ expect(response.headers['content-type']).toBe(
326
+ 'application/json; charset=utf-8',
327
+ );
328
+ expect(JSON.parse(response.body)).toEqual({
329
+ version: '1',
330
+ modules: {},
331
+ });
332
+ }
333
+ });
313
334
 
314
- for (const path of ['/graph/layout.json', '/graph/layout']) {
315
- const response = await get(`${origin}${path}`);
335
+ it('saves a layout from both POST paths to ./.muse, formatted for committing, and serves it back', async () => {
336
+ await install();
337
+
338
+ for (const [path, x] of [
339
+ ['/graph/layout', 1],
340
+ ['/graph/layout.json', 2],
341
+ ] as const) {
342
+ const saved: GraphLayout = {
343
+ ...layout,
344
+ modules: { ...layout.modules, AppModule: { position: { x, y: 0 } } },
345
+ };
346
+
347
+ const response = await post(`${origin}${path}`, JSON.stringify(saved));
348
+
349
+ expect(response.statusCode).toBe(200);
350
+ expect(JSON.parse(response.body)).toEqual({
351
+ ok: true,
352
+ message: 'Layout saved successfully',
353
+ });
354
+ await expect(readFile(muse(), 'utf8')).resolves.toBe(
355
+ `${JSON.stringify(saved, null, 2)}\n`,
356
+ );
357
+ expect(
358
+ JSON.parse((await get(`${origin}/graph/layout.json`)).body),
359
+ ).toEqual(saved);
360
+ }
361
+ });
362
+
363
+ it('saves explicitly configured saveAs: "file" to ./.muse too', async () => {
364
+ await install('file');
365
+
366
+ const response = await post(
367
+ `${origin}/graph/layout`,
368
+ JSON.stringify(layout),
369
+ );
316
370
 
317
371
  expect(response.statusCode).toBe(200);
318
- expect(response.headers['content-type']).toBe(
319
- 'application/json; charset=utf-8',
372
+ await expect(readFile(muse(), 'utf8')).resolves.toBe(
373
+ serializeGraphLayout(layout),
374
+ );
375
+ });
376
+
377
+ it('rejects an invalid layout with the reasons, and leaves the saved one alone', async () => {
378
+ await writeFile(muse(), serializeGraphLayout(layout));
379
+ await install();
380
+
381
+ const response = await post(
382
+ `${origin}/graph/layout`,
383
+ JSON.stringify({
384
+ version: '2',
385
+ modules: { UserModule: { position: { x: 'left', y: 0 } } },
386
+ }),
320
387
  );
388
+
389
+ expect(response.statusCode).toBe(400);
321
390
  expect(JSON.parse(response.body)).toEqual({
322
- version: '1',
323
- modules: {},
391
+ ok: false,
392
+ message: 'Invalid layout payload',
393
+ errors: [
394
+ 'layout/version must be "1"',
395
+ 'layout/modules/UserModule/position/x must be a finite number',
396
+ ],
324
397
  });
325
- }
326
- });
398
+ await expect(readFile(muse(), 'utf8')).resolves.toBe(
399
+ serializeGraphLayout(layout),
400
+ );
401
+ });
327
402
 
328
- it('saves a layout from both POST paths, formatted for committing, and serves it back', async () => {
329
- const filePath = join(layoutDir, 'layout.json');
330
- await install(filePath);
331
-
332
- for (const [path, x] of [
333
- ['/graph/layout', 1],
334
- ['/graph/layout.json', 2],
335
- ] as const) {
336
- const saved: GraphLayout = {
337
- ...layout,
338
- modules: { ...layout.modules, AppModule: { position: { x, y: 0 } } },
339
- };
403
+ it.each([
404
+ ['an empty body', ''],
405
+ ['malformed JSON', '{"version":'],
406
+ ])('rejects %s', async (_label, body) => {
407
+ await install();
340
408
 
341
- const response = await post(`${origin}${path}`, JSON.stringify(saved));
409
+ const response = await post(`${origin}/graph/layout`, body);
342
410
 
343
- expect(response.statusCode).toBe(200);
411
+ expect(response.statusCode).toBe(400);
344
412
  expect(JSON.parse(response.body)).toEqual({
345
- ok: true,
346
- message: 'Layout saved successfully',
413
+ ok: false,
414
+ message: 'Request body is not valid JSON.',
347
415
  });
348
- await expect(readFile(filePath, 'utf8')).resolves.toBe(
349
- `${JSON.stringify(saved, null, 2)}\n`,
416
+ await expectNoFile(muse());
417
+ });
418
+
419
+ it('refuses a body over the size limit before parsing it', async () => {
420
+ await install();
421
+
422
+ const response = await post(
423
+ `${origin}/graph/layout`,
424
+ 'x'.repeat(MAX_LAYOUT_BODY_BYTES + 1),
350
425
  );
351
- expect(
352
- JSON.parse((await get(`${origin}/graph/layout.json`)).body),
353
- ).toEqual(saved);
354
- }
355
- });
356
426
 
357
- it('resolves a relative layoutFilePath against the working directory and creates its parent directories', async () => {
358
- jest.spyOn(process, 'cwd').mockReturnValue(layoutDir);
359
- await install('nested/dir/layout.json');
427
+ expect(response.statusCode).toBe(413);
428
+ expect(JSON.parse(response.body)).toEqual({
429
+ ok: false,
430
+ message: 'Request body is too large.',
431
+ });
432
+ await expectNoFile(muse());
433
+ });
360
434
 
361
- const response = await post(
362
- `${origin}/graph/layout`,
363
- JSON.stringify(layout),
364
- );
435
+ it('refuses to read or save the layout without a token', async () => {
436
+ await install();
365
437
 
366
- expect(response.statusCode).toBe(200);
367
- await expect(
368
- readFile(join(layoutDir, 'nested', 'dir', 'layout.json'), 'utf8'),
369
- ).resolves.toBe(serializeGraphLayout(layout));
370
- });
438
+ const read = await httpGet(`${origin}/graph/layout.json`);
439
+ const save = await httpRequest(`${origin}/graph/layout`, {
440
+ method: 'POST',
441
+ headers: { 'content-type': 'application/json' },
442
+ body: JSON.stringify(layout),
443
+ });
371
444
 
372
- it('saves to nest-graph-layout.json in the working directory when no path is configured', async () => {
373
- jest.spyOn(process, 'cwd').mockReturnValue(layoutDir);
374
- await install();
445
+ expect(read.statusCode).toBe(401);
446
+ expect(save.statusCode).toBe(401);
447
+ await expectNoFile(muse());
448
+ });
375
449
 
376
- const response = await post(
377
- `${origin}/graph/layout`,
378
- JSON.stringify(layout),
379
- );
450
+ it('reports a layout file it cannot read back instead of serving it as empty', async () => {
451
+ await install();
380
452
 
381
- expect(response.statusCode).toBe(200);
382
- await expect(
383
- readFile(join(layoutDir, 'nest-graph-layout.json'), 'utf8'),
384
- ).resolves.toBe(serializeGraphLayout(layout));
385
- });
453
+ // What a merge conflict leaves behind in a committed layout file.
454
+ await writeFile(muse(), '<<<<<<< HEAD\n{}\n');
455
+ const unparsable = await get(`${origin}/graph/layout.json`);
386
456
 
387
- it('rejects an invalid layout with the reasons, and leaves the saved one alone', async () => {
388
- const filePath = join(layoutDir, 'layout.json');
389
- await writeFile(filePath, serializeGraphLayout(layout));
390
- await install(filePath);
457
+ expect(unparsable.statusCode).toBe(500);
458
+ expect(JSON.parse(unparsable.body)).toMatchObject({ ok: false });
459
+ expect(JSON.parse(unparsable.body).message).toContain(
460
+ `Graph layout file ${muse()} is not valid JSON`,
461
+ );
391
462
 
392
- const response = await post(
393
- `${origin}/graph/layout`,
394
- JSON.stringify({
395
- version: '2',
396
- modules: { UserModule: { position: { x: 'left', y: 0 } } },
397
- }),
398
- );
463
+ await writeFile(muse(), JSON.stringify({ version: '1' }));
464
+ const invalid = await get(`${origin}/graph/layout.json`);
399
465
 
400
- expect(response.statusCode).toBe(400);
401
- expect(JSON.parse(response.body)).toEqual({
402
- ok: false,
403
- message: 'Invalid layout payload',
404
- errors: [
405
- 'layout/version must be "1"',
406
- 'layout/modules/UserModule/position/x must be a finite number',
407
- ],
466
+ expect(invalid.statusCode).toBe(500);
467
+ expect(JSON.parse(invalid.body)).toEqual({
468
+ ok: false,
469
+ message: `Graph layout file ${muse()} is not a valid graph layout`,
470
+ errors: ['layout is missing required property "modules"'],
471
+ });
408
472
  });
409
- await expect(readFile(filePath, 'utf8')).resolves.toBe(
410
- serializeGraphLayout(layout),
411
- );
412
- });
413
473
 
414
- it.each([
415
- ['an empty body', ''],
416
- ['malformed JSON', '{"version":'],
417
- ])('rejects %s', async (_label, body) => {
418
- const filePath = join(layoutDir, 'layout.json');
419
- await install(filePath);
420
-
421
- const response = await post(`${origin}/graph/layout`, body);
474
+ it('lands overlapping saves one at a time, so the file always holds one whole layout', async () => {
475
+ await install();
476
+ // Different sizes on purpose: two interleaved writes of unequal length
477
+ // would leave the longer one's tail behind the shorter one.
478
+ const large: GraphLayout = {
479
+ version: '1',
480
+ modules: Object.fromEntries(
481
+ Array.from({ length: 500 }, (_, index) => [
482
+ `Module${index}`,
483
+ { position: { x: index, y: index } },
484
+ ]),
485
+ ),
486
+ };
422
487
 
423
- expect(response.statusCode).toBe(400);
424
- expect(JSON.parse(response.body)).toEqual({
425
- ok: false,
426
- message: 'Request body is not valid JSON.',
488
+ const responses = await Promise.all([
489
+ post(`${origin}/graph/layout`, JSON.stringify(large)),
490
+ post(`${origin}/graph/layout`, JSON.stringify(layout)),
491
+ post(`${origin}/graph/layout`, JSON.stringify(large)),
492
+ ]);
493
+
494
+ expect(responses.map((response) => response.statusCode)).toEqual([
495
+ 200, 200, 200,
496
+ ]);
497
+ expect([
498
+ serializeGraphLayout(large),
499
+ serializeGraphLayout(layout),
500
+ ]).toContain(await readFile(muse(), 'utf8'));
427
501
  });
428
- await expectNoFile(filePath);
429
502
  });
430
503
 
431
- it('refuses a body over the size limit before parsing it', async () => {
432
- const filePath = join(layoutDir, 'layout.json');
433
- await install(filePath);
504
+ describe("saveAs: 'runtime'", () => {
505
+ it('serves an empty layout from both GET paths until one is saved, entirely from RAM', async () => {
506
+ await install('runtime');
434
507
 
435
- const response = await post(
436
- `${origin}/graph/layout`,
437
- 'x'.repeat(MAX_LAYOUT_BODY_BYTES + 1),
438
- );
508
+ for (const path of ['/graph/layout.json', '/graph/layout']) {
509
+ const response = await get(`${origin}${path}`);
439
510
 
440
- expect(response.statusCode).toBe(413);
441
- expect(JSON.parse(response.body)).toEqual({
442
- ok: false,
443
- message: 'Request body is too large.',
511
+ expect(response.statusCode).toBe(200);
512
+ expect(response.headers['content-type']).toBe(
513
+ 'application/json; charset=utf-8',
514
+ );
515
+ expect(JSON.parse(response.body)).toEqual({
516
+ version: '1',
517
+ modules: {},
518
+ });
519
+ }
520
+
521
+ await expectNoFile(join(layoutDir, '.muse'));
444
522
  });
445
- await expectNoFile(filePath);
446
- });
447
523
 
448
- it('refuses to read or save the layout without a token', async () => {
449
- const filePath = join(layoutDir, 'layout.json');
450
- await install(filePath);
524
+ it('saves and reads back a layout from memory, writing nothing to disk', async () => {
525
+ await install('runtime');
451
526
 
452
- const read = await httpGet(`${origin}/graph/layout.json`);
453
- const save = await httpRequest(`${origin}/graph/layout`, {
454
- method: 'POST',
455
- headers: { 'content-type': 'application/json' },
456
- body: JSON.stringify(layout),
457
- });
527
+ for (const [path, x] of [
528
+ ['/graph/layout', 1],
529
+ ['/graph/layout.json', 2],
530
+ ] as const) {
531
+ const saved: GraphLayout = {
532
+ ...layout,
533
+ modules: { ...layout.modules, AppModule: { position: { x, y: 0 } } },
534
+ };
458
535
 
459
- expect(read.statusCode).toBe(401);
460
- expect(save.statusCode).toBe(401);
461
- await expectNoFile(filePath);
462
- });
536
+ const response = await post(`${origin}${path}`, JSON.stringify(saved));
463
537
 
464
- it('reports a layout file it cannot read back instead of serving it as empty', async () => {
465
- const filePath = join(layoutDir, 'layout.json');
466
- await install(filePath);
538
+ expect(response.statusCode).toBe(200);
539
+ expect(JSON.parse(response.body)).toEqual({
540
+ ok: true,
541
+ message: 'Layout saved successfully',
542
+ });
543
+ expect(
544
+ JSON.parse((await get(`${origin}/graph/layout.json`)).body),
545
+ ).toEqual(saved);
546
+ }
467
547
 
468
- // What a merge conflict leaves behind in a committed layout file.
469
- await writeFile(filePath, '<<<<<<< HEAD\n{}\n');
470
- const unparsable = await get(`${origin}/graph/layout.json`);
548
+ await expectNoFile(join(layoutDir, '.muse'));
549
+ });
471
550
 
472
- expect(unparsable.statusCode).toBe(500);
473
- expect(JSON.parse(unparsable.body)).toMatchObject({ ok: false });
474
- expect(JSON.parse(unparsable.body).message).toContain(
475
- `Graph layout file ${filePath} is not valid JSON`,
476
- );
551
+ it('rejects an invalid layout without touching the in-memory layout or disk', async () => {
552
+ await install('runtime');
553
+ await post(`${origin}/graph/layout`, JSON.stringify(layout));
477
554
 
478
- await writeFile(filePath, JSON.stringify({ version: '1' }));
479
- const invalid = await get(`${origin}/graph/layout.json`);
555
+ const response = await post(
556
+ `${origin}/graph/layout`,
557
+ JSON.stringify({
558
+ version: '2',
559
+ modules: { UserModule: { position: { x: 'left', y: 0 } } },
560
+ }),
561
+ );
480
562
 
481
- expect(invalid.statusCode).toBe(500);
482
- expect(JSON.parse(invalid.body)).toEqual({
483
- ok: false,
484
- message: `Graph layout file ${filePath} is not a valid graph layout`,
485
- errors: ['layout is missing required property "modules"'],
563
+ expect(response.statusCode).toBe(400);
564
+ expect(JSON.parse(response.body)).toEqual({
565
+ ok: false,
566
+ message: 'Invalid layout payload',
567
+ errors: [
568
+ 'layout/version must be "1"',
569
+ 'layout/modules/UserModule/position/x must be a finite number',
570
+ ],
571
+ });
572
+ expect(
573
+ JSON.parse((await get(`${origin}/graph/layout.json`)).body),
574
+ ).toEqual(layout);
575
+ await expectNoFile(join(layoutDir, '.muse'));
486
576
  });
487
- });
488
577
 
489
- it('reports a layout path it cannot write to', async () => {
490
- const blocker = join(layoutDir, 'not-a-directory');
491
- await writeFile(blocker, '');
492
- const filePath = join(blocker, 'layout.json');
493
- await install(filePath);
578
+ it.each([
579
+ ['an empty body', ''],
580
+ ['malformed JSON', '{"version":'],
581
+ ])('rejects %s', async (_label, body) => {
582
+ await install('runtime');
494
583
 
495
- const response = await post(
496
- `${origin}/graph/layout`,
497
- JSON.stringify(layout),
498
- );
584
+ const response = await post(`${origin}/graph/layout`, body);
499
585
 
500
- expect(response.statusCode).toBe(500);
501
- expect(JSON.parse(response.body).message).toContain(
502
- `Graph layout could not be saved to ${filePath}`,
503
- );
504
- });
586
+ expect(response.statusCode).toBe(400);
587
+ expect(JSON.parse(response.body)).toEqual({
588
+ ok: false,
589
+ message: 'Request body is not valid JSON.',
590
+ });
591
+ await expectNoFile(join(layoutDir, '.muse'));
592
+ });
505
593
 
506
- it('lands overlapping saves one at a time, so the file always holds one whole layout', async () => {
507
- const filePath = join(layoutDir, 'layout.json');
508
- await install(filePath);
509
- // Different sizes on purpose: two interleaved writes of unequal length
510
- // would leave the longer one's tail behind the shorter one.
511
- const large: GraphLayout = {
512
- version: '1',
513
- modules: Object.fromEntries(
514
- Array.from({ length: 500 }, (_, index) => [
515
- `Module${index}`,
516
- { position: { x: index, y: index } },
517
- ]),
518
- ),
519
- };
520
-
521
- const responses = await Promise.all([
522
- post(`${origin}/graph/layout`, JSON.stringify(large)),
523
- post(`${origin}/graph/layout`, JSON.stringify(layout)),
524
- post(`${origin}/graph/layout`, JSON.stringify(large)),
525
- ]);
526
-
527
- expect(responses.map((response) => response.statusCode)).toEqual([
528
- 200, 200, 200,
529
- ]);
530
- expect([
531
- serializeGraphLayout(large),
532
- serializeGraphLayout(layout),
533
- ]).toContain(await readFile(filePath, 'utf8'));
534
- });
594
+ it('refuses a body over the size limit before parsing it', async () => {
595
+ await install('runtime');
535
596
 
536
- it('creates a missing parent directory only when a layout is saved', async () => {
537
- const filePath = join(layoutDir, 'later', 'layout.json');
538
- await install(filePath);
597
+ const response = await post(
598
+ `${origin}/graph/layout`,
599
+ 'x'.repeat(MAX_LAYOUT_BODY_BYTES + 1),
600
+ );
539
601
 
540
- await get(`${origin}/graph/layout.json`);
541
- await expect(readFile(join(layoutDir, 'later'))).rejects.toMatchObject({
542
- code: 'ENOENT',
602
+ expect(response.statusCode).toBe(413);
603
+ expect(JSON.parse(response.body)).toEqual({
604
+ ok: false,
605
+ message: 'Request body is too large.',
606
+ });
607
+ await expectNoFile(join(layoutDir, '.muse'));
543
608
  });
544
609
 
545
- expect(
546
- (await post(`${origin}/graph/layout`, JSON.stringify(layout)))
547
- .statusCode,
548
- ).toBe(200);
549
- await expect(readFile(filePath, 'utf8')).resolves.toBe(
550
- serializeGraphLayout(layout),
551
- );
610
+ it('refuses to read or save the layout without a token', async () => {
611
+ await install('runtime');
612
+
613
+ const read = await httpGet(`${origin}/graph/layout.json`);
614
+ const save = await httpRequest(`${origin}/graph/layout`, {
615
+ method: 'POST',
616
+ headers: { 'content-type': 'application/json' },
617
+ body: JSON.stringify(layout),
618
+ });
619
+
620
+ expect(read.statusCode).toBe(401);
621
+ expect(save.statusCode).toBe(401);
622
+ await expectNoFile(join(layoutDir, '.muse'));
623
+ });
552
624
  });
553
625
  });
554
626