@orieljs/testing 0.1.0-alpha.4 → 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
@@ -605,11 +605,11 @@ declare function install<T extends object, F extends T>(token: Token<T>, fake: F
605
605
  declare function nameOf(target: Target | object): string;
606
606
  /** A recorded object (or string event) matches a target: same constructor, or the same (class) name. */
607
607
  declare function matches(item: unknown, target: Target): boolean;
608
- /** 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. */
609
609
  declare const SHOULD_QUEUE: unique symbol;
610
610
  /**
611
- * A verbatim copy of Bus's isQueueable() (06-queue-bus.md; Testing can't import Bus). The rule is read on the
612
- * 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().
613
613
  */
614
614
  declare function isQueueable(job: unknown): boolean;
615
615
 
@@ -887,6 +887,7 @@ interface StoredFile {
887
887
  mimeType: string | undefined;
888
888
  modified: Date;
889
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). */
890
891
  declare function guessMimeType(path: string): string | undefined;
891
892
  /**
892
893
  * An in-memory disk implementing the Filesystem contract, with Laravel's fake-disk assertions. (Laravel's fake
@@ -907,7 +908,12 @@ declare class FakeDisk implements Filesystem {
907
908
  readStream(path: string): Promise<ReadableStream<Uint8Array>>;
908
909
  put(path: string, contents: string | Uint8Array | ReadableStream<Uint8Array>, options?: PutOptions): Promise<boolean>;
909
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
+ */
910
915
  putFileAs(directory: string, file: UploadedFile, name: string, options?: PutOptions): Promise<string>;
916
+ private write;
911
917
  delete(paths: string | string[]): Promise<boolean>;
912
918
  copy(from: string, to: string): Promise<boolean>;
913
919
  move(from: string, to: string): Promise<boolean>;
@@ -967,9 +973,17 @@ declare class StorageFake implements FilesystemManager {
967
973
 
968
974
  /** Laravel's UploadedFile::fake(): File objects to post as multipart uploads (t.post(uri, { avatar: file })). */
969
975
  declare const FakeFile: {
970
- /** 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
+ */
971
982
  create(name: string, kilobytes?: number, mimeType?: string): File;
972
- /** 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
+ */
973
987
  image(name: string, width?: number, height?: number): File;
974
988
  };
975
989
 
package/dist/index.js CHANGED
@@ -1992,7 +1992,7 @@ var QUEUE_OPTIONS = ["connection", "queue", "delay", "tries", "backoff", "timeou
1992
1992
  function isQueueable(job) {
1993
1993
  if (typeof job !== "object" || job === null) return false;
1994
1994
  const members = job;
1995
- if (members[SHOULD_QUEUE] === true) return true;
1995
+ if (members.shouldQueue === true || members[SHOULD_QUEUE] === true) return true;
1996
1996
  if (QUEUE_OPTIONS.some((option) => members[option] !== void 0)) return true;
1997
1997
  return typeof members.uniqueId === "function" || typeof members.retryUntil === "function";
1998
1998
  }
@@ -2802,6 +2802,7 @@ var NotificationFake = class _NotificationFake {
2802
2802
 
2803
2803
  // Fakes/StorageFake.ts
2804
2804
  import { randomBytes } from "crypto";
2805
+ import { posix } from "path";
2805
2806
  import { ConfigRepository as ConfigRepository4, FilesystemManager } from "@orieljs/contracts";
2806
2807
  var FakeDiskError = class extends Error {
2807
2808
  constructor(name, message, path) {
@@ -2832,8 +2833,74 @@ var MIME = {
2832
2833
  mp4: "video/mp4"
2833
2834
  };
2834
2835
  function guessMimeType(path) {
2835
- const extension = path.split(".").pop()?.toLowerCase() ?? "";
2836
- 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;
2837
2904
  }
2838
2905
  function hashName(file) {
2839
2906
  const alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
@@ -2889,17 +2956,24 @@ var FakeDisk = class {
2889
2956
  async put(path, contents, options = {}) {
2890
2957
  const key = normalize(path);
2891
2958
  const bytes = typeof contents === "string" ? new TextEncoder().encode(contents) : contents instanceof Uint8Array ? contents.slice() : await collect(contents);
2892
- 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);
2893
2960
  return true;
2894
2961
  }
2895
2962
  putFile(directory, file, options = {}) {
2896
2963
  return this.putFileAs(directory, file, hashName(file), options);
2897
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
+ */
2898
2969
  async putFileAs(directory, file, name, options = {}) {
2899
2970
  const path = normalize(`${directory}/${name}`);
2900
- 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);
2901
2972
  return path;
2902
2973
  }
2974
+ write(key, bytes, mimeType, options) {
2975
+ this.stored.set(key, { bytes, visibility: options.visibility ?? "private", mimeType, modified: /* @__PURE__ */ new Date() });
2976
+ }
2903
2977
  async delete(paths) {
2904
2978
  for (const path of [paths].flat()) this.stored.delete(normalize(path));
2905
2979
  return true;
@@ -3096,6 +3170,37 @@ var StorageFake = class _StorageFake {
3096
3170
 
3097
3171
  // FakeFile.ts
3098
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
+ };
3099
3204
  var CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
3100
3205
  let c = n;
3101
3206
  for (let k = 0; k < 8; k++) c = c & 1 ? 3988292384 ^ c >>> 1 : c >>> 1;
@@ -3123,23 +3228,175 @@ function png(width, height) {
3123
3228
  header.set([8, 2, 0, 0, 0], 8);
3124
3229
  const rows = new Uint8Array((width * 3 + 1) * height).fill(255);
3125
3230
  for (let y = 0; y < height; y++) rows[y * (width * 3 + 1)] = 0;
3126
- const parts = [new Uint8Array([137, 80, 78, 71, 13, 10, 26, 10]), chunk("IHDR", header), chunk("IDAT", deflateSync(rows)), chunk("IEND", new Uint8Array())];
3127
- const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
3128
- let offset = 0;
3129
- for (const part of parts) {
3130
- out.set(part, offset);
3131
- offset += part.length;
3132
- }
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);
3133
3380
  return out;
3134
3381
  }
3135
3382
  var FakeFile = {
3136
- /** 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
+ */
3137
3389
  create(name, kilobytes = 0, mimeType) {
3138
- 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 });
3139
3392
  },
3140
- /** 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
+ */
3141
3397
  image(name, width = 10, height = 10) {
3142
- 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 });
3143
3400
  }
3144
3401
  };
3145
3402
  export {