File size: 6,003 Bytes
c7a38a3
 
 
1fe7075
 
 
c7a38a3
1fe7075
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c7a38a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1fe7075
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { mkdtemp, writeFile } from 'node:fs/promises';
import os from 'node:os';
import { join } from 'node:path';
import { Test } from '@nestjs/testing';
import type { INestApplicationContext } from '@nestjs/common';
import { CatalogModule } from '../src/catalog/catalog.module.js';
import { adminCatalogProblems } from '../src/catalog/admin-catalog.js';
import { CatalogService } from '../src/catalog/catalog.service.js';
import type { CatalogEntry } from '../src/catalog/interfaces/catalog-entry.interface.js';
import { resetDatabase, sql } from './helpers.js';

const entry = (slug: string, overrides: Partial<CatalogEntry> = {}): CatalogEntry => ({
  slug,
  name: slug,
  collection: null,
  surfaces: ['floor'],
  widthMm: 600,
  heightMm: 600,
  position: null,
  imageUrl: `https://cdn.test/${slug}.jpg`,
  thumbnailUrl: null,
  assets: {},
  render: { layout: 'grid' },
  ...overrides,
});

describe('Catalog seed (e2e)', () => {
  let app: INestApplicationContext;
  let catalog: CatalogService;

  beforeAll(async () => {
    const moduleRef = await Test.createTestingModule({ imports: [CatalogModule] }).compile();
    app = moduleRef;
    catalog = moduleRef.get(CatalogService);
  });
  afterAll(() => app.close());
  beforeEach(resetDatabase);

  it('seeds the committed catalog and can run again', async () => {
    const entries = await catalog.load();
    expect(entries.length).toBeGreaterThan(0);
    expect(new Set(entries.map((tile) => tile.slug)).size).toBe(entries.length);
    expect(await catalog.seed(entries)).toBe(entries.length);
    expect(await catalog.seed(entries)).toBe(entries.length);
    expect((await sql('SELECT count(*)::int AS n FROM tiles'))[0].n).toBe(entries.length);
  });

  it('updates details, skips tiles without an image and never revives a deleted tile', async () => {
    await catalog.seed([entry('a'), entry('b')]);
    await sql("UPDATE tiles SET deleted_at = now() WHERE slug = 'b'");
    await catalog.seed([entry('a', { name: 'A renamed' }), entry('b', { name: 'B renamed' }), entry('c', { imageUrl: null })]);
    const rows = await sql('SELECT slug, name, deleted_at IS NOT NULL AS deleted FROM tiles ORDER BY slug');
    expect(rows).toEqual([
      { slug: 'a', name: 'A renamed', deleted: false },
      { slug: 'b', name: 'b', deleted: true },
    ]);
  });

  describe('admin-only Embellence catalog', () => {
    const file = new URL('../prisma/catalog.admin-embellence.json', import.meta.url);

    it('is valid, admin-only, and does not collide with the public catalog', async () => {
      const entries = await catalog.load(file);
      expect(entries.map((tile) => tile.slug)).toEqual([
        'embellence-wildflower-meadow',
        'embellence-tulip-garden',
        'embellence-archive-landscape-9477w',
        'embellence-dawn-forest-9883w',
      ]);
      expect(adminCatalogProblems(entries, await catalog.load())).toEqual([]);
      for (const tile of entries) {
        expect(tile).toMatchObject({ brand: 'embellence', organization: 'embellence-group', visibility: 'admin', surfaces: ['walls'] });
        expect(tile.imageUrl).toMatch(/^https:\/\/baetes-visualizer\.fra1\.cdn\.digitaloceanspaces\.com\/tiles\/embellence-group\//);
        expect(tile.render).toMatchObject({ groutWidthMeters: 0, tileVariationStrength: 0, patternMatch: 'straight', roughness: 0.86 });
        expect(tile.render.tileWidthMeters).toBeCloseTo(tile.widthMm! / 1000);
        expect(tile.render.tileHeightMeters).toBeCloseTo(tile.heightMm! / 1000);
      }
      const murals = entries.filter((tile) => tile.render.kind === 'mural').map((tile) => [tile.slug, tile.widthMm, tile.heightMm, tile.assets.sku]);
      expect(murals).toEqual([
        ['embellence-archive-landscape-9477w', 3920, 3200, '9477W'],
        ['embellence-dawn-forest-9883w', 1960, 3000, '9883W'],
      ]);
      expect(entries.some((tile) => tile.slug.includes('watercolour'))).toBe(false);
    });

    it('imports without duplicates, keeps visibility when catalog.json is reseeded, and never revives a deleted tile', async () => {
      const admin = await catalog.load(file);
      await catalog.seed(await catalog.load());
      expect(await catalog.seed(admin)).toBe(4);
      expect(await catalog.seed(admin)).toBe(4);
      await catalog.seed(await catalog.load());
      const rows = await sql(
        "SELECT slug, visibility::text, brand, organization FROM tiles WHERE organization = 'embellence-group' ORDER BY position",
      );
      expect(rows).toHaveLength(4);
      expect(rows.every((row) => row.visibility === 'admin' && row.brand === 'embellence')).toBe(true);
      expect((await sql("SELECT count(*)::int AS n FROM tiles WHERE visibility = 'public'"))[0].n).toBe((await catalog.load()).length);

      await sql("UPDATE tiles SET deleted_at = now() WHERE slug = 'embellence-tulip-garden'");
      await catalog.seed(admin);
      expect((await sql("SELECT deleted_at IS NOT NULL AS deleted FROM tiles WHERE slug = 'embellence-tulip-garden'"))[0].deleted).toBe(true);
    });

    it('refuses public entries, slug collisions and missing owners', async () => {
      const publicCatalog = [entry('demo')];
      expect(
        adminCatalogProblems(
          [
            entry('demo', { visibility: 'admin', brand: 'b', organization: 'o' }),
            entry('open', { brand: 'b', organization: 'o' }),
            entry('anon', { visibility: 'admin' }),
          ],
          publicCatalog,
        ),
      ).toEqual([
        'demo: slug is already in the public catalog',
        'open: visibility must be "admin"',
        'anon: brand and organization are required',
      ]);
    });

    it('rejects an unknown visibility when loading', async () => {
      const path = join(await mkdtemp(join(os.tmpdir(), 'catalog-')), 'bad.json');
      await writeFile(path, JSON.stringify({ tiles: [{ ...entry('x'), visibility: 'secret' }] }));
      await expect(catalog.load(path)).rejects.toThrow('unknown visibility');
    });
  });
});