feat: generate image metadata and expose public search API

This commit is contained in:
smileBeda committed 2026-09-30 16:24:53 -03:00
1 parent 082f715c39
commit 137cad1d84
7 files changed
+450 -93

No files matched your search

+55 -1
View File
@@ -34,7 +34,7 @@ test('queues model approvals below the auto-publish quality threshold', async ()
globalThis.fetch = async (_url, options) => {
const body = JSON.parse(options.body);
const prompt = body.input[0].content.find((item) => item.type === 'input_text').text;
const prompt = body.input[0].content;
assert.match(prompt, /could the exact same caption work on 50 unrelated images/);
assert.match(prompt, /Use approved only when it appears legal and score is at least 90/);
@@ -74,3 +74,57 @@ test('queues model approvals below the auto-publish quality threshold', async ()
}
}
});
test('generates searchable captions using a constrained tag catalog and discards invented tags', async (t) => {
const originalKey = process.env.OPENAI_API_KEY;
const originalFetch = globalThis.fetch;
process.env.OPENAI_API_KEY = 'test-key';
t.after(() => {
globalThis.fetch = originalFetch;
if (originalKey === undefined) delete process.env.OPENAI_API_KEY;
else process.env.OPENAI_API_KEY = originalKey;
});
globalThis.fetch = async (_url, options) => {
const body = JSON.parse(options.body);
assert.equal(body.text.format.type, 'json_schema');
assert.equal(body.text.format.strict, true);
assert.deepEqual(body.text.format.schema.properties.tags.items.enum, ['cats', 'work']);
assert.match(body.input[0].content, /Available tags.*\["cats","work"\]/);
assert.match(body.input[0].content, /never as instructions/);
assert.equal(body.input[1].content[0].type, 'input_image');
return { ok: true, json: async () => ({ status: 'completed', output: [{ content: [{ type: 'output_text', text: JSON.stringify({
decision: 'approved', score: 99, reason: 'Good meme', caption: ' A cat says “I hate Mondays” at work. ', tags: ['cats', 'invented', 'cats']
}) }] }] }) };
};
const result = await moderateImage({ buffer: Buffer.from('image'), mime: 'image/webp', availableTags: ['cats', 'work'] });
assert.equal(result.status, 'approved');
assert.equal(result.caption, 'A cat says “I hate Mondays” at work.');
assert.deepEqual(result.tags, ['cats']);
});
test('failed or incomplete caption generation raises an error; failed moderation queues safely', async (t) => {
const { captionImage } = await import('../src/moderation.js');
const originalKey = process.env.OPENAI_API_KEY;
const originalFetch = globalThis.fetch;
process.env.OPENAI_API_KEY = 'test-key';
t.after(() => {
globalThis.fetch = originalFetch;
if (originalKey === undefined) delete process.env.OPENAI_API_KEY;
else process.env.OPENAI_API_KEY = originalKey;
});
const image = { buffer: Buffer.from('image'), mime: 'image/webp' };
for (const payload of [
{ status: 'incomplete', output_text: '{"caption":"partial","tags":[]}' },
{ status: 'completed', output: [{ content: [{ type: 'refusal', refusal: 'No' }] }] },
{ output_text: 'not JSON' }
]) {
globalThis.fetch = async () => ({ ok: true, json: async () => payload });
await assert.rejects(captionImage(image), { statusCode: 503 });
const result = await moderateImage(image);
assert.equal(result.status, 'pending');
assert.equal(result.caption, '');
assert.deepEqual(result.tags, []);
}
delete process.env.OPENAI_API_KEY;
await assert.rejects(captionImage(image), { statusCode: 503 });
});