@1aboveio/content-skills 0.0.0-stage → 0.1.0
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.
- package/README.md +24 -2
- package/package.json +33 -4
- package/skills/article-review/LICENSE +3 -0
- package/skills/article-review/SKILL.md +66 -0
- package/skills/article-review/references/report-format.md +68 -0
- package/skills/article-review/references/template-guide.md +79 -0
- package/skills/article-review/references/template-wechat-payments.md +125 -0
- package/skills/article-review/release-check.mjs +93 -0
- package/skills/article-to-image/LICENSE +3 -0
- package/skills/article-to-image/SKILL.md +91 -0
- package/skills/article-to-image/references/formats.md +53 -0
- package/skills/article-to-image/release-check.mjs +93 -0
- package/skills/flowchart-svg/LICENSE +3 -0
- package/skills/flowchart-svg/SKILL.md +43 -0
- package/skills/flowchart-svg/release-check.mjs +93 -0
- package/skills/flowchart-svg/scripts/export_png.sh +8 -0
- package/skills/image-gen/LICENSE +3 -0
- package/skills/image-gen/SKILL.md +192 -0
- package/skills/image-gen/release-check.mjs +93 -0
- package/skills/image-gen/scripts/credentials.mjs +217 -0
- package/skills/image-gen/scripts/credentials.test.mjs +212 -0
- package/skills/image-gen/scripts/generate.mjs +240 -0
- package/skills/image-gen/scripts/options.mjs +293 -0
- package/skills/image-gen/scripts/options.test.mjs +245 -0
- package/skills/mermaid/LICENSE +3 -0
- package/skills/mermaid/SKILL.md +635 -0
- package/skills/mermaid/release-check.mjs +93 -0
- package/skills/wechat-article-writer/LICENSE +3 -0
- package/skills/wechat-article-writer/SKILL.md +92 -0
- package/skills/wechat-article-writer/references/style-selector.md +133 -0
- package/skills/wechat-article-writer/references/writing-rules.md +152 -0
- package/skills/wechat-article-writer/release-check.mjs +93 -0
- package/skills/wechat-article-writer/templates/wechat-comparison.md +52 -0
- package/skills/wechat-article-writer/templates/wechat-howto.md +51 -0
- package/skills/wechat-article-writer/templates/wechat-insight.md +50 -0
- package/skills/wechat-article-writer/templates/wechat-pitfall.md +52 -0
- package/skills/wechat-publisher/LICENSE +3 -0
- package/skills/wechat-publisher/SKILL.md +171 -0
- package/skills/wechat-publisher/references/api.md +105 -0
- package/skills/wechat-publisher/references/layout-rules.md +57 -0
- package/skills/wechat-publisher/references/wechat-safe-template.html +86 -0
- package/skills/wechat-publisher/release-check.mjs +93 -0
- package/skills/wechat-publisher/scripts/md_to_wechat_html.py +194 -0
- package/skills/wechat-publisher/scripts/publish.py +239 -0
- package/skills/wechat-search/LICENSE +3 -0
- package/skills/wechat-search/SKILL.md +211 -0
- package/skills/wechat-search/release-check.mjs +93 -0
- package/skills/wechat-search/scripts/start-chrome.sh +83 -0
- package/skills/xai-search/LICENSE +3 -0
- package/skills/xai-search/SKILL.md +79 -0
- package/skills/xai-search/release-check.mjs +93 -0
- package/skills/xai-search/scripts/xai_search.sh +187 -0
- package/skills/xai-search/tests/smoke_test.sh +36 -0
|
@@ -0,0 +1,293 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Argument parsing and option mapping.
|
|
3
|
+
*
|
|
4
|
+
* These are pure functions so the awkward cases — a misspelled flag, a size the
|
|
5
|
+
* provider cannot express, a response whose bytes do not match the filename —
|
|
6
|
+
* can be tested without touching the network.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
export const OPENAI_SIZES = ['1024x1024', '1536x1024', '1024x1536', 'auto'];
|
|
10
|
+
export const QUALITIES = ['low', 'medium', 'high', 'auto'];
|
|
11
|
+
|
|
12
|
+
// Exactly the set the Gemini API reports when it rejects an invalid ratio.
|
|
13
|
+
// Verified against generativelanguage.googleapis.com rather than assumed.
|
|
14
|
+
export const GEMINI_ASPECT_RATIOS = [
|
|
15
|
+
'1:1', '1:4', '1:8', '2:3', '3:2', '3:4', '4:1',
|
|
16
|
+
'4:3', '4:5', '5:4', '8:1', '9:16', '16:9', '21:9',
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
export const FLAG_SPEC = {
|
|
20
|
+
provider: { values: ['openai', 'gemini'] },
|
|
21
|
+
model: {},
|
|
22
|
+
size: {},
|
|
23
|
+
quality: { values: QUALITIES, onlyFor: 'openai' },
|
|
24
|
+
output: {},
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const KNOWN_FLAGS = Object.keys(FLAG_SPEC);
|
|
28
|
+
|
|
29
|
+
function suggestFlag(name) {
|
|
30
|
+
// Cheap edit-distance-ish match: a typo usually shares a prefix or most letters.
|
|
31
|
+
const candidates = KNOWN_FLAGS.filter(
|
|
32
|
+
(known) => known[0] === name[0] && Math.abs(known.length - name.length) <= 2,
|
|
33
|
+
);
|
|
34
|
+
if (!candidates.length) return undefined;
|
|
35
|
+
const score = (known) => [...new Set(name)].filter((ch) => known.includes(ch)).length;
|
|
36
|
+
return candidates.sort((a, b) => score(b) - score(a))[0];
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Parse argv into a prompt plus flags.
|
|
41
|
+
*
|
|
42
|
+
* Unknown flags are errors rather than silent no-ops: a misspelled `--qualtiy`
|
|
43
|
+
* that is quietly dropped produces an image at the wrong setting and no hint of
|
|
44
|
+
* why, which is worse than refusing to run.
|
|
45
|
+
*
|
|
46
|
+
* @returns {{prompt: string, flags: object, errors: string[], warnings: string[]}}
|
|
47
|
+
*/
|
|
48
|
+
export function parseArgs(argv) {
|
|
49
|
+
const flags = {};
|
|
50
|
+
const positional = [];
|
|
51
|
+
const errors = [];
|
|
52
|
+
const warnings = [];
|
|
53
|
+
|
|
54
|
+
for (let i = 0; i < argv.length; i++) {
|
|
55
|
+
const arg = argv[i];
|
|
56
|
+
if (!arg.startsWith('--')) {
|
|
57
|
+
positional.push(arg);
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
const body = arg.slice(2);
|
|
62
|
+
const eq = body.indexOf('=');
|
|
63
|
+
const name = eq >= 0 ? body.slice(0, eq) : body;
|
|
64
|
+
let value = eq >= 0 ? body.slice(eq + 1) : undefined;
|
|
65
|
+
|
|
66
|
+
if (!Object.hasOwn(FLAG_SPEC, name)) {
|
|
67
|
+
const suggestion = suggestFlag(name);
|
|
68
|
+
errors.push(`Unknown option --${name}.${suggestion ? ` Did you mean --${suggestion}?` : ''}`);
|
|
69
|
+
if (value === undefined && argv[i + 1] && !argv[i + 1].startsWith('--')) i++;
|
|
70
|
+
continue;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (value === undefined) {
|
|
74
|
+
const next = argv[i + 1];
|
|
75
|
+
// A flag swallowing the next flag as its value is how `--quality --output x`
|
|
76
|
+
// silently loses the output path, so treat it as missing instead.
|
|
77
|
+
if (next === undefined || next.startsWith('--')) {
|
|
78
|
+
errors.push(`Option --${name} needs a value.`);
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
value = next;
|
|
82
|
+
i++;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const allowed = FLAG_SPEC[name].values;
|
|
86
|
+
if (allowed && !allowed.includes(value)) {
|
|
87
|
+
errors.push(`Invalid --${name} "${value}". Valid options: ${allowed.join(', ')}`);
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
flags[name] = value;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const provider = flags.provider || 'openai';
|
|
95
|
+
for (const [name, spec] of Object.entries(FLAG_SPEC)) {
|
|
96
|
+
if (spec.onlyFor && flags[name] !== undefined && spec.onlyFor !== provider) {
|
|
97
|
+
warnings.push(`--${name} applies to ${spec.onlyFor} only; ignoring it for ${provider}.`);
|
|
98
|
+
delete flags[name];
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return { prompt: positional.join(' ').trim(), flags, errors, warnings };
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Validate a size for the OpenAI image endpoint, which takes literal pixel
|
|
107
|
+
* dimensions.
|
|
108
|
+
*/
|
|
109
|
+
export function openAiSize(size) {
|
|
110
|
+
if (!size) return { value: '1024x1024' };
|
|
111
|
+
if (OPENAI_SIZES.includes(size)) return { value: size };
|
|
112
|
+
return { error: `Invalid size "${size}". Valid options: ${OPENAI_SIZES.join(', ')}` };
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Map a `--size` to a Gemini aspect ratio.
|
|
117
|
+
*
|
|
118
|
+
* Gemini has no pixel-dimension parameter — it takes a ratio — so a WxH size is
|
|
119
|
+
* snapped to the closest ratio it supports rather than rejected. Without this the
|
|
120
|
+
* flag was accepted and silently discarded, and every Gemini image came back
|
|
121
|
+
* square no matter what was asked for.
|
|
122
|
+
*
|
|
123
|
+
* @returns {{value?: string, error?: string, snappedFrom?: string}}
|
|
124
|
+
* `value` undefined means "send no ratio and let the model default".
|
|
125
|
+
*/
|
|
126
|
+
export function geminiAspectRatio(size) {
|
|
127
|
+
if (!size || size === 'auto') return {};
|
|
128
|
+
if (GEMINI_ASPECT_RATIOS.includes(size)) return { value: size };
|
|
129
|
+
|
|
130
|
+
const ratioMatch = /^(\d+):(\d+)$/.exec(size);
|
|
131
|
+
if (ratioMatch) {
|
|
132
|
+
return {
|
|
133
|
+
error:
|
|
134
|
+
`Gemini does not support the aspect ratio "${size}". ` +
|
|
135
|
+
`Valid ratios: ${GEMINI_ASPECT_RATIOS.join(', ')}`,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const pixelMatch = /^(\d+)x(\d+)$/i.exec(size);
|
|
140
|
+
if (!pixelMatch) {
|
|
141
|
+
return {
|
|
142
|
+
error:
|
|
143
|
+
`Invalid size "${size}". Use WxH (e.g. 1536x1024) or an aspect ratio ` +
|
|
144
|
+
`(${GEMINI_ASPECT_RATIOS.join(', ')}).`,
|
|
145
|
+
};
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
const width = Number(pixelMatch[1]);
|
|
149
|
+
const height = Number(pixelMatch[2]);
|
|
150
|
+
if (!width || !height) return { error: `Invalid size "${size}": zero dimension.` };
|
|
151
|
+
|
|
152
|
+
// Compare in log space so 2:1 and 1:2 are equally far from 1:1.
|
|
153
|
+
const target = Math.log(width / height);
|
|
154
|
+
let best;
|
|
155
|
+
for (const ratio of GEMINI_ASPECT_RATIOS) {
|
|
156
|
+
const [w, h] = ratio.split(':').map(Number);
|
|
157
|
+
const distance = Math.abs(Math.log(w / h) - target);
|
|
158
|
+
if (!best || distance < best.distance) best = { ratio, distance };
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
return best.ratio === '1:1' && best.distance === 0
|
|
162
|
+
? { value: '1:1' }
|
|
163
|
+
: { value: best.ratio, snappedFrom: size };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Read pixel dimensions out of the returned bytes.
|
|
168
|
+
*
|
|
169
|
+
* Providers and proxies do not always honor the requested size — a gateway was
|
|
170
|
+
* observed returning 1402x1122 for a 1024x1536 request — and a silent mismatch
|
|
171
|
+
* is invisible until someone opens the file. Reading the header costs nothing and
|
|
172
|
+
* turns that into a printed fact.
|
|
173
|
+
*
|
|
174
|
+
* @returns {{width: number, height: number} | null} null when the format is not
|
|
175
|
+
* one this recognizes, which is a reason to stay quiet rather than to warn.
|
|
176
|
+
*/
|
|
177
|
+
export function imageDimensions(buffer) {
|
|
178
|
+
if (!buffer || buffer.length < 24) return null;
|
|
179
|
+
|
|
180
|
+
if (buffer.readUInt32BE(0) === 0x89504e47) {
|
|
181
|
+
return { width: buffer.readUInt32BE(16), height: buffer.readUInt32BE(20) };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
if (buffer[0] === 0xff && buffer[1] === 0xd8) {
|
|
185
|
+
let offset = 2;
|
|
186
|
+
while (offset + 9 < buffer.length) {
|
|
187
|
+
if (buffer[offset] !== 0xff) {
|
|
188
|
+
offset += 1;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
const marker = buffer[offset + 1];
|
|
192
|
+
const isStartOfFrame =
|
|
193
|
+
(marker >= 0xc0 && marker <= 0xc3) ||
|
|
194
|
+
(marker >= 0xc5 && marker <= 0xc7) ||
|
|
195
|
+
(marker >= 0xc9 && marker <= 0xcb) ||
|
|
196
|
+
(marker >= 0xcd && marker <= 0xcf);
|
|
197
|
+
if (isStartOfFrame) {
|
|
198
|
+
return { width: buffer.readUInt16BE(offset + 7), height: buffer.readUInt16BE(offset + 5) };
|
|
199
|
+
}
|
|
200
|
+
if (marker === 0xd8 || marker === 0x01 || (marker >= 0xd0 && marker <= 0xd7)) {
|
|
201
|
+
offset += 2;
|
|
202
|
+
continue;
|
|
203
|
+
}
|
|
204
|
+
offset += 2 + buffer.readUInt16BE(offset + 2);
|
|
205
|
+
}
|
|
206
|
+
return null;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (buffer.length >= 30 && buffer.toString('ascii', 0, 4) === 'RIFF' && buffer.toString('ascii', 8, 12) === 'WEBP') {
|
|
210
|
+
const chunk = buffer.toString('ascii', 12, 16);
|
|
211
|
+
if (chunk === 'VP8X') {
|
|
212
|
+
return {
|
|
213
|
+
width: buffer.readUIntLE(24, 3) + 1,
|
|
214
|
+
height: buffer.readUIntLE(27, 3) + 1,
|
|
215
|
+
};
|
|
216
|
+
}
|
|
217
|
+
if (chunk === 'VP8 ') {
|
|
218
|
+
return {
|
|
219
|
+
width: buffer.readUInt16LE(26) & 0x3fff,
|
|
220
|
+
height: buffer.readUInt16LE(28) & 0x3fff,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
return null;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Compare what was asked for against what arrived.
|
|
230
|
+
*
|
|
231
|
+
* @returns {string | undefined} a warning when they disagree meaningfully.
|
|
232
|
+
*/
|
|
233
|
+
export function describeSizeMismatch({ requestedSize, aspectRatio, actual }) {
|
|
234
|
+
if (!actual) return undefined;
|
|
235
|
+
|
|
236
|
+
const pixels = requestedSize && /^(\d+)x(\d+)$/i.exec(requestedSize);
|
|
237
|
+
if (pixels) {
|
|
238
|
+
const [width, height] = [Number(pixels[1]), Number(pixels[2])];
|
|
239
|
+
if (width !== actual.width || height !== actual.height) {
|
|
240
|
+
return `Requested ${width}x${height} but the provider returned ${actual.width}x${actual.height}.`;
|
|
241
|
+
}
|
|
242
|
+
return undefined;
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (aspectRatio) {
|
|
246
|
+
const [w, h] = aspectRatio.split(':').map(Number);
|
|
247
|
+
const wanted = w / h;
|
|
248
|
+
const got = actual.width / actual.height;
|
|
249
|
+
// 2% covers rounding to whole pixels without hiding a real substitution.
|
|
250
|
+
if (Math.abs(Math.log(got / wanted)) > 0.02) {
|
|
251
|
+
return `Requested aspect ratio ${aspectRatio} but the provider returned ${actual.width}x${actual.height}.`;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
return undefined;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
const MIME_EXTENSIONS = {
|
|
259
|
+
'image/png': '.png',
|
|
260
|
+
'image/jpeg': '.jpg',
|
|
261
|
+
'image/jpg': '.jpg',
|
|
262
|
+
'image/webp': '.webp',
|
|
263
|
+
'image/gif': '.gif',
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/** The file extension matching the bytes a provider actually returned. */
|
|
267
|
+
export function extensionForMimeType(mimeType) {
|
|
268
|
+
if (!mimeType) return '.png';
|
|
269
|
+
return MIME_EXTENSIONS[mimeType.split(';')[0].trim().toLowerCase()] ?? '.png';
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Decide where to write, given what the provider returned.
|
|
274
|
+
*
|
|
275
|
+
* An explicit --output is always honored — overriding the user's chosen path
|
|
276
|
+
* would be worse than a wrong extension — but a mismatch is worth saying out
|
|
277
|
+
* loud, since a JPEG named .png breaks some viewers and upload validators.
|
|
278
|
+
*
|
|
279
|
+
* @returns {{path: string, warning?: string}}
|
|
280
|
+
*/
|
|
281
|
+
export function resolveOutputPath({ requested, defaultBase, mimeType }) {
|
|
282
|
+
const extension = extensionForMimeType(mimeType);
|
|
283
|
+
if (!requested) return { path: `${defaultBase}${extension}` };
|
|
284
|
+
|
|
285
|
+
const currentExtension = /\.[A-Za-z0-9]+$/.exec(requested)?.[0]?.toLowerCase();
|
|
286
|
+
if (currentExtension && currentExtension !== extension) {
|
|
287
|
+
return {
|
|
288
|
+
path: requested,
|
|
289
|
+
warning: `Saved ${mimeType} bytes to ${requested}; the extension says ${currentExtension}.`,
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
return { path: currentExtension ? requested : `${requested}${extension}` };
|
|
293
|
+
}
|
|
@@ -0,0 +1,245 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import { describe, it } from 'node:test';
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
GEMINI_ASPECT_RATIOS,
|
|
6
|
+
describeSizeMismatch,
|
|
7
|
+
extensionForMimeType,
|
|
8
|
+
imageDimensions,
|
|
9
|
+
geminiAspectRatio,
|
|
10
|
+
openAiSize,
|
|
11
|
+
parseArgs,
|
|
12
|
+
resolveOutputPath,
|
|
13
|
+
} from './options.mjs';
|
|
14
|
+
|
|
15
|
+
describe('parseArgs', () => {
|
|
16
|
+
it('joins the positional words into the prompt', () => {
|
|
17
|
+
const { prompt, errors } = parseArgs(['a', 'red', 'fox']);
|
|
18
|
+
|
|
19
|
+
assert.equal(prompt, 'a red fox');
|
|
20
|
+
assert.deepEqual(errors, []);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('accepts --flag value and --flag=value alike', () => {
|
|
24
|
+
assert.equal(parseArgs(['p', '--model', 'm']).flags.model, 'm');
|
|
25
|
+
assert.equal(parseArgs(['p', '--model=m']).flags.model, 'm');
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('rejects an unknown flag instead of ignoring it', () => {
|
|
29
|
+
// The old parser accepted --qualtiy silently and generated at the default.
|
|
30
|
+
const { errors } = parseArgs(['p', '--qualtiy', 'high']);
|
|
31
|
+
|
|
32
|
+
assert.equal(errors.length, 1);
|
|
33
|
+
assert.match(errors[0], /Unknown option --qualtiy/);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it('suggests the intended flag for a near miss', () => {
|
|
37
|
+
assert.match(parseArgs(['p', '--outpt', 'x.png']).errors[0], /Did you mean --output\?/);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
it('does not let a flag swallow the next flag as its value', () => {
|
|
41
|
+
// `--quality --output x.png` used to set quality="--output" and lose the path.
|
|
42
|
+
const { flags, errors } = parseArgs(['p', '--quality', '--output', 'x.png']);
|
|
43
|
+
|
|
44
|
+
assert.match(errors[0], /--quality needs a value/);
|
|
45
|
+
assert.equal(flags.output, 'x.png');
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('reports a trailing flag with no value', () => {
|
|
49
|
+
assert.match(parseArgs(['p', '--output']).errors[0], /--output needs a value/);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('validates enumerated flag values', () => {
|
|
53
|
+
assert.match(parseArgs(['p', '--provider', 'midjourney']).errors[0], /Invalid --provider/);
|
|
54
|
+
assert.match(parseArgs(['p', '--quality', 'ultra']).errors[0], /Invalid --quality/);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('warns and drops a flag that does not apply to the chosen provider', () => {
|
|
58
|
+
const { flags, warnings } = parseArgs(['p', '--provider', 'gemini', '--quality', 'high']);
|
|
59
|
+
|
|
60
|
+
assert.equal(flags.quality, undefined);
|
|
61
|
+
assert.match(warnings[0], /--quality applies to openai only/);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it('keeps provider-specific flags when the provider does match', () => {
|
|
65
|
+
assert.equal(parseArgs(['p', '--quality', 'high']).flags.quality, 'high');
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
describe('openAiSize', () => {
|
|
70
|
+
it('defaults to a square', () => {
|
|
71
|
+
assert.equal(openAiSize(undefined).value, '1024x1024');
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('accepts the documented sizes and rejects others', () => {
|
|
75
|
+
assert.equal(openAiSize('1536x1024').value, '1536x1024');
|
|
76
|
+
assert.match(openAiSize('800x600').error, /Invalid size/);
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('geminiAspectRatio', () => {
|
|
81
|
+
it('sends nothing when no size is given', () => {
|
|
82
|
+
assert.equal(geminiAspectRatio(undefined).value, undefined);
|
|
83
|
+
assert.equal(geminiAspectRatio('auto').value, undefined);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it('passes a supported ratio straight through', () => {
|
|
87
|
+
assert.equal(geminiAspectRatio('16:9').value, '16:9');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
it('snaps pixel dimensions to the nearest supported ratio', () => {
|
|
91
|
+
// This is the whole point: --size used to be discarded for Gemini.
|
|
92
|
+
assert.equal(geminiAspectRatio('1536x1024').value, '3:2');
|
|
93
|
+
assert.equal(geminiAspectRatio('1024x1536').value, '2:3');
|
|
94
|
+
assert.equal(geminiAspectRatio('1920x1080').value, '16:9');
|
|
95
|
+
assert.equal(geminiAspectRatio('1024x1024').value, '1:1');
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('reports what it snapped away from, so the change is visible', () => {
|
|
99
|
+
const snapped = geminiAspectRatio('1000x500');
|
|
100
|
+
|
|
101
|
+
// 2:1 is not supported; 16:9 is the closest thing to it that is.
|
|
102
|
+
assert.equal(snapped.value, '16:9');
|
|
103
|
+
assert.equal(snapped.snappedFrom, '1000x500');
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
it('treats an exact square as exact, not snapped', () => {
|
|
107
|
+
assert.equal(geminiAspectRatio('1024x1024').snappedFrom, undefined);
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it('rejects a well-formed ratio the API does not support', () => {
|
|
111
|
+
const rejected = geminiAspectRatio('7:5');
|
|
112
|
+
|
|
113
|
+
assert.match(rejected.error, /does not support the aspect ratio "7:5"/);
|
|
114
|
+
assert.match(rejected.error, /16:9/);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('rejects unparseable sizes and zero dimensions', () => {
|
|
118
|
+
assert.match(geminiAspectRatio('huge').error, /Invalid size/);
|
|
119
|
+
assert.match(geminiAspectRatio('0x100').error, /zero dimension/);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('only ever emits ratios the API accepts', () => {
|
|
123
|
+
for (const size of ['1536x1024', '1024x1536', '1920x1080', '100x800', '800x100']) {
|
|
124
|
+
assert.ok(GEMINI_ASPECT_RATIOS.includes(geminiAspectRatio(size).value), size);
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
describe('extensionForMimeType', () => {
|
|
130
|
+
it('maps the image types a provider may return', () => {
|
|
131
|
+
assert.equal(extensionForMimeType('image/png'), '.png');
|
|
132
|
+
assert.equal(extensionForMimeType('image/jpeg'), '.jpg');
|
|
133
|
+
assert.equal(extensionForMimeType('image/webp'), '.webp');
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('ignores charset parameters and casing', () => {
|
|
137
|
+
assert.equal(extensionForMimeType('IMAGE/JPEG; charset=binary'), '.jpg');
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('falls back to .png for anything unrecognized', () => {
|
|
141
|
+
assert.equal(extensionForMimeType(undefined), '.png');
|
|
142
|
+
assert.equal(extensionForMimeType('application/octet-stream'), '.png');
|
|
143
|
+
});
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
describe('resolveOutputPath', () => {
|
|
147
|
+
it('names the default file after the bytes actually returned', () => {
|
|
148
|
+
const resolved = resolveOutputPath({ defaultBase: './generated-1', mimeType: 'image/jpeg' });
|
|
149
|
+
|
|
150
|
+
assert.equal(resolved.path, './generated-1.jpg');
|
|
151
|
+
assert.equal(resolved.warning, undefined);
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('honors an explicit path but flags an extension mismatch', () => {
|
|
155
|
+
const resolved = resolveOutputPath({ requested: 'logo.png', mimeType: 'image/webp' });
|
|
156
|
+
|
|
157
|
+
assert.equal(resolved.path, 'logo.png');
|
|
158
|
+
assert.match(resolved.warning, /image\/webp/);
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it('stays quiet when the extension already matches', () => {
|
|
162
|
+
assert.equal(resolveOutputPath({ requested: 'logo.png', mimeType: 'image/png' }).warning, undefined);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('appends an extension when the requested path has none', () => {
|
|
166
|
+
assert.equal(resolveOutputPath({ requested: 'logo', mimeType: 'image/png' }).path, 'logo.png');
|
|
167
|
+
});
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
describe('imageDimensions', () => {
|
|
171
|
+
const png = (w, h) => {
|
|
172
|
+
const b = Buffer.alloc(24);
|
|
173
|
+
b.writeUInt32BE(0x89504e47, 0);
|
|
174
|
+
b.writeUInt32BE(w, 16);
|
|
175
|
+
b.writeUInt32BE(h, 20);
|
|
176
|
+
return b;
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
it('reads PNG dimensions', () => {
|
|
180
|
+
assert.deepEqual(imageDimensions(png(1536, 1024)), { width: 1536, height: 1024 });
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
it('reads JPEG dimensions from the start-of-frame marker', () => {
|
|
184
|
+
const b = Buffer.alloc(32, 0);
|
|
185
|
+
b[0] = 0xff; b[1] = 0xd8; // SOI
|
|
186
|
+
b[2] = 0xff; b[3] = 0xe0; // APP0, skipped via its length
|
|
187
|
+
b.writeUInt16BE(6, 4);
|
|
188
|
+
b[10] = 0xff; b[11] = 0xc0; // SOF0
|
|
189
|
+
b.writeUInt16BE(17, 12);
|
|
190
|
+
b.writeUInt16BE(480, 15); // height
|
|
191
|
+
b.writeUInt16BE(640, 17); // width
|
|
192
|
+
|
|
193
|
+
assert.deepEqual(imageDimensions(b), { width: 640, height: 480 });
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it('reads WebP VP8X canvas size', () => {
|
|
197
|
+
const b = Buffer.alloc(32, 0);
|
|
198
|
+
b.write('RIFF', 0, 'ascii');
|
|
199
|
+
b.write('WEBP', 8, 'ascii');
|
|
200
|
+
b.write('VP8X', 12, 'ascii');
|
|
201
|
+
b.writeUIntLE(1023, 24, 3); // stored as width - 1
|
|
202
|
+
b.writeUIntLE(767, 27, 3);
|
|
203
|
+
|
|
204
|
+
assert.deepEqual(imageDimensions(b), { width: 1024, height: 768 });
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('returns null for formats it cannot parse, rather than guessing', () => {
|
|
208
|
+
assert.equal(imageDimensions(Buffer.alloc(4)), null);
|
|
209
|
+
assert.equal(imageDimensions(Buffer.alloc(64, 7)), null);
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe('describeSizeMismatch', () => {
|
|
214
|
+
it('reports a pixel-size substitution', () => {
|
|
215
|
+
// The observed case: 1024x1536 requested, 1402x1122 returned.
|
|
216
|
+
const warning = describeSizeMismatch({
|
|
217
|
+
requestedSize: '1024x1536',
|
|
218
|
+
actual: { width: 1402, height: 1122 },
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
assert.match(warning, /Requested 1024x1536 but the provider returned 1402x1122/);
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
it('stays quiet when the size matches', () => {
|
|
225
|
+
assert.equal(
|
|
226
|
+
describeSizeMismatch({ requestedSize: '1536x1024', actual: { width: 1536, height: 1024 } }),
|
|
227
|
+
undefined,
|
|
228
|
+
);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it('reports an aspect-ratio substitution within a rounding tolerance', () => {
|
|
232
|
+
assert.equal(
|
|
233
|
+
describeSizeMismatch({ aspectRatio: '16:9', actual: { width: 1920, height: 1080 } }),
|
|
234
|
+
undefined,
|
|
235
|
+
);
|
|
236
|
+
assert.match(
|
|
237
|
+
describeSizeMismatch({ aspectRatio: '16:9', actual: { width: 1024, height: 1024 } }),
|
|
238
|
+
/Requested aspect ratio 16:9/,
|
|
239
|
+
);
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
it('says nothing when dimensions could not be read', () => {
|
|
243
|
+
assert.equal(describeSizeMismatch({ requestedSize: '1024x1024', actual: null }), undefined);
|
|
244
|
+
});
|
|
245
|
+
});
|