@orieljs/testing 0.1.0-alpha.3 → 0.1.0-alpha.5

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/dist/index.d.ts CHANGED
@@ -362,6 +362,12 @@ declare class HttpClient {
362
362
  request(method: string, uri: string, options?: RequestOptions): Promise<TestResponse>;
363
363
  /** Runs `fn` in a scope prepared like a request's (carried session, actingAs users), e.g. for assertAuthenticated(). */
364
364
  inScope<T>(fn: (scope: Container) => T | Promise<T>): Promise<T>;
365
+ /**
366
+ * Runs `run` in a request scope like this client's requests have (a request for `method uri` bound, the carried
367
+ * session, actingAs users), then carries the session and the users on, as after a request. Live.test() runs its
368
+ * mount and updates through it.
369
+ */
370
+ withinRequest<T>(run: () => Promise<T>, method?: string, uri?: string): Promise<T>;
365
371
  /** Runs the request through the app's real HttpKernel, in a request scope this client owns. */
366
372
  protected send(request: Request): Promise<TestResponse>;
367
373
  /** Prepares the request scope: recorders, the carried session and actingAs users. */
@@ -435,6 +441,11 @@ declare abstract class InteractsWithApplication {
435
441
  protected defineEnvironment(_app: Application): void | Promise<void>;
436
442
  setUp(): Promise<void>;
437
443
  tearDown(): Promise<void>;
444
+ /**
445
+ * Laravel's withoutVite(): in this test's requests vite([...]) and viteReactRefresh() render nothing, so feature
446
+ * tests of pages pass before `vite build` has written public/build/manifest.json.
447
+ */
448
+ withoutVite(): this;
438
449
  /** Runs in tearDown(), before the application is thrown away (in registration order). */
439
450
  beforeApplicationDestroyed(callback: Callback): void;
440
451
  }
@@ -446,6 +457,8 @@ declare abstract class MakesHttpRequests extends InteractsWithApplication {
446
457
  /** The current test's HTTP client (created on first use). */
447
458
  protected get http(): HttpClient;
448
459
  protected createHttpClient(): HttpClient;
460
+ /** Live.test() runs in this test's request scope (Live reads the same token: no import between the packages). */
461
+ setUp(): Promise<void>;
449
462
  tearDown(): Promise<void>;
450
463
  withHeaders(headers: Headers$1): this;
451
464
  withHeader(name: string, value: string): this;
@@ -592,11 +605,11 @@ declare function install<T extends object, F extends T>(token: Token<T>, fake: F
592
605
  declare function nameOf(target: Target | object): string;
593
606
  /** A recorded object (or string event) matches a target: same constructor, or the same (class) name. */
594
607
  declare function matches(item: unknown, target: Target): boolean;
595
- /** Bus's ShouldQueue marker (the Queueable base class sets it on its prototype); the same Symbol.for key as 06. */
608
+ /** Bus's ShouldQueue marker (the Queueable base class sets it on its prototype); the same Symbol.for key as Bus's. */
596
609
  declare const SHOULD_QUEUE: unique symbol;
597
610
  /**
598
- * A verbatim copy of Bus's isQueueable() (06-queue-bus.md; Testing can't import Bus). The rule is read on the
599
- * instance: the ShouldQueue marker, a value for any ShouldQueue option, or uniqueId()/retryUntil().
611
+ * A copy of Bus's isQueueable(), read on the instance: ShouldQueue's `shouldQueue: true` (own field or prototype),
612
+ * the SHOULD_QUEUE symbol, a value for any ShouldQueue option, or uniqueId()/retryUntil().
600
613
  */
601
614
  declare function isQueueable(job: unknown): boolean;
602
615
 
@@ -874,6 +887,7 @@ interface StoredFile {
874
887
  mimeType: string | undefined;
875
888
  modified: Date;
876
889
  }
890
+ /** By the extension as PHP's pathinfo() reads it: after the last dot of the base name ('.pdf' is a PDF, 'pdf' isn't). */
877
891
  declare function guessMimeType(path: string): string | undefined;
878
892
  /**
879
893
  * An in-memory disk implementing the Filesystem contract, with Laravel's fake-disk assertions. (Laravel's fake
@@ -894,7 +908,12 @@ declare class FakeDisk implements Filesystem {
894
908
  readStream(path: string): Promise<ReadableStream<Uint8Array>>;
895
909
  put(path: string, contents: string | Uint8Array | ReadableStream<Uint8Array>, options?: PutOptions): Promise<boolean>;
896
910
  putFile(directory: string, file: UploadedFile, options?: PutOptions): Promise<string>;
911
+ /**
912
+ * The upload's own type, as the S3 disk sends it: its extension(), never the MIME type the client sent as such nor
913
+ * the stored name's type.
914
+ */
897
915
  putFileAs(directory: string, file: UploadedFile, name: string, options?: PutOptions): Promise<string>;
916
+ private write;
898
917
  delete(paths: string | string[]): Promise<boolean>;
899
918
  copy(from: string, to: string): Promise<boolean>;
900
919
  move(from: string, to: string): Promise<boolean>;
@@ -954,9 +973,17 @@ declare class StorageFake implements FilesystemManager {
954
973
 
955
974
  /** Laravel's UploadedFile::fake(): File objects to post as multipart uploads (t.post(uri, { avatar: file })). */
956
975
  declare const FakeFile: {
957
- /** A file of `kilobytes` KiB (zero bytes), typed by `mimeType` or the extension. */
976
+ /**
977
+ * A file of `kilobytes` KiB typed by `mimeType` or the name's extension. Validation reads a file's bytes, so the
978
+ * file starts like its type (a PDF's `%PDF-`, a PNG's signature, text for text types) and passes `mimes` for its
979
+ * name, as Laravel's fakes do. It is at least its type's leading bytes and never empty, whatever the size asked for.
980
+ * An image made this way has no readable size: use image() for `dimensions`.
981
+ */
958
982
  create(name: string, kilobytes?: number, mimeType?: string): File;
959
- /** A real PNG image of the given size (typed by the name's extension, like Laravel's image()). */
983
+ /**
984
+ * A real white image of the given size in the format the name's extension asks for: png, jpg/jpeg, gif, webp or
985
+ * bmp (any other name gets a JPEG, as Laravel's fakes do), so image, mimes and dimensions pass on it.
986
+ */
960
987
  image(name: string, width?: number, height?: number): File;
961
988
  };
962
989
 
package/dist/index.js CHANGED
@@ -274,7 +274,7 @@ var DatabaseTransactions = {
274
274
  };
275
275
 
276
276
  // Concerns/MakesHttpRequests.ts
277
- import { AuthFactory as AuthFactory2 } from "@orieljs/contracts";
277
+ import { AuthFactory as AuthFactory2, token } from "@orieljs/contracts";
278
278
 
279
279
  // Http/HttpClient.ts
280
280
  import {
@@ -283,9 +283,11 @@ import {
283
283
  Encrypter as Encrypter2,
284
284
  ExceptionHandler,
285
285
  HttpKernel,
286
+ HttpRequest,
286
287
  Session,
287
288
  ViewFactory
288
289
  } from "@orieljs/contracts";
290
+ import { IncomingRequest } from "@orieljs/http";
289
291
 
290
292
  // environment.ts
291
293
  var TESTING_ENVIRONMENT = Object.freeze({
@@ -1444,6 +1446,31 @@ var HttpClient = class {
1444
1446
  return fn(scope);
1445
1447
  });
1446
1448
  }
1449
+ /**
1450
+ * Runs `run` in a request scope like this client's requests have (a request for `method uri` bound, the carried
1451
+ * session, actingAs users), then carries the session and the users on, as after a request. Live.test() runs its
1452
+ * mount and updates through it.
1453
+ */
1454
+ async withinRequest(run, method = "GET", uri = "/") {
1455
+ await this.app.boot();
1456
+ const request = this.buildRequest(method, uri, {});
1457
+ setClientAddress(request, "127.0.0.1");
1458
+ const scope = this.app.createScope();
1459
+ const context = { request, app: this.app, client: this };
1460
+ const result = await scope.run(async () => {
1461
+ this.beforeRequest(scope, context);
1462
+ scope.instance(HttpRequest, new IncomingRequest(request));
1463
+ if (scope.bound(AuthFactory)) {
1464
+ try {
1465
+ await scope.make(AuthFactory).guard().check();
1466
+ } catch {
1467
+ }
1468
+ }
1469
+ return run();
1470
+ });
1471
+ await scope.run(() => this.afterRequest(scope, context));
1472
+ return result;
1473
+ }
1447
1474
  /** Runs the request through the app's real HttpKernel, in a request scope this client owns. */
1448
1475
  async send(request) {
1449
1476
  const app = this.app;
@@ -1588,7 +1615,7 @@ import {
1588
1615
  DatabaseManager as DatabaseManager2
1589
1616
  } from "@orieljs/contracts";
1590
1617
  import { Container } from "@orieljs/container";
1591
- import { Application as FoundationApplication } from "@orieljs/foundation";
1618
+ import { Application as FoundationApplication, Vite, ViteHtml } from "@orieljs/foundation";
1592
1619
  var globalHooks = () => {
1593
1620
  const g = globalThis;
1594
1621
  return typeof g.beforeEach === "function" && typeof g.afterEach === "function" ? { beforeEach: g.beforeEach, afterEach: g.afterEach } : void 0;
@@ -1669,11 +1696,43 @@ var InteractsWithApplication = class {
1669
1696
  if (global === app) Container.setInstance(void 0);
1670
1697
  }
1671
1698
  }
1699
+ /**
1700
+ * Laravel's withoutVite(): in this test's requests vite([...]) and viteReactRefresh() render nothing, so feature
1701
+ * tests of pages pass before `vite build` has written public/build/manifest.json.
1702
+ */
1703
+ withoutVite() {
1704
+ const app = this.app;
1705
+ app.scoped(Vite, () => new WithoutVite(app));
1706
+ return this;
1707
+ }
1672
1708
  /** Runs in tearDown(), before the application is thrown away (in registration order). */
1673
1709
  beforeApplicationDestroyed(callback) {
1674
1710
  this.destroying.push(callback);
1675
1711
  }
1676
1712
  };
1713
+ var WithoutVite = class extends Vite {
1714
+ tags() {
1715
+ return new ViteHtml("");
1716
+ }
1717
+ reactRefresh() {
1718
+ return void 0;
1719
+ }
1720
+ asset() {
1721
+ return "";
1722
+ }
1723
+ content() {
1724
+ return "";
1725
+ }
1726
+ manifestHash() {
1727
+ return void 0;
1728
+ }
1729
+ preloadedAssets() {
1730
+ return {};
1731
+ }
1732
+ toHtml() {
1733
+ return "";
1734
+ }
1735
+ };
1677
1736
  async function disconnectAll(db) {
1678
1737
  const manager = db;
1679
1738
  if (typeof manager.disconnectAll === "function") await manager.disconnectAll();
@@ -1690,6 +1749,11 @@ var MakesHttpRequests = class extends InteractsWithApplication {
1690
1749
  createHttpClient() {
1691
1750
  return new HttpClient(this.app);
1692
1751
  }
1752
+ /** Live.test() runs in this test's request scope (Live reads the same token: no import between the packages). */
1753
+ async setUp() {
1754
+ await super.setUp();
1755
+ this.app.instance(token("Testing.RequestScope"), (run) => this.http.withinRequest(run));
1756
+ }
1693
1757
  async tearDown() {
1694
1758
  try {
1695
1759
  await super.tearDown();
@@ -1705,8 +1769,8 @@ var MakesHttpRequests = class extends InteractsWithApplication {
1705
1769
  this.http.headers[name] = value;
1706
1770
  return this;
1707
1771
  }
1708
- withToken(token, type = "Bearer") {
1709
- return this.withHeader("Authorization", `${type} ${token}`);
1772
+ withToken(token2, type = "Bearer") {
1773
+ return this.withHeader("Authorization", `${type} ${token2}`);
1710
1774
  }
1711
1775
  withBasicAuth(username, password) {
1712
1776
  return this.withToken(Buffer.from(`${username}:${password}`).toString("base64"), "Basic");
@@ -1899,13 +1963,13 @@ var TestCase = class extends InteractsWithConsole {
1899
1963
  // Fakes/support.ts
1900
1964
  import { currentContainer } from "@orieljs/container";
1901
1965
  import { facade } from "@orieljs/support";
1902
- function install(token, fake) {
1903
- facade(token).swap(fake);
1966
+ function install(token2, fake) {
1967
+ facade(token2).swap(fake);
1904
1968
  return fake;
1905
1969
  }
1906
- function bound(token) {
1970
+ function bound(token2) {
1907
1971
  const container = currentContainer();
1908
- return container !== void 0 && container.bound(token) ? container.make(token) : void 0;
1972
+ return container !== void 0 && container.bound(token2) ? container.make(token2) : void 0;
1909
1973
  }
1910
1974
  function nameOf(target2) {
1911
1975
  if (typeof target2 === "string") return target2;
@@ -1928,7 +1992,7 @@ var QUEUE_OPTIONS = ["connection", "queue", "delay", "tries", "backoff", "timeou
1928
1992
  function isQueueable(job) {
1929
1993
  if (typeof job !== "object" || job === null) return false;
1930
1994
  const members = job;
1931
- if (members[SHOULD_QUEUE] === true) return true;
1995
+ if (members.shouldQueue === true || members[SHOULD_QUEUE] === true) return true;
1932
1996
  if (QUEUE_OPTIONS.some((option) => members[option] !== void 0)) return true;
1933
1997
  return typeof members.uniqueId === "function" || typeof members.retryUntil === "function";
1934
1998
  }
@@ -2738,6 +2802,7 @@ var NotificationFake = class _NotificationFake {
2738
2802
 
2739
2803
  // Fakes/StorageFake.ts
2740
2804
  import { randomBytes } from "crypto";
2805
+ import { posix } from "path";
2741
2806
  import { ConfigRepository as ConfigRepository4, FilesystemManager } from "@orieljs/contracts";
2742
2807
  var FakeDiskError = class extends Error {
2743
2808
  constructor(name, message, path) {
@@ -2768,8 +2833,74 @@ var MIME = {
2768
2833
  mp4: "video/mp4"
2769
2834
  };
2770
2835
  function guessMimeType(path) {
2771
- const extension = path.split(".").pop()?.toLowerCase() ?? "";
2772
- return MIME[extension];
2836
+ const base = path.slice(path.lastIndexOf("/") + 1);
2837
+ const dot = base.lastIndexOf(".");
2838
+ const extension = dot === -1 ? "" : base.slice(dot + 1).toLowerCase();
2839
+ return Object.hasOwn(MIME, extension) ? MIME[extension] : void 0;
2840
+ }
2841
+ var DISK_TYPES = {
2842
+ txt: "text/plain",
2843
+ html: "text/html",
2844
+ htm: "text/html",
2845
+ css: "text/css",
2846
+ csv: "text/csv",
2847
+ md: "text/markdown",
2848
+ js: "text/javascript",
2849
+ mjs: "text/javascript",
2850
+ json: "application/json",
2851
+ xml: "application/xml",
2852
+ pdf: "application/pdf",
2853
+ zip: "application/zip",
2854
+ gz: "application/gzip",
2855
+ tar: "application/x-tar",
2856
+ "7z": "application/x-7z-compressed",
2857
+ rar: "application/vnd.rar",
2858
+ epub: "application/epub+zip",
2859
+ rtf: "application/rtf",
2860
+ doc: "application/msword",
2861
+ docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
2862
+ xls: "application/vnd.ms-excel",
2863
+ xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
2864
+ ppt: "application/vnd.ms-powerpoint",
2865
+ pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
2866
+ odt: "application/vnd.oasis.opendocument.text",
2867
+ ods: "application/vnd.oasis.opendocument.spreadsheet",
2868
+ odp: "application/vnd.oasis.opendocument.presentation",
2869
+ png: "image/png",
2870
+ jpg: "image/jpeg",
2871
+ jpeg: "image/jpeg",
2872
+ gif: "image/gif",
2873
+ webp: "image/webp",
2874
+ avif: "image/avif",
2875
+ svg: "image/svg+xml",
2876
+ ico: "image/x-icon",
2877
+ bmp: "image/bmp",
2878
+ tif: "image/tiff",
2879
+ tiff: "image/tiff",
2880
+ heic: "image/heic",
2881
+ heif: "image/heif",
2882
+ mp3: "audio/mpeg",
2883
+ m4a: "audio/mp4",
2884
+ wav: "audio/wav",
2885
+ ogg: "audio/ogg",
2886
+ oga: "audio/ogg",
2887
+ opus: "audio/ogg",
2888
+ flac: "audio/flac",
2889
+ aac: "audio/aac",
2890
+ mp4: "video/mp4",
2891
+ mov: "video/quicktime",
2892
+ webm: "video/webm",
2893
+ ogv: "video/ogg",
2894
+ avi: "video/x-msvideo",
2895
+ woff: "font/woff",
2896
+ woff2: "font/woff2",
2897
+ ttf: "font/ttf",
2898
+ otf: "font/otf",
2899
+ wasm: "application/wasm"
2900
+ };
2901
+ function diskType(extension) {
2902
+ const key = extension.replace(/^\./, "").toLowerCase();
2903
+ return Object.hasOwn(DISK_TYPES, key) ? DISK_TYPES[key] : void 0;
2773
2904
  }
2774
2905
  function hashName(file) {
2775
2906
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -2825,17 +2956,24 @@ var FakeDisk = class {
2825
2956
  async put(path, contents, options = {}) {
2826
2957
  const key = normalize(path);
2827
2958
  const bytes = typeof contents === "string" ? new TextEncoder().encode(contents) : contents instanceof Uint8Array ? contents.slice() : await collect(contents);
2828
- this.stored.set(key, { bytes, visibility: options.visibility ?? "private", mimeType: options.mimeType ?? guessMimeType(key), modified: /* @__PURE__ */ new Date() });
2959
+ this.write(key, bytes, options.mimeType ?? diskType(posix.extname(key)), options);
2829
2960
  return true;
2830
2961
  }
2831
2962
  putFile(directory, file, options = {}) {
2832
2963
  return this.putFileAs(directory, file, hashName(file), options);
2833
2964
  }
2965
+ /**
2966
+ * The upload's own type, as the S3 disk sends it: its extension(), never the MIME type the client sent as such nor
2967
+ * the stored name's type.
2968
+ */
2834
2969
  async putFileAs(directory, file, name, options = {}) {
2835
2970
  const path = normalize(`${directory}/${name}`);
2836
- await this.put(path, new Uint8Array(await file.arrayBuffer()), { ...options, mimeType: options.mimeType ?? file.mimeType });
2971
+ this.write(path, new Uint8Array(await file.arrayBuffer()), options.mimeType ?? diskType(file.extension()), options);
2837
2972
  return path;
2838
2973
  }
2974
+ write(key, bytes, mimeType, options) {
2975
+ this.stored.set(key, { bytes, visibility: options.visibility ?? "private", mimeType, modified: /* @__PURE__ */ new Date() });
2976
+ }
2839
2977
  async delete(paths) {
2840
2978
  for (const path of [paths].flat()) this.stored.delete(normalize(path));
2841
2979
  return true;
@@ -3032,6 +3170,37 @@ var StorageFake = class _StorageFake {
3032
3170
 
3033
3171
  // FakeFile.ts
3034
3172
  import { deflateSync } from "zlib";
3173
+ var ascii = (text) => [...text].map((char) => char.charCodeAt(0));
3174
+ var u16be = (n) => [n >>> 8 & 255, n & 255];
3175
+ var u16le = (n) => [n & 255, n >>> 8 & 255];
3176
+ var u32le = (n) => [n & 255, n >>> 8 & 255, n >>> 16 & 255, n >>> 24 & 255];
3177
+ function concat(...parts) {
3178
+ const out = new Uint8Array(parts.reduce((n, part) => n + part.length, 0));
3179
+ let offset = 0;
3180
+ for (const part of parts) {
3181
+ out.set(part, offset);
3182
+ offset += part.length;
3183
+ }
3184
+ return out;
3185
+ }
3186
+ var BitWriter = class {
3187
+ bytes = [];
3188
+ buffer = 0;
3189
+ count = 0;
3190
+ write(value, bits) {
3191
+ this.buffer |= value << this.count;
3192
+ this.count += bits;
3193
+ while (this.count >= 8) {
3194
+ this.bytes.push(this.buffer & 255);
3195
+ this.buffer >>>= 8;
3196
+ this.count -= 8;
3197
+ }
3198
+ }
3199
+ finish() {
3200
+ if (this.count > 0) this.bytes.push(this.buffer & 255);
3201
+ return Uint8Array.from(this.bytes);
3202
+ }
3203
+ };
3035
3204
  var CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
3036
3205
  let c = n;
3037
3206
  for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
@@ -3059,23 +3228,175 @@ function png(width, height) {
3059
3228
  header.set([8, 2, 0, 0, 0], 8);
3060
3229
  const rows = new Uint8Array((width * 3 + 1) * height).fill(255);
3061
3230
  for (let y = 0; y < height; y++) rows[y * (width * 3 + 1)] = 0;
3062
- const parts = [new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", header), chunk("IDAT", deflateSync(rows)), chunk("IEND", new Uint8Array())];
3063
- const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
3064
- let offset = 0;
3065
- for (const part of parts) {
3066
- out.set(part, offset);
3067
- offset += part.length;
3068
- }
3231
+ return concat([137, 80, 78, 71, 13, 10, 26, 10], chunk("IHDR", header), chunk("IDAT", deflateSync(rows)), chunk("IEND", new Uint8Array()));
3232
+ }
3233
+ var oneCodeTable = (tableClass) => [255, 196, ...u16be(20), tableClass, 1, ...new Array(15).fill(0), 0];
3234
+ function jpeg(width, height) {
3235
+ const bits = Math.ceil(width / 8) * Math.ceil(height / 8) * 2;
3236
+ const scan = new Uint8Array(Math.ceil(bits / 8));
3237
+ const padding = scan.length * 8 - bits;
3238
+ if (padding > 0) scan[scan.length - 1] = (1 << padding) - 1;
3239
+ return concat(
3240
+ [255, 216],
3241
+ // SOI
3242
+ [255, 224, ...u16be(16), ...ascii("JFIF"), 0, 1, 1, 0, 0, 1, 0, 1, 0, 0],
3243
+ // APP0
3244
+ [255, 219, ...u16be(67), 0, ...new Array(64).fill(1)],
3245
+ // DQT: table 0, all ones
3246
+ [255, 192, ...u16be(11), 8, ...u16be(height), ...u16be(width), 1, 1, 17, 0],
3247
+ // SOF0: one component
3248
+ oneCodeTable(0),
3249
+ // DHT: DC table 0
3250
+ oneCodeTable(16),
3251
+ // DHT: AC table 0
3252
+ [255, 218, ...u16be(8), 1, 1, 0, 0, 63, 0],
3253
+ // SOS
3254
+ scan,
3255
+ [255, 217]
3256
+ // EOI
3257
+ );
3258
+ }
3259
+ function lzwZeros(pixels, minCodeSize) {
3260
+ const clear = 1 << minCodeSize;
3261
+ const end = clear + 1;
3262
+ const out = new BitWriter();
3263
+ const table = /* @__PURE__ */ new Map();
3264
+ let codeSize = minCodeSize + 1;
3265
+ let next = end + 1;
3266
+ let current = 0;
3267
+ out.write(clear, codeSize);
3268
+ for (let i = 1; i < pixels; i++) {
3269
+ const key = current << 8;
3270
+ const known = table.get(key);
3271
+ if (known !== void 0) {
3272
+ current = known;
3273
+ continue;
3274
+ }
3275
+ out.write(current, codeSize);
3276
+ if (next === 4096) {
3277
+ out.write(clear, codeSize);
3278
+ table.clear();
3279
+ codeSize = minCodeSize + 1;
3280
+ next = end + 1;
3281
+ } else {
3282
+ if (next >= 1 << codeSize) codeSize++;
3283
+ table.set(key, next++);
3284
+ }
3285
+ current = 0;
3286
+ }
3287
+ out.write(current, codeSize);
3288
+ out.write(end, codeSize);
3289
+ return out.finish();
3290
+ }
3291
+ function gif(width, height) {
3292
+ const data = lzwZeros(width * height, 2);
3293
+ const blocks = [];
3294
+ for (let offset = 0; offset < data.length; offset += 255) {
3295
+ const block = data.subarray(offset, offset + 255);
3296
+ blocks.push(block.length, ...block);
3297
+ }
3298
+ return concat(
3299
+ ascii("GIF89a"),
3300
+ [...u16le(width), ...u16le(height), 128, 0, 0],
3301
+ // a global palette of two colours
3302
+ [255, 255, 255, 0, 0, 0],
3303
+ [44, 0, 0, 0, 0, ...u16le(width), ...u16le(height), 0],
3304
+ // the image descriptor
3305
+ [2],
3306
+ blocks,
3307
+ [0, 59]
3308
+ // the end of the data, the trailer
3309
+ );
3310
+ }
3311
+ function webp(width, height) {
3312
+ const bits = new BitWriter();
3313
+ bits.write(47, 8);
3314
+ bits.write(width - 1, 14);
3315
+ bits.write(height - 1, 14);
3316
+ bits.write(0, 4);
3317
+ bits.write(0, 3);
3318
+ for (const symbol of [255, 255, 255, 255, 0]) {
3319
+ bits.write(1, 1);
3320
+ bits.write(0, 1);
3321
+ bits.write(1, 1);
3322
+ bits.write(symbol, 8);
3323
+ }
3324
+ const data = bits.finish();
3325
+ const padded = data.length % 2 === 0 ? data : concat(data, [0]);
3326
+ return concat(ascii("RIFF"), u32le(12 + padded.length), ascii("WEBP"), ascii("VP8L"), u32le(data.length), padded);
3327
+ }
3328
+ function bmp(width, height) {
3329
+ const row = Math.ceil(width * 3 / 4) * 4;
3330
+ const out = new Uint8Array(54 + row * height);
3331
+ const view = new DataView(out.buffer);
3332
+ out.set(ascii("BM"));
3333
+ view.setUint32(2, out.length, true);
3334
+ view.setUint32(10, 54, true);
3335
+ view.setUint32(14, 40, true);
3336
+ view.setInt32(18, width, true);
3337
+ view.setInt32(22, height, true);
3338
+ view.setUint16(26, 1, true);
3339
+ view.setUint16(28, 24, true);
3340
+ view.setUint32(34, row * height, true);
3341
+ for (let y = 0; y < height; y++) out.fill(255, 54 + y * row, 54 + y * row + width * 3);
3342
+ return out;
3343
+ }
3344
+ var IMAGE_FORMATS = {
3345
+ png: { type: "image/png", encode: png },
3346
+ jpg: { type: "image/jpeg", encode: jpeg },
3347
+ jpeg: { type: "image/jpeg", encode: jpeg },
3348
+ gif: { type: "image/gif", encode: gif },
3349
+ webp: { type: "image/webp", encode: webp },
3350
+ bmp: { type: "image/bmp", encode: bmp }
3351
+ };
3352
+ var extensionOf = (name) => name.includes(".") ? name.slice(name.lastIndexOf(".") + 1).toLowerCase() : "";
3353
+ var MORE_MIME_TYPES = {
3354
+ bmp: "image/bmp",
3355
+ avif: "image/avif",
3356
+ xml: "text/xml",
3357
+ htm: "text/html",
3358
+ md: "text/markdown",
3359
+ tsv: "text/tab-separated-values",
3360
+ text: "text/plain"
3361
+ };
3362
+ var LEADING_BYTES = {
3363
+ "image/jpeg": { bytes: [255, 216, 255, 224, ...u16be(16), ...ascii("JFIF"), 0, 1, 1, 0, 0, 1, 0, 1, 0, 0] },
3364
+ "image/png": { bytes: [137, 80, 78, 71, 13, 10, 26, 10] },
3365
+ "image/gif": { bytes: "GIF89a" },
3366
+ "image/webp": { bytes: "RIFF\0\0\0\0WEBP" },
3367
+ "image/avif": { bytes: [0, 0, 0, 20, ...ascii("ftypavif"), 0, 0, 0, 0, ...ascii("avifmif1")] },
3368
+ "image/bmp": { bytes: [...ascii("BM"), 0, 0, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0] },
3369
+ "application/pdf": { bytes: "%PDF-1.4\n" },
3370
+ "image/svg+xml": { bytes: '<svg xmlns="http://www.w3.org/2000/svg"/>\n', text: true },
3371
+ "text/html": { bytes: "<!doctype html>\n", text: true },
3372
+ "text/xml": { bytes: '<?xml version="1.0"?>\n', text: true }
3373
+ };
3374
+ function contentFor(type, size) {
3375
+ const text = type.startsWith("text/") || type === "application/json";
3376
+ const leading = LEADING_BYTES[type] ?? { bytes: text ? "\n" : [], text };
3377
+ const head = typeof leading.bytes === "string" ? new TextEncoder().encode(leading.bytes) : Uint8Array.from(leading.bytes);
3378
+ const out = new Uint8Array(Math.max(size, head.length, 1)).fill(leading.text ? 32 : 0);
3379
+ out.set(head);
3069
3380
  return out;
3070
3381
  }
3071
3382
  var FakeFile = {
3072
- /** A file of `kilobytes` KiB (zero bytes), typed by `mimeType` or the extension. */
3383
+ /**
3384
+ * A file of `kilobytes` KiB typed by `mimeType` or the name's extension. Validation reads a file's bytes, so the
3385
+ * file starts like its type (a PDF's `%PDF-`, a PNG's signature, text for text types) and passes `mimes` for its
3386
+ * name, as Laravel's fakes do. It is at least its type's leading bytes and never empty, whatever the size asked for.
3387
+ * An image made this way has no readable size: use image() for `dimensions`.
3388
+ */
3073
3389
  create(name, kilobytes = 0, mimeType) {
3074
- return new File([new Uint8Array(Math.round(kilobytes * 1024))], name, { type: mimeType ?? guessMimeType(name) ?? "application/octet-stream" });
3390
+ const type = mimeType ?? guessMimeType(name) ?? MORE_MIME_TYPES[extensionOf(name)] ?? "application/octet-stream";
3391
+ return new File([contentFor(type, Math.round(kilobytes * 1024))], name, { type });
3075
3392
  },
3076
- /** A real PNG image of the given size (typed by the name's extension, like Laravel's image()). */
3393
+ /**
3394
+ * A real white image of the given size in the format the name's extension asks for: png, jpg/jpeg, gif, webp or
3395
+ * bmp (any other name gets a JPEG, as Laravel's fakes do), so image, mimes and dimensions pass on it.
3396
+ */
3077
3397
  image(name, width = 10, height = 10) {
3078
- return new File([png(width, height)], name, { type: guessMimeType(name) ?? "image/png" });
3398
+ const format2 = IMAGE_FORMATS[extensionOf(name)] ?? IMAGE_FORMATS.jpeg;
3399
+ return new File([format2.encode(width, height)], name, { type: guessMimeType(name) ?? format2.type });
3079
3400
  }
3080
3401
  };
3081
3402
  export {