feat: integrate image_make with MindSpace assets

This commit is contained in:
john
2026-07-19 18:34:50 +08:00
parent 2f4366e882
commit 5b88f8ece5
14 changed files with 768 additions and 14 deletions
@@ -0,0 +1,68 @@
import assert from 'node:assert/strict';
import { once } from 'node:events';
import test from 'node:test';
import express from 'express';
import { attachMindSpaceImageGenerationRoutes } from './mindspace-image-generation-routes.mjs';
async function start(service) {
const app = express();
app.use(express.json());
app.use((req, _res, next) => {
req.currentUser = { id: 'user-1' };
req.requestId = 'req-1';
next();
});
attachMindSpaceImageGenerationRoutes(app, { getService: () => service });
const server = app.listen(0, '127.0.0.1');
await once(server, 'listening');
return {
url: `http://127.0.0.1:${server.address().port}`,
async close() { server.close(); await once(server, 'close'); },
};
}
test('image generation route passes authenticated user and idempotency key to the service', async () => {
const calls = [];
const fixture = await start({
async generate(input) {
calls.push(input);
return { ok: true, asset: { id: 'asset-1', publicUrl: '/image.webp' } };
},
});
try {
const response = await fetch(`${fixture.url}/mindspace/v1/images/generate`, {
method: 'POST',
headers: { 'content-type': 'application/json', 'idempotency-key': 'imgreq-1' },
body: JSON.stringify({ purpose: 'hero', prompt: 'calm library' }),
});
assert.equal(response.status, 201);
assert.equal((await response.json()).data.asset.id, 'asset-1');
assert.deepEqual(calls, [{
userId: 'user-1',
purpose: 'hero',
prompt: 'calm library',
negativePrompt: undefined,
consumerRef: 'mindspace:req-1:hero',
idempotencyKey: 'imgreq-1',
}]);
} finally { await fixture.close(); }
});
test('image generation route exposes safe fallback without changing existing flows', async () => {
const fixture = await start({
async generate() {
return { ok: false, fallback: true, code: 'purpose_disabled', message: 'disabled' };
},
});
try {
const response = await fetch(`${fixture.url}/mindspace/v1/images/generate`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ purpose: 'feed_cover', prompt: 'calm library' }),
});
assert.equal(response.status, 409);
assert.deepEqual(await response.json(), {
ok: false, fallback: true, code: 'purpose_disabled', message: 'disabled',
});
} finally { await fixture.close(); }
});