UNPKG

@daiso-tech/core

Version:

The library offers flexible, framework-agnostic solutions for modern web applications, built on adaptable components that integrate seamlessly with popular frameworks like Next Js.

1,085 lines 75.5 kB
/** * @module FileStorage */ import {} from "vitest"; import {} from "../../../execution-context/contracts/_module.js"; import { NoOpExecutionContextAdapter } from "../../../execution-context/implementations/adapters/no-op-execution-context-adapter/_module.js"; import { ExecutionContext } from "../../../execution-context/implementations/derivables/_module.js"; import { FILE_WRITE_ENUM, } from "../../../file-storage/contracts/_module.js"; import { isUint8ByteArrayEqualityTester, resolveStream, } from "../../../test-utilities/_module.js"; import {} from "../../../utilities/_module.js"; /** * The `fileStorageAdapterTestSuite` function simplifies the process of testing your custom implementation of {@link IFileStorageAdapter | `IFileStorageAdapter`} with `vitest`. * * IMPORT_PATH: `"@daiso-tech/core/file-storage/test-utilities"` * @group TestUtilities */ export function fileStorageAdapterTestSuite(settings) { const { expect, test, createAdapter, describe, beforeEach: beforeEach_, enableGetMetaData = true, context = new ExecutionContext(new NoOpExecutionContextAdapter()), } = settings; let adapter; beforeEach_(async () => { adapter = await createAdapter(); }); describe("IFileStorageAdapter tests:", () => { expect.addEqualityTesters([isUint8ByteArrayEqualityTester]); describe("method: exists", () => { test("Should return false when key does not exists", async () => { const noneExistingKey = "a"; const result = await adapter.exists(context, noneExistingKey); expect(result).toBe(false); }); test("Should return true when key exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.exists(context, key); expect(result).toBe(true); }); }); describe("method: getStream", () => { test("Should return null when key does not exists", async () => { const noneExistingKey = "a"; const result = await adapter.getStream(context, noneExistingKey); expect(result).toBeNull(); }); test("Should return AsyncIterable<Uint8Array> when key exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await resolveStream(await adapter.getStream(context, key)); expect(result).toEqual(data); }); }); describe("method: getBytes", () => { test("Should return null when key does not exists", async () => { const noneExistingKey = "a"; const result = await adapter.getBytes(context, noneExistingKey); expect(result).toBeNull(); }); test("Should return Uint8Array when key exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); }); describe.skipIf(!enableGetMetaData)("method: getMetaData", () => { test("Should return null when key does not exists", async () => { const noneExistingKey = "a"; const result = await adapter.getMetaData(context, noneExistingKey); expect(result).toBeNull(); }); test("Should return initial metadata with a null updatedAt after add method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return initial metadata with a null updatedAt after put method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.put(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after add method followed by update method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.update(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after put method followed by update method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.put(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.update(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after put overwrites an existing file", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.put(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.put(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return initial metadata with a null updatedAt after addStream method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return initial metadata with a null updatedAt after putStream method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after addStream method followed by updateStream method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after putStream method followed by updateStream method", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); test("Should return metadata with a valid updatedAt after putStream overwrites an existing file", async () => { const key = "a.txt"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "text/plain"; await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newContentType = "application/octet-stream"; const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType: newContentType, fileSizeInBytes: newData.length, }); const result = await adapter.getMetaData(context, key); expect(result).toEqual({ etag: expect.any(String), contentType: newContentType, fileSizeInBytes: data.byteLength, updatedAt: expect.any(Date), }); }); }); describe("method: add", () => { test("Should return true when key does not exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should return false when key exists", async () => { const key = "a"; const contentType = "application/octet-stream"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8")); const result = await adapter.add(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should persist data when key does not exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); test("Should not persist data when key exists", async () => { const key = "a"; const contentType = "application/octet-stream"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8")); await adapter.add(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); }); describe("method: addStream", () => { test("Should return true when key does not exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should return false when key exists", async () => { const key = "a"; const contentType = "application/octet-stream"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8")); const result = await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should persist data when key does not exists", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); test("Should not persist data when key exists", async () => { const key = "a"; const contentType = "application/octet-stream"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("NEW_CONTENT", "utf8")); await adapter.addStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); }); describe("method: update", () => { test("Should return false when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.update(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should not persist data when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.update(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toBeNull(); }); test("Should return true when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const result = await adapter.update(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should persist data when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.update(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(newData); }); }); describe("method: updateStream", () => { test("Should return false when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should not persist data when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toBeNull(); }); test("Should return true when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const result = await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should persist data when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.updateStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(newData); }); }); describe("method: put", () => { test("Should return false when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.put(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should persist data when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.put(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); test("Should return true when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const result = await adapter.put(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should persist data when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.put(context, key, { data: newData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(newData); }); }); describe("method: putStream", () => { test("Should return false when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; const result = await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(false); }); test("Should persist data when key doesnt exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(data); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(data); }); test("Should return true when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const result = await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); expect(result).toBe(true); }); test("Should persist data when key exist", async () => { const key = "a"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, key, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const newData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.putStream(context, key, { data: { async *[Symbol.asyncIterator]() { yield Promise.resolve(newData); }, }, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.getBytes(context, key); expect(result).toEqual(newData); }); }); describe("method: copy", () => { test("Should return FILE_WRITE_ENUM.NOT_FOUND when source does not exists and destination does not exists", async () => { const noneExistingSource = "a"; const noneExistingDestination = "c"; const result = await adapter.copy(context, noneExistingSource, noneExistingDestination); expect(result).toBe(FILE_WRITE_ENUM.NOT_FOUND); }); test("Should return FILE_WRITE_ENUM.NOT_FOUND when source does not exists and destination exists", async () => { const noneExistingSource = "a"; const destination = "c"; const data = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, destination, { data, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: data.length, }); const result = await adapter.copy(context, noneExistingSource, destination); expect(result).toBe(FILE_WRITE_ENUM.NOT_FOUND); }); test("Should return FILE_WRITE_ENUM.KEY_EXISTS when source exists and destination exists", async () => { const source = "a"; const sourceData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, source, { data: sourceData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: sourceData.length, }); const destination = "c"; const destinationData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.add(context, destination, { data: destinationData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: destinationData.length, }); const result = await adapter.copy(context, source, destination); expect(result).toBe(FILE_WRITE_ENUM.KEY_EXISTS); }); test("Should not persist when source exists and destination exists", async () => { const source = "a"; const sourceData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, source, { data: sourceData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: sourceData.length, }); const destination = "c"; const destinationData = new Uint8Array(Buffer.from("CONTENT", "utf8")); await adapter.add(context, destination, { data: destinationData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: destinationData.length, }); await adapter.copy(context, source, destination); const result = await adapter.getBytes(context, destination); expect(result).toEqual(destinationData); }); test("Should return FILE_WRITE_ENUM.SUCCESS when source exists and destination does not exists", async () => { const source = "a"; const sourceData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, source, { data: sourceData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: sourceData.length, }); const destination = "c"; const result = await adapter.copy(context, source, destination); expect(result).toBe(FILE_WRITE_ENUM.SUCCESS); }); test("Should persist data when source exists and destination does not exists", async () => { const source = "a"; const sourceData = new Uint8Array(Buffer.from("CONTENT", "utf8")); const contentType = "application/octet-stream"; await adapter.add(context, source, { data: sourceData, cacheControl: null, contentDisposition: null, contentEncoding: null, contentLanguage: null, contentType, fileSizeInBytes: sourceData.length, }); const destination = "c"; await adapter.copy(context, source, destination); const result = await adapter.getBytes(context, destination); expect(result).toEqual(sourceData); }); }); describe("method: copyAndReplace", () => { test("Should return false when source does