|
| 1 | +import { describe, it, mock, beforeEach } from "node:test"; |
| 2 | +import assert from "node:assert/strict"; |
| 3 | + |
| 4 | +mock.module("./config.js", { |
| 5 | + namedExports: { |
| 6 | + config: { |
| 7 | + API_BASE: "https://test.run402.com", |
| 8 | + PROJECT_ID: "prj_test", |
| 9 | + SERVICE_KEY: "sk_test", |
| 10 | + }, |
| 11 | + }, |
| 12 | +}); |
| 13 | + |
| 14 | +const { assets } = await import("./assets.js"); |
| 15 | + |
| 16 | +const STUB_REF = { |
| 17 | + key: "images/avatar.png", |
| 18 | + sha256: "abc123", |
| 19 | + size_bytes: 4, |
| 20 | + content_type: "image/png", |
| 21 | + visibility: "public", |
| 22 | + immutable: true, |
| 23 | + url: "https://cdn.run402.com/images/avatar.png", |
| 24 | + immutable_url: "https://cdn.run402.com/images/avatar.png@abc123", |
| 25 | + cdn_url: null, |
| 26 | + cdn_immutable_url: null, |
| 27 | + sri: null, |
| 28 | + etag: "abc123", |
| 29 | + content_digest: "sha256-abc123", |
| 30 | +}; |
| 31 | + |
| 32 | +describe("assets.put — gateway path and auth", () => { |
| 33 | + let capturedUrl = ""; |
| 34 | + let capturedOpts: RequestInit = {}; |
| 35 | + |
| 36 | + beforeEach(() => { |
| 37 | + capturedUrl = ""; |
| 38 | + capturedOpts = {}; |
| 39 | + mock.method(globalThis, "fetch", async (url: string, opts: RequestInit) => { |
| 40 | + capturedUrl = url; |
| 41 | + capturedOpts = opts; |
| 42 | + return new Response(JSON.stringify(STUB_REF), { |
| 43 | + status: 200, |
| 44 | + headers: { "Content-Type": "application/json" }, |
| 45 | + }); |
| 46 | + }); |
| 47 | + }); |
| 48 | + |
| 49 | + it("posts to /apply/v1/service-asset-put with service credentials", async () => { |
| 50 | + await assets.put("images/avatar.png", new Uint8Array([1, 2, 3, 4])); |
| 51 | + |
| 52 | + assert.equal(capturedUrl, "https://test.run402.com/apply/v1/service-asset-put"); |
| 53 | + assert.equal(capturedOpts.method, "POST"); |
| 54 | + const headers = capturedOpts.headers as Record<string, string>; |
| 55 | + assert.equal(headers.Authorization, "Bearer sk_test"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("sets x-run402-asset-key header to the key argument", async () => { |
| 59 | + await assets.put("images/avatar.png", new Uint8Array([1, 2, 3, 4])); |
| 60 | + |
| 61 | + const headers = capturedOpts.headers as Record<string, string>; |
| 62 | + assert.equal(headers["x-run402-asset-key"], "images/avatar.png"); |
| 63 | + }); |
| 64 | + |
| 65 | + it("defaults visibility to public and immutable to true", async () => { |
| 66 | + await assets.put("images/avatar.png", new Uint8Array([1, 2, 3, 4])); |
| 67 | + |
| 68 | + const headers = capturedOpts.headers as Record<string, string>; |
| 69 | + assert.equal(headers["x-run402-asset-visibility"], "public"); |
| 70 | + assert.equal(headers["x-run402-asset-immutable"], "true"); |
| 71 | + }); |
| 72 | + |
| 73 | + it("forwards explicit visibility and immutable options", async () => { |
| 74 | + await assets.put("data/secret.bin", new Uint8Array([1, 2, 3, 4]), { |
| 75 | + visibility: "private", |
| 76 | + immutable: false, |
| 77 | + }); |
| 78 | + |
| 79 | + const headers = capturedOpts.headers as Record<string, string>; |
| 80 | + assert.equal(headers["x-run402-asset-visibility"], "private"); |
| 81 | + assert.equal(headers["x-run402-asset-immutable"], "false"); |
| 82 | + }); |
| 83 | + |
| 84 | + it("guesses Content-Type from key extension", async () => { |
| 85 | + await assets.put("styles/main.css", new Uint8Array([46, 99, 108, 115])); |
| 86 | + |
| 87 | + const headers = capturedOpts.headers as Record<string, string>; |
| 88 | + assert.equal(headers["Content-Type"], "text/css; charset=utf-8"); |
| 89 | + }); |
| 90 | + |
| 91 | + it("uses explicit contentType option over guessed extension", async () => { |
| 92 | + await assets.put("data/blob", new Uint8Array([1, 2, 3, 4]), { |
| 93 | + contentType: "application/msgpack", |
| 94 | + }); |
| 95 | + |
| 96 | + const headers = capturedOpts.headers as Record<string, string>; |
| 97 | + assert.equal(headers["Content-Type"], "application/msgpack"); |
| 98 | + }); |
| 99 | + |
| 100 | + it("sends raw binary body (not JSON)", async () => { |
| 101 | + const bytes = new Uint8Array([10, 20, 30]); |
| 102 | + await assets.put("file.bin", bytes); |
| 103 | + |
| 104 | + assert.ok(capturedOpts.body instanceof ArrayBuffer, "body must be ArrayBuffer (binary)"); |
| 105 | + assert.notEqual(typeof capturedOpts.body, "string", "body must not be stringified"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("accepts a string source — encodes to UTF-8 bytes", async () => { |
| 109 | + await assets.put("hello.txt", "hello"); |
| 110 | + |
| 111 | + assert.ok(capturedOpts.body instanceof ArrayBuffer); |
| 112 | + const decoded = new TextDecoder().decode(capturedOpts.body as ArrayBuffer); |
| 113 | + assert.equal(decoded, "hello"); |
| 114 | + }); |
| 115 | + |
| 116 | + it("returns a widened AssetRef with camelCase aliases", async () => { |
| 117 | + const ref = await assets.put("images/avatar.png", new Uint8Array([1, 2, 3, 4])); |
| 118 | + |
| 119 | + assert.equal(ref.key, "images/avatar.png"); |
| 120 | + assert.equal(ref.immutableUrl, STUB_REF.immutable_url); |
| 121 | + assert.equal(ref.contentType, STUB_REF.content_type); |
| 122 | + assert.equal(ref.size, STUB_REF.size_bytes); |
| 123 | + assert.equal(ref.contentSha256, STUB_REF.sha256); |
| 124 | + }); |
| 125 | +}); |
| 126 | + |
| 127 | +describe("assets.put — validation", () => { |
| 128 | + it("throws for empty key", async () => { |
| 129 | + await assert.rejects( |
| 130 | + async () => { await assets.put("", new Uint8Array([1])); }, |
| 131 | + /key must be a non-empty string/, |
| 132 | + ); |
| 133 | + }); |
| 134 | + |
| 135 | + it("throws for empty source bytes", async () => { |
| 136 | + await assert.rejects( |
| 137 | + async () => { await assets.put("file.bin", new Uint8Array([])); }, |
| 138 | + /bytes must be non-empty/, |
| 139 | + ); |
| 140 | + }); |
| 141 | + |
| 142 | + it("throws for object source with both content and bytes", async () => { |
| 143 | + await assert.rejects( |
| 144 | + async () => { |
| 145 | + await assets.put("f.bin", { content: "hi", bytes: new Uint8Array([1]) }); |
| 146 | + }, |
| 147 | + /provide exactly one of/, |
| 148 | + ); |
| 149 | + }); |
| 150 | + |
| 151 | + it("throws on non-ok response and preserves error detail", async () => { |
| 152 | + mock.method(globalThis, "fetch", async () => |
| 153 | + new Response(JSON.stringify({ code: "STORAGE_QUOTA_EXCEEDED", message: "over limit" }), { |
| 154 | + status: 402, |
| 155 | + }), |
| 156 | + ); |
| 157 | + await assert.rejects( |
| 158 | + async () => { await assets.put("f.bin", new Uint8Array([1])); }, |
| 159 | + /Asset put failed \(402\): STORAGE_QUOTA_EXCEEDED: over limit/, |
| 160 | + ); |
| 161 | + }); |
| 162 | +}); |
0 commit comments