import { describe, it, expect } from "vitest"; import utils from "./utils.js"; type TestCase any> = [desc: string, fnParams: Parameters, expected: ReturnType]; describe("#newEntityId", () => { it("should return a string with a length of 12", () => { const result = utils.newEntityId(); expect(result).toBeTypeOf("string"); expect(result).toHaveLength(12); }); }); describe("#randomString", () => { it("should return a string with a length as per argument", () => { const stringLength = 5; const result = utils.randomString(stringLength); expect(result).toBeTypeOf("string"); expect(result).toHaveLength(stringLength); }); }); describe.todo("#randomSecureToken", () => {}); describe.todo("#md5", () => {}); describe.todo("#hashedBlobId", () => {}); describe.todo("#toBase64", () => {}); describe.todo("#fromBase64", () => {}); describe.todo("#hmac", () => {}); describe.todo("#isElectron", () => {}); describe.todo("#hash", () => {}); describe("#isEmptyOrWhitespace", () => { const testCases: TestCase[] = [ ["w/ 'null' it should return true", [null], true], ["w/ 'null' it should return true", [null], true], ["w/ undefined it should return true", [undefined], true], ["w/ empty string '' it should return true", [""], true], ["w/ single whitespace string ' ' it should return true", [" "], true], ["w/ multiple whitespace string ' ' it should return true", [" "], true], ["w/ non-empty string ' t ' it should return false", [" t "], false], ]; testCases.forEach(testCase => { const [desc, fnParams, expected] = testCase; it(desc, () => { const result = utils.isEmptyOrWhitespace(...fnParams); expect(result).toStrictEqual(expected); }) }) }); describe("#sanitizeSqlIdentifier", () => { const testCases: TestCase[] = [ ["w/ 'test' it should not strip anything", ["test"], "test"], ["w/ 'test123' it should not strip anything", ["test123"], "test123"], ["w/ 'tEst_TeSt' it should not strip anything", ["tEst_TeSt"], "tEst_TeSt"], ["w/ 'test_test' it should not strip '_'", ["test_test"], "test_test"], ["w/ 'test-' it should strip the '-'", ["test-"], "test"], ["w/ 'test-test' it should strip the '-'", ["test-test"], "testtest"], ["w/ 'test; --test' it should strip the '; --'", ["test; --test"], "testtest"], ["w/ 'test test' it should strip the ' '", ["test test"], "testtest"], ]; testCases.forEach(testCase => { const [desc, fnParams, expected] = testCase; it(desc, () => { const result = utils.sanitizeSqlIdentifier(...fnParams); expect(result).toStrictEqual(expected); }) }); }); describe.todo("#escapeHtml", () => {}); describe.todo("#unescapeHtml", () => {}); describe.todo("#toObject", () => {}); describe.todo("#stripTags", () => {}); describe.todo("#union", () => {}); describe.todo("#escapeRegExp", () => {}); describe.todo("#crash", () => {}); describe.todo("#sanitizeFilenameForHeader", () => {}); describe.todo("#getContentDisposition", () => {}); describe.todo("#isStringNote", () => {}); describe.todo("#quoteRegex", () => {}); describe.todo("#replaceAll", () => {}); // TriliumNextTODO move existing formatDownloadTitle in here // describe.todo("#formatDownloadTitle", () => {}); describe.todo("#removeTextFileExtension", () => {}); describe.todo("#getNoteTitle", () => {}); describe.todo("#timeLimit", () => {}); describe.todo("#deferred", () => {}); describe.todo("#removeDiacritic", () => {}); describe.todo("#normalize", () => {}); describe.todo("#toMap", () => {}); describe.todo("#isString", () => {}); describe.todo("#getResourceDir", () => {}); describe.todo("#isMac", () => {}); describe.todo("#isWindows", () => {}); describe("#formatDownloadTitle", () => { //prettier-ignore const testCases: [fnValue: Parameters, expectedValue: ReturnType][] = [ // empty fileName tests [ ["", "text", ""], "untitled.html" ], [ ["", "canvas", ""], "untitled.json" ], [ ["", null, ""], "untitled" ], // json extension from type tests [ ["test_file", "canvas", ""], "test_file.json" ], [ ["test_file", "relationMap", ""], "test_file.json" ], [ ["test_file", "search", ""], "test_file.json" ], // extension based on mime type [ ["test_file", null, "text/csv"], "test_file.csv" ], [ ["test_file_wo_ext", "image", "image/svg+xml"], "test_file_wo_ext.svg" ], [ ["test_file_wo_ext", "file", "application/json"], "test_file_wo_ext.json" ], [ ["test_file_w_fake_ext.ext", "image", "image/svg+xml"], "test_file_w_fake_ext.ext.svg" ], [ ["test_file_w_correct_ext.svg", "image", "image/svg+xml"], "test_file_w_correct_ext.svg" ], [ ["test_file_w_correct_ext.svgz", "image", "image/svg+xml"], "test_file_w_correct_ext.svgz" ], [ ["test_file.zip", "file", "application/zip"], "test_file.zip" ], [ ["test_file", "file", "application/zip"], "test_file.zip" ], // application/octet-stream tests [ ["test_file", "file", "application/octet-stream"], "test_file" ], [ ["test_file.zip", "file", "application/octet-stream"], "test_file.zip" ], [ ["test_file.unknown", null, "application/octet-stream"], "test_file.unknown" ], // sanitized filename tests [ ["test/file", null, "application/octet-stream"], "testfile" ], [ ["test:file.zip", "file", "application/zip"], "testfile.zip" ], [ [":::", "file", "application/zip"], ".zip" ], [ [":::a", "file", "application/zip"], "a.zip" ] ]; testCases.forEach((testCase) => { const [fnParams, expected] = testCase; return it(`With args '${JSON.stringify(fnParams)}', it should return '${expected}'`, () => { const actual = utils.formatDownloadTitle(...fnParams); expect(actual).toStrictEqual(expected); }); }); });