@heyramzi/utils 0.0.0-stage → 1.12.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/LICENSE +21 -0
- package/README.md +112 -2
- package/package.json +103 -4
- package/src/ai/image-agnes.test.ts +130 -0
- package/src/ai/image.ts +435 -0
- package/src/ai/models.test.ts +98 -0
- package/src/ai/models.ts +450 -0
- package/src/analytics/client.test.ts +149 -0
- package/src/analytics/client.ts +129 -0
- package/src/analytics/server.test.ts +165 -0
- package/src/analytics/server.ts +88 -0
- package/src/analytics/svelte.test.ts +105 -0
- package/src/analytics/svelte.ts +58 -0
- package/src/analytics/types.ts +26 -0
- package/src/analytics/umami/protocol.test.ts +18 -0
- package/src/analytics/umami/protocol.ts +104 -0
- package/src/analytics/umami/reporting.test.ts +391 -0
- package/src/analytics/umami/reporting.ts +627 -0
- package/src/auth/config/otp.ts +8 -0
- package/src/auth/config/rate-limits.ts +11 -0
- package/src/auth/config/trusted-origins.ts +32 -0
- package/src/auth/email/index.ts +7 -0
- package/src/auth/email/layout.test.ts +73 -0
- package/src/auth/email/layout.ts +122 -0
- package/src/auth/email/resend.test.ts +35 -0
- package/src/auth/email/resend.ts +26 -0
- package/src/auth/email/template.ts +77 -0
- package/src/auth/email/types.ts +74 -0
- package/src/auth/index.ts +5 -0
- package/src/cache.test.ts +67 -0
- package/src/cache.ts +5 -0
- package/src/cn.test.ts +52 -0
- package/src/cn.ts +16 -0
- package/src/collection.test.ts +66 -0
- package/src/collection.ts +8 -0
- package/src/crypto.test.ts +86 -0
- package/src/crypto.ts +157 -0
- package/src/error.test.ts +79 -0
- package/src/error.ts +16 -0
- package/src/errors/sentry/index.ts +20 -0
- package/src/errors/sentry/protocol.test.ts +85 -0
- package/src/errors/sentry/protocol.ts +144 -0
- package/src/errors/sentry/sveltekit.test.ts +88 -0
- package/src/errors/sentry/sveltekit.ts +188 -0
- package/src/format.test.ts +225 -0
- package/src/format.ts +103 -0
- package/src/grab.d.ts +2 -0
- package/src/grab.js +112 -0
- package/src/index.ts +38 -0
- package/src/logger-types.ts +14 -0
- package/src/logger.ts +24 -0
- package/src/math.test.ts +41 -0
- package/src/math.ts +3 -0
- package/src/og/fonts.ts +69 -0
- package/src/og/index.ts +5 -0
- package/src/og/markup.test.ts +73 -0
- package/src/og/markup.ts +90 -0
- package/src/og/renderer.ts +42 -0
- package/src/og/types.ts +26 -0
- package/src/og/wawoff2.d.ts +6 -0
- package/src/package-imports.test.mjs +22 -0
- package/src/result.test.ts +149 -0
- package/src/result.ts +29 -0
- package/src/string.test.ts +205 -0
- package/src/string.ts +71 -0
- package/src/testimonials/render-content.ts +21 -0
- package/src/testimonials/server.ts +47 -0
- package/src/testimonials/types.ts +35 -0
- package/src/timing.test.ts +166 -0
- package/src/timing.ts +28 -0
- package/src/vite.d.ts +12 -0
- package/src/vite.js +265 -0
- package/src/vite.test.js +235 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ramzi
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,113 @@
|
|
|
1
|
-
|
|
1
|
+
> [!IMPORTANT]
|
|
2
|
+
> This submodule is originally created and maintained in `Studio/vibe-kit/modules`. Changes should be made and pushed from there. We are the creators and maintainers of this submodule.
|
|
2
3
|
|
|
3
|
-
|
|
4
|
+
# @heyramzi/utils
|
|
5
|
+
|
|
6
|
+
Shared utility functions used across all workspace projects.
|
|
7
|
+
|
|
8
|
+
## Install
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
pnpm add @heyramzi/utils
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
In the workspace, use `"@heyramzi/utils": "workspace:*"` in your `package.json`.
|
|
15
|
+
|
|
16
|
+
## Exports
|
|
17
|
+
|
|
18
|
+
### Main entry (`.`)
|
|
19
|
+
|
|
20
|
+
All utilities re-exported from a single barrel:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
import { cn, formatDate, debounce, groupBy } from "@heyramzi/utils";
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
### Subpath exports
|
|
27
|
+
|
|
28
|
+
Some modules are also a direct import for tree-shaking. The rest come from the barrel only.
|
|
29
|
+
|
|
30
|
+
| Subpath | Exports |
|
|
31
|
+
| --- | --- |
|
|
32
|
+
| `@heyramzi/utils/cn` | `cn`, `WithElementRef`, `WithoutChild`, `WithoutChildren`, `WithoutChildrenOrChild` |
|
|
33
|
+
| `@heyramzi/utils/format` | `formatDate`, `formatCurrency`, `formatAmount`, `formatMillisecondsToHours`, `formatRelativeTime` |
|
|
34
|
+
| `@heyramzi/utils/string` | `slugify`, `generateSlug`, `buildSearchParams`, `parseJSON`, `parseCommaSeparatedList`, `matchesQuery` |
|
|
35
|
+
| `@heyramzi/utils/timing` | `debounce`, `throttle`, `sleep` |
|
|
36
|
+
| `@heyramzi/utils/math` | `clamp` |
|
|
37
|
+
| `@heyramzi/utils/collection` | `groupBy` |
|
|
38
|
+
| `@heyramzi/utils/result` | `Ok`, `Err`, `tryCatch`, `tryCatchAsync`, `Result` |
|
|
39
|
+
| `@heyramzi/utils/crypto` | `encryptSecret`, `decryptSecret`, `encryptSecretWithKey`, `decryptSecretWithKey`, `packSecret`, `unpackSecret`, `SecretCiphertext` |
|
|
40
|
+
| `@heyramzi/utils/vite` | `packageImportsOverKitAlias`, `reclaimPort` |
|
|
41
|
+
| `@heyramzi/utils/grab` | Side effect only: `if (dev) import("@heyramzi/utils/grab")`, then Alt+Click copies an element's HTML and Svelte component stack |
|
|
42
|
+
|
|
43
|
+
Barrel only: `fetchApi`, `isCacheExpired`, `setLogger`, `log`.
|
|
44
|
+
|
|
45
|
+
## Usage in projects
|
|
46
|
+
|
|
47
|
+
Projects re-export what they need from their own `src/lib/utils.ts`:
|
|
48
|
+
|
|
49
|
+
```ts
|
|
50
|
+
// src/lib/utils.ts
|
|
51
|
+
export { cn, formatDate, formatRelativeTime, debounce } from "@heyramzi/utils";
|
|
52
|
+
export type { WithoutChild, WithoutChildren, WithElementRef } from "@heyramzi/utils";
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This keeps the public API stable per-project while sourcing from a single implementation.
|
|
56
|
+
|
|
57
|
+
## Logger
|
|
58
|
+
|
|
59
|
+
`log()` does nothing until an app calls `setLogger()` once at startup with an implementation of the `Logger` interface. It never throws.
|
|
60
|
+
|
|
61
|
+
## Secret Encryption
|
|
62
|
+
|
|
63
|
+
`@heyramzi/utils/crypto` exists for reversible encryption of OAuth tokens and API keys at rest in app backends. It does not store anything for you and it does not implement provider-specific auth flows.
|
|
64
|
+
|
|
65
|
+
Import it as a server-only subpath:
|
|
66
|
+
|
|
67
|
+
```ts
|
|
68
|
+
import { decryptSecret, encryptSecret } from "@heyramzi/utils/crypto";
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
It uses AES-256-GCM with:
|
|
72
|
+
|
|
73
|
+
- a 32-byte master key from `HEYRAMZI_UTILS_CRYPTO_KEY`
|
|
74
|
+
- a fresh random 12-byte IV for every encryption
|
|
75
|
+
- base64 output for both `ciphertext` and `iv`
|
|
76
|
+
|
|
77
|
+
`ciphertext` includes the GCM auth tag, so consumers only need to persist `ciphertext` and `iv`. Use this helper only when the secret must be recovered later. It is not for password storage or hashing.
|
|
78
|
+
|
|
79
|
+
If an app already has its own env var name or stores encrypted secrets in a single database field, use the additive helpers:
|
|
80
|
+
|
|
81
|
+
- `encryptSecretWithKey` / `decryptSecretWithKey` to pass a raw 32-byte key directly
|
|
82
|
+
- `packSecret` / `unpackSecret` to bridge `{ ciphertext, iv }` with a single base64 payload
|
|
83
|
+
|
|
84
|
+
## Dependencies
|
|
85
|
+
|
|
86
|
+
- `clsx` + `tailwind-merge` (for `cn`)
|
|
87
|
+
- `vite` (optional peer, only for `@heyramzi/utils/vite`)
|
|
88
|
+
|
|
89
|
+
## Tooling scripts
|
|
90
|
+
|
|
91
|
+
This package hosts two code-intelligence CLIs as devDependencies, invoked from the utils package so they have a stable home. They are not part of the runtime library surface.
|
|
92
|
+
|
|
93
|
+
| Script | Command | What it does |
|
|
94
|
+
| -------------- | --------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
95
|
+
| `pnpm fallow` | [`fallow`][fallow] | Static analysis for TypeScript/JavaScript: unused files/exports/deps, circular references, copy-pasted blocks, complexity hotspots, architecture boundary drift. |
|
|
96
|
+
| `pnpm shannon` | [`@keygraph/shannon`][shan] | Autonomous AI pentester for web apps and APIs. Reads a source checkout, runs exploits against a live URL, reports only verified vulnerabilities. Requires Docker and Node 18+. |
|
|
97
|
+
|
|
98
|
+
Run from `packages/utils`:
|
|
99
|
+
|
|
100
|
+
```bash
|
|
101
|
+
pnpm fallow # summary across the codebase
|
|
102
|
+
pnpm fallow dead-code # just unused code
|
|
103
|
+
pnpm fallow audit --format json
|
|
104
|
+
pnpm shannon setup
|
|
105
|
+
pnpm shannon start -u https://example.com -r /path/to/repo
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
[fallow]: https://github.com/fallow-rs/fallow
|
|
109
|
+
[shan]: https://github.com/KeygraphHQ/shannon
|
|
110
|
+
|
|
111
|
+
## Removed
|
|
112
|
+
|
|
113
|
+
The `error-reporter`, `image` and `sveltekit` modules are gone, along with `getErrorMessage`, `CACHE_TTL`, `getCacheExpiry`, `getLogger` and `formatTime`. Nothing imported them, and the `sharp` and SvelteKit peers went with them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,105 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@heyramzi/utils",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.12.0",
|
|
4
|
+
"description": "Shared TypeScript decisions for every Studio app: cn, result, formatting, crypto, auth defaults, email, Sentry errors, Umami analytics, OG images, testimonials and the AI model table.",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/heyramzi/vibe-kit.git",
|
|
8
|
+
"directory": "packages/utils"
|
|
9
|
+
},
|
|
10
|
+
"files": [
|
|
11
|
+
"src",
|
|
12
|
+
"package.json"
|
|
13
|
+
],
|
|
14
|
+
"type": "module",
|
|
15
|
+
"imports": {
|
|
16
|
+
"#src/vite.js": "./src/vite.js",
|
|
17
|
+
"#src/*.js": "./src/*.ts",
|
|
18
|
+
"#src/*": "./src/*"
|
|
19
|
+
},
|
|
20
|
+
"exports": {
|
|
21
|
+
".": "./src/index.ts",
|
|
22
|
+
"./cn": "./src/cn.ts",
|
|
23
|
+
"./format": "./src/format.ts",
|
|
24
|
+
"./string": "./src/string.ts",
|
|
25
|
+
"./result": "./src/result.ts",
|
|
26
|
+
"./timing": "./src/timing.ts",
|
|
27
|
+
"./math": "./src/math.ts",
|
|
28
|
+
"./collection": "./src/collection.ts",
|
|
29
|
+
"./vite": {
|
|
30
|
+
"types": "./src/vite.d.ts",
|
|
31
|
+
"default": "./src/vite.js"
|
|
32
|
+
},
|
|
33
|
+
"./grab": {
|
|
34
|
+
"types": "./src/grab.d.ts",
|
|
35
|
+
"default": "./src/grab.js"
|
|
36
|
+
},
|
|
37
|
+
"./crypto": "./src/crypto.ts",
|
|
38
|
+
"./auth": "./src/auth/index.ts",
|
|
39
|
+
"./email": "./src/auth/email/index.ts",
|
|
40
|
+
"./errors": "./src/errors/sentry/index.ts",
|
|
41
|
+
"./errors/sveltekit": "./src/errors/sentry/sveltekit.ts",
|
|
42
|
+
"./analytics": "./src/analytics/client.ts",
|
|
43
|
+
"./analytics/server": "./src/analytics/server.ts",
|
|
44
|
+
"./analytics/svelte": "./src/analytics/svelte.ts",
|
|
45
|
+
"./analytics/umami": "./src/analytics/umami/protocol.ts",
|
|
46
|
+
"./analytics/reporting": "./src/analytics/umami/reporting.ts",
|
|
47
|
+
"./og": "./src/og/index.ts",
|
|
48
|
+
"./testimonials": "./src/testimonials/types.ts",
|
|
49
|
+
"./testimonials/server": "./src/testimonials/server.ts",
|
|
50
|
+
"./testimonials/render-content": "./src/testimonials/render-content.ts",
|
|
51
|
+
"./ai": "./src/ai/models.ts",
|
|
52
|
+
"./ai/image": "./src/ai/image.ts"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public",
|
|
56
|
+
"registry": "https://registry.npmjs.org/"
|
|
57
|
+
},
|
|
58
|
+
"dependencies": {
|
|
59
|
+
"clsx": "^2.1.1",
|
|
60
|
+
"tailwind-merge": "^3.7.0"
|
|
61
|
+
},
|
|
62
|
+
"devDependencies": {
|
|
63
|
+
"@keygraph/shannon": "^3.4.0",
|
|
64
|
+
"vite-plus": "1.3.0",
|
|
65
|
+
"vitest": "5.0.5"
|
|
66
|
+
},
|
|
67
|
+
"peerDependencies": {
|
|
68
|
+
"ai": ">=7.0.11",
|
|
69
|
+
"satori": ">=0.63.2",
|
|
70
|
+
"satori-html": ">=0.3.2",
|
|
71
|
+
"sharp": ">=0.33.0",
|
|
72
|
+
"ultrahtml": ">=1.7.0",
|
|
73
|
+
"vite": ">=5.0.0",
|
|
74
|
+
"wawoff2": ">=2.0.1"
|
|
75
|
+
},
|
|
76
|
+
"peerDependenciesMeta": {
|
|
77
|
+
"vite": {
|
|
78
|
+
"optional": true
|
|
79
|
+
},
|
|
80
|
+
"ai": {
|
|
81
|
+
"optional": true
|
|
82
|
+
},
|
|
83
|
+
"satori": {
|
|
84
|
+
"optional": true
|
|
85
|
+
},
|
|
86
|
+
"satori-html": {
|
|
87
|
+
"optional": true
|
|
88
|
+
},
|
|
89
|
+
"ultrahtml": {
|
|
90
|
+
"optional": true
|
|
91
|
+
},
|
|
92
|
+
"wawoff2": {
|
|
93
|
+
"optional": true
|
|
94
|
+
},
|
|
95
|
+
"sharp": {
|
|
96
|
+
"optional": true
|
|
97
|
+
}
|
|
98
|
+
},
|
|
99
|
+
"scripts": {
|
|
100
|
+
"test": "vp test run",
|
|
101
|
+
"typecheck": "tsc --noEmit",
|
|
102
|
+
"shannon": "shannon"
|
|
103
|
+
},
|
|
104
|
+
"license": "MIT"
|
|
105
|
+
}
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from "vite-plus/test";
|
|
2
|
+
import { generateImage } from "#src/ai/image.js";
|
|
3
|
+
|
|
4
|
+
function mockFetch(replies: { ok?: boolean; status?: number; json?: unknown; bytes?: string }[]) {
|
|
5
|
+
const calls: { url: string; init?: RequestInit }[] = [];
|
|
6
|
+
let index = 0;
|
|
7
|
+
vi.stubGlobal(
|
|
8
|
+
"fetch",
|
|
9
|
+
vi.fn(async (url: string | URL, init?: RequestInit) => {
|
|
10
|
+
calls.push({ url: String(url), init });
|
|
11
|
+
const reply = replies[Math.min(index++, replies.length - 1)];
|
|
12
|
+
return {
|
|
13
|
+
ok: reply.ok ?? true,
|
|
14
|
+
status: reply.status ?? 200,
|
|
15
|
+
headers: new Headers({ "content-type": "image/webp" }),
|
|
16
|
+
json: async () => reply.json ?? {},
|
|
17
|
+
arrayBuffer: async () => new TextEncoder().encode(reply.bytes ?? "").buffer,
|
|
18
|
+
} as unknown as Response;
|
|
19
|
+
}),
|
|
20
|
+
);
|
|
21
|
+
return calls;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
const OPTS = { prompt: "a poster", apiKey: "agnes_test" };
|
|
25
|
+
|
|
26
|
+
afterEach(() => {
|
|
27
|
+
vi.unstubAllGlobals();
|
|
28
|
+
vi.unstubAllEnvs();
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
describe("generateImage on the agnes lane", () => {
|
|
32
|
+
it("posts to the Agnes images endpoint and returns decoded bytes at no cost", async () => {
|
|
33
|
+
const calls = mockFetch([{ json: { data: [{ b64_json: btoa("PNGDATA") }] } }]);
|
|
34
|
+
|
|
35
|
+
const image = await generateImage({ ...OPTS, provider: "agnes" });
|
|
36
|
+
|
|
37
|
+
expect(calls[0].url).toBe("https://apihub.agnes-ai.com/v1/images/generations");
|
|
38
|
+
expect(new TextDecoder().decode(image.bytes)).toBe("PNGDATA");
|
|
39
|
+
expect(image.priceUsd).toBe(0);
|
|
40
|
+
expect(image.config.provider).toBe("agnes");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
// The tier is the output resolution, because that is how Agnes prices a frame.
|
|
44
|
+
it.each([
|
|
45
|
+
["small", "1K"],
|
|
46
|
+
["medium", "2K"],
|
|
47
|
+
["large", "4K"],
|
|
48
|
+
] as const)("maps the %s tier to size %s", async (tier, size) => {
|
|
49
|
+
const calls = mockFetch([{ json: { data: [{ b64_json: btoa("x") }] } }]);
|
|
50
|
+
|
|
51
|
+
await generateImage({ ...OPTS, provider: "agnes", tier });
|
|
52
|
+
|
|
53
|
+
expect(JSON.parse(String(calls[0].init?.body))).toMatchObject({ size, ratio: "1:1" });
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
// The regression guard for a 5-minute hang. Asking Agnes for base64 makes it hold the connection without sending headers, and undici kills the request at exactly 300s with UND_ERR_HEADERS_TIMEOUT.
|
|
57
|
+
|
|
58
|
+
// The same prompt answers in 35s in url mode. Measured 31 Aug 2026.
|
|
59
|
+
it("never asks for base64, which is what made the request time out", async () => {
|
|
60
|
+
const calls = mockFetch([
|
|
61
|
+
{ json: { data: [{ url: "https://cdn.agnes/f.png" }] } },
|
|
62
|
+
{ bytes: "PNG" },
|
|
63
|
+
]);
|
|
64
|
+
|
|
65
|
+
await generateImage({ ...OPTS, provider: "agnes" });
|
|
66
|
+
|
|
67
|
+
const body = JSON.parse(String(calls[0].init?.body));
|
|
68
|
+
expect(body.return_base64).toBeUndefined();
|
|
69
|
+
expect(body.extra_body).toBeUndefined();
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
// Measured 9 Sep 2026: the lane carries text-to-image only, and reports a reference as `403 team not allowed to access model`, which reads as a dead lane rather than a
|
|
73
|
+
|
|
74
|
+
// missing capability. It used to send the reference as a data URI and let the API answer that; refusing before the call is what makes the error true,
|
|
75
|
+
|
|
76
|
+
// and no request is worth spending to be told this.
|
|
77
|
+
it("refuses a reference before spending a call, and says why", async () => {
|
|
78
|
+
const calls = mockFetch([{ json: { data: [{ b64_json: btoa("x") }] } }]);
|
|
79
|
+
|
|
80
|
+
await expect(
|
|
81
|
+
generateImage({
|
|
82
|
+
...OPTS,
|
|
83
|
+
provider: "agnes",
|
|
84
|
+
references: [{ bytes: new TextEncoder().encode("REF"), mediaType: "image/jpeg" }],
|
|
85
|
+
}),
|
|
86
|
+
).rejects.toThrow("draws from a prompt only");
|
|
87
|
+
|
|
88
|
+
expect(calls).toHaveLength(0);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("carries the aspect ratio through as `ratio`", async () => {
|
|
92
|
+
const calls = mockFetch([{ json: { data: [{ b64_json: btoa("x") }] } }]);
|
|
93
|
+
|
|
94
|
+
await generateImage({ ...OPTS, provider: "agnes", aspectRatio: "16:9" });
|
|
95
|
+
|
|
96
|
+
expect(JSON.parse(String(calls[0].init?.body)).ratio).toBe("16:9");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
// An account that ignores `return_base64` answers with a url instead. Fetching
|
|
100
|
+
// it here keeps every caller on one return shape: bytes.
|
|
101
|
+
it("fetches the url when the account answers with one instead of base64", async () => {
|
|
102
|
+
const calls = mockFetch([
|
|
103
|
+
{ json: { data: [{ url: "https://cdn.agnes/frame.webp" }] } },
|
|
104
|
+
{ bytes: "WEBPDATA" },
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
const image = await generateImage({ ...OPTS, provider: "agnes" });
|
|
108
|
+
|
|
109
|
+
expect(calls[1].url).toBe("https://cdn.agnes/frame.webp");
|
|
110
|
+
expect(new TextDecoder().decode(image.bytes)).toBe("WEBPDATA");
|
|
111
|
+
expect(image.mediaType).toBe("image/webp");
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("carries the provider's own message when it refuses", async () => {
|
|
115
|
+
mockFetch([{ ok: false, status: 400, json: { error: { message: "ratio not supported" } } }]);
|
|
116
|
+
|
|
117
|
+
await expect(generateImage({ ...OPTS, provider: "agnes" })).rejects.toThrow(
|
|
118
|
+
"ratio not supported",
|
|
119
|
+
);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it("names the env var when no key is anywhere", async () => {
|
|
123
|
+
vi.stubEnv("AGNES_API_KEY", "");
|
|
124
|
+
mockFetch([{ json: {} }]);
|
|
125
|
+
|
|
126
|
+
await expect(generateImage({ prompt: "p", provider: "agnes" })).rejects.toThrow(
|
|
127
|
+
"AGNES_API_KEY",
|
|
128
|
+
);
|
|
129
|
+
});
|
|
130
|
+
});
|