mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 05:21:15 +00:00
5651c11803
- design_book takes a brief (energy, decoration, collages, theme), a cover archetype and a cover lockup, and pages.overrides that give single pages of the plan another beat or layout. Directives written before parse as they did, and a design asked for none of it is composed exactly as before. - The engine composes the seven cover archetypes from the book's own photos, words, theme faces and colours, lockups, shapes and stickers, varied by the seed: spine band, poster pop, cut-out, illustrated postcard, grid and badge, minimal serif and stamp collage. A title the directives give a display tier is held to it, and the notes say when a band, a badge or a stamp holds it smaller or a cover leaves a layout out. - A lively or loud book turns two in five hero and grid spreads into collages, script overlays, badge grids, stamp rows or two-tone splits, the first of them always, turns and frames the prints of its other pages, sets its cover title larger and takes one or two, or three to six, stickers of its theme's motifs a spread, never over words, a photo's detail or the fold. A calm book asked for collages gets them and nothing else. - Every turn the engine gives a print or a sticker stays within what the brief's energy takes, a calm book's within its theme's own, so a design is never judged against its own brief. - New checks name a flat cover, a page without a lead, colours or faces off the theme and, for a design made with a brief, a book livelier or calmer than it asked, on the pages the design made and on a page revised in the draft. Page edits answer with the art checks too, and the assistant's cards name them in every language. - list_book_assets lists the shapes, faces, lockups, themes and patterns beside the stickers, and the Studio's shape groups and photo frames are one list the assistant reads too. - The Studio prompt gains an index of themes, archetypes and faces and JSON recipes of the elements to add and style for each cover and three lively spreads, and a turn that designs the book may preview sixteen pages, still three images a request; a viewer is told of none of it.
172 lines
8.9 KiB
TypeScript
172 lines
8.9 KiB
TypeScript
/**
|
|
* list_book_assets (JBASSET-*): the Studio's catalogues searched through
|
|
* JourneyBookMcp, the sticker library and the shapes, faces, lockups, themes
|
|
* and patterns beside it, and a sticker placed with
|
|
* edit_book_page from the library the edit context hands the engine. Taken
|
|
* from JourneyBookModule over a snapshot test db.
|
|
*
|
|
* What it pins: the tool waits for the Journey addon, only reads and sits in
|
|
* the journey group; any member may search, an outsider is told the journey
|
|
* is not there; the answer is the catalogue's rows with their slots; a
|
|
* sticker the library holds lands on the page in its proportions, one it does
|
|
* not hold is refused with the engine's code and nothing is written.
|
|
*/
|
|
import type { McpContext } from '../../../src/nest-mcp';
|
|
import { getEntry, type ClassRef } from '../../../src/nest-mcp/metadata';
|
|
import { AddonsService } from '../../../src/nest/addons/addons.service';
|
|
import { JourneyBookMcp } from '../../../src/nest/journey/journey-book.mcp';
|
|
import { JourneyBookModule } from '../../../src/nest/journey/journey-book.module';
|
|
import { createSnapshotTestDb } from '../../helpers/db-mock';
|
|
import { addJourneyContributor, makeJourney } from '../../helpers/factories/journeys';
|
|
import { makeUser } from '../../helpers/factories/users';
|
|
import { resetTestDb } from '../../helpers/test-db';
|
|
import { createTestModule, type TestModule } from '../../helpers/test-module';
|
|
import { createTestOrm, type TestOrm } from '../../helpers/test-orm';
|
|
import { bookOverviewSchema, bookWriteResultSchema, type ListBookAssetsRequest } from '@trek/shared';
|
|
|
|
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
const db = createSnapshotTestDb();
|
|
let t: TestModule;
|
|
let orm: TestOrm;
|
|
let mcp: JourneyBookMcp;
|
|
|
|
beforeAll(async () => {
|
|
t = await createTestModule({ db, imports: [JourneyBookModule] });
|
|
mcp = t.get(JourneyBookMcp);
|
|
orm = await createTestOrm(db);
|
|
});
|
|
beforeEach(() => {
|
|
resetTestDb(db);
|
|
orm.clear();
|
|
vi.restoreAllMocks();
|
|
});
|
|
afterAll(async () => {
|
|
await orm.close();
|
|
await t.close();
|
|
db.close();
|
|
});
|
|
|
|
const ctx = (userId: number) => ({ userId }) as McpContext;
|
|
const json = (result: { content: Array<{ type: string; text: string }> }) => JSON.parse(result.content[0]?.text ?? '');
|
|
|
|
/** A journey with a blank book, and the id of its one double spread. */
|
|
async function bookFor(ownerId: number) {
|
|
const journey = await makeJourney(orm, ownerId);
|
|
await mcp.createBook({ journeyId: journey.id }, ctx(ownerId));
|
|
const overview = bookOverviewSchema.parse(json(await mcp.getBook({ journeyId: journey.id }, ctx(ownerId))));
|
|
const inner = overview.spreads.find((s) => s.role === 'inner')?.id ?? '';
|
|
return { journey, inner };
|
|
}
|
|
|
|
describe('list_book_assets', () => {
|
|
it('JBASSET-001: waits for the Journey addon, reads only, in the journey group', async () => {
|
|
const { user } = await makeUser(orm);
|
|
const entry = getEntry(JourneyBookMcp as unknown as ClassRef, 'listAssets');
|
|
if (entry?.kind !== 'tool') throw new Error('listAssets is no tool');
|
|
expect(entry.options.name).toBe('list_book_assets');
|
|
expect(entry.options.access).toEqual({ group: 'journey', mode: 'read' });
|
|
expect(entry.options.annotations?.readOnlyHint).toBe(true);
|
|
const enabled = vi.spyOn(t.get(AddonsService), 'isAddonEnabled');
|
|
enabled.mockResolvedValue(false);
|
|
expect(await entry.options.when?.(ctx(user.id), mcp)).toBe(false);
|
|
enabled.mockResolvedValue(true);
|
|
expect(await entry.options.when?.(ctx(user.id), mcp)).toBe(true);
|
|
});
|
|
|
|
it('JBASSET-002: any member searches the library, an outsider is told the journey is not there', async () => {
|
|
const { user: owner } = await makeUser(orm);
|
|
const { user: viewer } = await makeUser(orm);
|
|
const { user: stranger } = await makeUser(orm);
|
|
const journey = await makeJourney(orm, owner.id);
|
|
await addJourneyContributor(orm, journey.id, viewer.id, 'viewer');
|
|
|
|
const answer = json(
|
|
await mcp.listAssets({ journeyId: journey.id, kind: 'sticker', query: 'lemon', limit: 3 }, ctx(viewer.id)),
|
|
);
|
|
expect(answer.kind).toBe('sticker');
|
|
expect(answer.rows).toHaveLength(3);
|
|
expect(answer.rows[0]).toMatchObject({ id: 'lemon', theme: 'mediterranean', aspect: 1.25 });
|
|
expect(answer.rows[0].slots).toEqual([
|
|
{ name: 'lemon', default: '#ffd23f' },
|
|
{ name: 'leaves', default: '#3f8f4f' },
|
|
]);
|
|
expect(answer.total).toBeGreaterThanOrEqual(3);
|
|
|
|
const paper = json(await mcp.listAssets({ journeyId: journey.id, kind: 'sticker', theme: 'paper' }, ctx(owner.id)));
|
|
expect(paper.rows.every((row: { theme: string }) => row.theme === 'paper')).toBe(true);
|
|
await expect(mcp.listAssets({ journeyId: journey.id, kind: 'sticker' }, ctx(stranger.id))).rejects.toThrow(
|
|
'Journey not found',
|
|
);
|
|
});
|
|
|
|
it('JBASSET-005: lists the shapes, faces, lockups, themes and patterns too, each row an id, a line and its tags', async () => {
|
|
const { user: owner } = await makeUser(orm);
|
|
const journey = await makeJourney(orm, owner.id);
|
|
const list = async (args: ListBookAssetsRequest) =>
|
|
json(await mcp.listAssets({ journeyId: journey.id, ...args }, ctx(owner.id)));
|
|
|
|
const arches = await list({ kind: 'shape', query: 'arch', limit: 3 });
|
|
expect(arches).toMatchObject({ kind: 'shape', rows: [{ id: 'arch' }, {}, {}] });
|
|
expect(arches.total).toBeGreaterThan(3);
|
|
expect(arches.rows[0]).toMatchObject({ frame: true, edges: true, tags: expect.arrayContaining(['basic']) });
|
|
const scripts = await list({ kind: 'font', tags: ['script'] });
|
|
expect(scripts.rows.length).toBeGreaterThanOrEqual(10);
|
|
expect(scripts.rows.every((r: { tags: string[] }) => r.tags.includes('script'))).toBe(true);
|
|
const badge = await list({ kind: 'lockup', query: 'postmark' });
|
|
expect(badge.rows[0]).toMatchObject({ id: 'postmark', words: expect.arrayContaining(['place', 'dates']) });
|
|
const themes = await list({ kind: 'theme', query: 'tropical' });
|
|
const tropical = themes.rows.find((r: { id: string }) => r.id === 'tropical');
|
|
expect(tropical).toMatchObject({ palette: { paper: expect.any(String) }, fonts: { display: expect.any(String) } });
|
|
const patterns = await list({ kind: 'pattern' });
|
|
expect(patterns.rows.map((r: { id: string }) => r.id)).toContain('gingham');
|
|
// A theme narrows stickers only; the other kinds pay it no heed.
|
|
expect((await list({ kind: 'pattern', theme: 'paper' })).total).toBe(patterns.total);
|
|
});
|
|
|
|
it('JBASSET-006: says what each kind lists, and that templates are list_book_layouts', () => {
|
|
const entry = getEntry(JourneyBookMcp as unknown as ClassRef, 'listAssets');
|
|
if (entry?.kind !== 'tool') throw new Error('listAssets is no tool');
|
|
const description = entry.options.description ?? '';
|
|
for (const kind of ['kind sticker', 'kind shape', 'kind font', 'kind lockup', 'kind theme', 'kind pattern']) {
|
|
expect(description).toContain(kind);
|
|
}
|
|
expect(description).toContain("Templates are list_book_layouts'");
|
|
expect(description).not.toMatch(/[\u2013\u2014]/);
|
|
});
|
|
});
|
|
|
|
describe('a sticker on a page', () => {
|
|
it('JBASSET-003: edit_book_page places a sticker the library holds, in its proportions and the colours asked for', async () => {
|
|
const { user: owner } = await makeUser(orm);
|
|
const { journey, inner } = await bookFor(owner.id);
|
|
const element = { kind: 'sticker' as const, stickerId: 'eiffelTower' };
|
|
// The look follows the add in the same call, by the id the add gave it.
|
|
const colours = { kind: 'sticker' as const, colors: ['#1d3557'] };
|
|
const ops = [
|
|
{ op: 'add' as const, element, id: 'tower' },
|
|
{ op: 'style' as const, elementId: 'tower', set: colours },
|
|
];
|
|
const written = bookWriteResultSchema.parse(
|
|
json(await mcp.editBookPage({ journeyId: journey.id, spreadId: inner, ops }, ctx(owner.id))),
|
|
);
|
|
expect(written.spread_ids).toEqual([inner]);
|
|
const page = json(await mcp.describeBookPage({ journeyId: journey.id, spreadId: inner }, ctx(owner.id)));
|
|
const sticker = page.elements.find((e: { kind: string }) => e.kind === 'sticker');
|
|
// The tower is drawn 70 by 100: on the 21 cm page it stands 63 mm tall and 44.1 mm wide.
|
|
expect(sticker?.frame).toMatchObject({ w: 44.1, h: 63 });
|
|
expect(page.text).toMatch(/sticker eiffelTower, colours #1d3557/);
|
|
});
|
|
|
|
it('JBASSET-004: a sticker the library does not hold is refused with the engine code, and nothing is written', async () => {
|
|
const { user: owner } = await makeUser(orm);
|
|
const { journey, inner } = await bookFor(owner.id);
|
|
const before = json(await mcp.getBook({ journeyId: journey.id }, ctx(owner.id))).version;
|
|
const element = { kind: 'sticker' as const, stickerId: 'unicornCastle' };
|
|
await expect(
|
|
mcp.editBookPage({ journeyId: journey.id, spreadId: inner, ops: [{ op: 'add', element }] }, ctx(owner.id)),
|
|
).rejects.toThrow(/no sticker unicornCastle/);
|
|
expect(json(await mcp.getBook({ journeyId: journey.id }, ctx(owner.id))).version).toBe(before);
|
|
});
|
|
});
|