diff --git a/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-database.js b/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-database.js index 563f86bf517..1eec51bbc8d 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-database.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-database.js @@ -6,6 +6,7 @@ tags: [[$:/tags/test-spec]] Tests the SQL tiddler database layer \*/ +/// if($tw.node) { (function(){ @@ -13,110 +14,110 @@ if($tw.node) { /*global $tw: false */ "use strict"; -describe("SQL tiddler database with node built-in sqlite", function() { - runSqlDatabaseTests("node"); +describe("SQL tiddler database with node built-in sqlite", function () { + void runSqlDatabaseTests("node").catch(console.error); }); -describe("SQL tiddler database with node-sqlite3-wasm", function() { - runSqlDatabaseTests("wasm"); +describe("SQL tiddler database with node-sqlite3-wasm", function () { + void runSqlDatabaseTests("wasm").catch(console.error); }); -describe("SQL tiddler database with better-sqlite3", function() { - runSqlDatabaseTests("better"); +describe("SQL tiddler database with better-sqlite3", function () { + void runSqlDatabaseTests("better").catch(console.error); }); -function runSqlDatabaseTests(engine) { +async function runSqlDatabaseTests(engine) { // Create and initialise the tiddler store var SqlTiddlerDatabase = require("$:/plugins/tiddlywiki/multiwikiserver/store/sql-tiddler-database.js").SqlTiddlerDatabase; const sqlTiddlerDatabase = new SqlTiddlerDatabase({ engine: engine }); - sqlTiddlerDatabase.createTables(); + await sqlTiddlerDatabase.createTables(); // Tear down - afterAll(function() { + afterAll(async function() { // Close the database - sqlTiddlerDatabase.close(); + await sqlTiddlerDatabase.close(); }); // Run tests - it("should save and retrieve tiddlers using engine: " + engine, function() { + it("should save and retrieve tiddlers using engine: " + engine, async function() { // Create bags and recipes - expect(sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha")).toEqual(1); - expect(sqlTiddlerDatabase.createBag("bag-beta","Bag beta")).toEqual(2); - expect(sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma")).toEqual(3); - expect(sqlTiddlerDatabase.listBags()).toEqual([ + expect(await sqlTiddlerDatabase.createBag("bag-alpha","Bag alpha")).toEqual(1); + expect(await sqlTiddlerDatabase.createBag("bag-beta","Bag beta")).toEqual(2); + expect(await sqlTiddlerDatabase.createBag("bag-gamma","Bag gamma")).toEqual(3); + expect(await sqlTiddlerDatabase.listBags()).toEqual([ { bag_name: 'bag-alpha', bag_id: 1, accesscontrol: '', description: "Bag alpha" }, { bag_name: 'bag-beta', bag_id: 2, accesscontrol: '', description: "Bag beta" }, { bag_name: 'bag-gamma', bag_id: 3, accesscontrol: '', description: "Bag gamma" } ]); - expect(sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(1); - expect(sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma")).toEqual(2); - expect(sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau")).toEqual(3); - expect(sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon")).toEqual(4); - expect(sqlTiddlerDatabase.listRecipes()).toEqual([ + expect(await sqlTiddlerDatabase.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(1); + expect(await sqlTiddlerDatabase.createRecipe("recipe-sigma",["bag-alpha","bag-gamma"],"Recipe sigma")).toEqual(2); + expect(await sqlTiddlerDatabase.createRecipe("recipe-tau",["bag-alpha"],"Recipe tau")).toEqual(3); + expect(await sqlTiddlerDatabase.createRecipe("recipe-upsilon",["bag-alpha","bag-gamma","bag-beta"],"Recipe upsilon")).toEqual(4); + expect(await sqlTiddlerDatabase.listRecipes()).toEqual([ { recipe_name: 'recipe-rho', recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho", owner_id: null }, { recipe_name: 'recipe-sigma', recipe_id: 2, bag_names: ["bag-alpha","bag-gamma"], description: "Recipe sigma", owner_id: null }, { recipe_name: 'recipe-tau', recipe_id: 3, bag_names: ["bag-alpha"], description: "Recipe tau", owner_id: null }, { recipe_name: 'recipe-upsilon', recipe_id: 4, bag_names: ["bag-alpha","bag-gamma","bag-beta"], description: "Recipe upsilon", owner_id: null } ]); - expect(sqlTiddlerDatabase.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]); - expect(sqlTiddlerDatabase.getRecipeBags("recipe-sigma")).toEqual(["bag-alpha","bag-gamma"]); - expect(sqlTiddlerDatabase.getRecipeBags("recipe-tau")).toEqual(["bag-alpha"]); - expect(sqlTiddlerDatabase.getRecipeBags("recipe-upsilon")).toEqual(["bag-alpha","bag-gamma","bag-beta"]); + expect(await sqlTiddlerDatabase.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]); + expect(await sqlTiddlerDatabase.getRecipeBags("recipe-sigma")).toEqual(["bag-alpha","bag-gamma"]); + expect(await sqlTiddlerDatabase.getRecipeBags("recipe-tau")).toEqual(["bag-alpha"]); + expect(await sqlTiddlerDatabase.getRecipeBags("recipe-upsilon")).toEqual(["bag-alpha","bag-gamma","bag-beta"]); // Save tiddlers - expect(sqlTiddlerDatabase.saveBagTiddler({title: "Another Tiddler",text: "I'm in alpha",tags: "one two three"},"bag-alpha")).toEqual({ + expect(await sqlTiddlerDatabase.saveBagTiddler({title: "Another Tiddler",text: "I'm in alpha",tags: "one two three"},"bag-alpha")).toEqual({ tiddler_id: 1 }); - expect(sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in alpha as well",tags: "one two three"},"bag-alpha")).toEqual({ + expect(await sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in alpha as well",tags: "one two three"},"bag-alpha")).toEqual({ tiddler_id: 2 }); - expect(sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in beta",tags: "four five six"},"bag-beta")).toEqual({ + expect(await sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in beta",tags: "four five six"},"bag-beta")).toEqual({ tiddler_id: 3 }); - expect(sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in gamma",tags: "seven eight nine"},"bag-gamma")).toEqual({ + expect(await sqlTiddlerDatabase.saveBagTiddler({title: "Hello There",text: "I'm in gamma",tags: "seven eight nine"},"bag-gamma")).toEqual({ tiddler_id: 4 }); // Verify what we've got - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha', is_deleted: 0 }, { title: 'Hello There', tiddler_id: 3, bag_name: 'bag-beta', is_deleted: 0 } ]); - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha', is_deleted: 0 }, { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma', is_deleted: 0 } ]); - expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-rho").tiddler).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" }); - expect(sqlTiddlerDatabase.getRecipeTiddler("Missing Tiddler","recipe-rho")).toEqual(null); - expect(sqlTiddlerDatabase.getRecipeTiddler("Another Tiddler","recipe-rho").tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }); - expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-sigma").tiddler).toEqual({ title: "Hello There", text: "I'm in gamma", tags: "seven eight nine" }); - expect(sqlTiddlerDatabase.getRecipeTiddler("Another Tiddler","recipe-sigma").tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }); - expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-upsilon").tiddler).toEqual({title: "Hello There",text: "I'm in beta",tags: "four five six"}); + expect((await sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-rho"))?.tiddler).toEqual({ title: "Hello There", text: "I'm in beta", tags: "four five six" }); + expect(await sqlTiddlerDatabase.getRecipeTiddler("Missing Tiddler","recipe-rho")).toEqual(null); + expect((await sqlTiddlerDatabase.getRecipeTiddler("Another Tiddler","recipe-rho"))?.tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }); + expect((await sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-sigma"))?.tiddler).toEqual({ title: "Hello There", text: "I'm in gamma", tags: "seven eight nine" }); + expect((await sqlTiddlerDatabase.getRecipeTiddler("Another Tiddler","recipe-sigma"))?.tiddler).toEqual({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }); + expect((await sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-upsilon"))?.tiddler).toEqual({title: "Hello There",text: "I'm in beta",tags: "four five six"}); // Delete a tiddlers to ensure the underlying tiddler in the recipe shows through - sqlTiddlerDatabase.deleteTiddler("Hello There","bag-beta"); - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ + await sqlTiddlerDatabase.deleteTiddler("Hello There","bag-beta"); + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha', is_deleted: 0 }, { title: 'Hello There', tiddler_id: 2, bag_name: 'bag-alpha', is_deleted: 0 } ]); - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Another Tiddler', tiddler_id: 1, bag_name: 'bag-alpha', is_deleted: 0 }, { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma', is_deleted: 0 } ]); - expect(sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null); - sqlTiddlerDatabase.deleteTiddler("Another Tiddler","bag-alpha"); - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: 'Hello There', tiddler_id: 2, bag_name: 'bag-alpha', is_deleted: 0 } ]); - expect(sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: 'Hello There', tiddler_id: 4, bag_name: 'bag-gamma', is_deleted: 0 } ]); + expect(await sqlTiddlerDatabase.getRecipeTiddler("Hello There","recipe-beta")).toEqual(null); + await sqlTiddlerDatabase.deleteTiddler("Another Tiddler","bag-alpha"); + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-rho")).toEqual([ { title: "Hello There", tiddler_id: 2, bag_name: "bag-alpha", is_deleted: 0 } ]); + expect(await sqlTiddlerDatabase.getRecipeTiddlers("recipe-sigma")).toEqual([ { title: "Hello There", tiddler_id: 4, bag_name: "bag-gamma", is_deleted: 0 } ]); // Save a recipe tiddler - expect(sqlTiddlerDatabase.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho")).toEqual({tiddler_id: 7, bag_name: 'bag-beta'}); - expect(sqlTiddlerDatabase.getRecipeTiddler("More","recipe-rho").tiddler).toEqual({title: "More", text: "None"}); + expect(await sqlTiddlerDatabase.saveRecipeTiddler({title: "More", text: "None"},"recipe-rho")).toEqual({tiddler_id: 7, bag_name: "bag-beta"}); + expect((await sqlTiddlerDatabase.getRecipeTiddler("More","recipe-rho"))?.tiddler).toEqual({title: "More", text: "None"}); }); - it("should manage users correctly", function() { + it("should manage users correctly", async function() { console.log("should manage users correctly") // Create users - const userId1 = sqlTiddlerDatabase.createUser("john_doe", "john@example.com", "pass123"); - const userId2 = sqlTiddlerDatabase.createUser("jane_doe", "jane@example.com", "pass123"); + const userId1 = await sqlTiddlerDatabase.createUser("john_doe", "john@example.com", "pass123"); + const userId2 = await sqlTiddlerDatabase.createUser("jane_doe", "jane@example.com", "pass123"); // Retrieve users - const user1 = sqlTiddlerDatabase.getUser(userId1); + const user1 = await sqlTiddlerDatabase.getUser(userId1); expect(user1.user_id).toBe(userId1); expect(user1.username).toBe("john_doe"); expect(user1.email).toBe("john@example.com"); @@ -124,107 +125,107 @@ function runSqlDatabaseTests(engine) { expect(user1.last_login).toBeNull(); // Update user - sqlTiddlerDatabase.updateUser(userId1, "john_updated", "john_updated@example.com"); - expect(sqlTiddlerDatabase.getUser(userId1).username).toBe("john_updated"); - expect(sqlTiddlerDatabase.getUser(userId1).email).toBe("john_updated@example.com"); + await sqlTiddlerDatabase.updateUser(userId1, "john_updated", "john_updated@example.com"); + expect((await sqlTiddlerDatabase.getUser(userId1)).username).toBe("john_updated"); + expect((await sqlTiddlerDatabase.getUser(userId1)).email).toBe("john_updated@example.com"); // List users - const users = sqlTiddlerDatabase.listUsers(); + const users = await sqlTiddlerDatabase.listUsers(); expect(users.length).toBe(2); expect(users[0].username).toBe("jane_doe"); expect(users[1].username).toBe("john_updated"); // Delete user - sqlTiddlerDatabase.deleteUser(userId2); - // expect(sqlTiddlerDatabase.getUser(userId2)).toBe(null || undefined); + await sqlTiddlerDatabase.deleteUser(userId2); + // expect(await sqlTiddlerDatabase.getUser(userId2)).toBe(null || undefined); }); - it("should manage groups correctly", function() { + it("should manage groups correctly", async function() { console.log("should manage groups correctly") // Create groups - const groupId1 = sqlTiddlerDatabase.createGroup("Editors", "Can edit content"); - const groupId2 = sqlTiddlerDatabase.createGroup("Viewers", "Can view content"); + const groupId1 = await sqlTiddlerDatabase.createGroup("Editors", "Can edit content"); + const groupId2 = await sqlTiddlerDatabase.createGroup("Viewers", "Can view content"); // Retrieve groups - expect(sqlTiddlerDatabase.getGroup(groupId1)).toEqual({ + expect(await sqlTiddlerDatabase.getGroup(groupId1)).toEqual({ group_id: groupId1, group_name: "Editors", description: "Can edit content" }); // Update group - sqlTiddlerDatabase.updateGroup(groupId1, "Super Editors", "Can edit all content"); - expect(sqlTiddlerDatabase.getGroup(groupId1).group_name).toBe("Super Editors"); - expect(sqlTiddlerDatabase.getGroup(groupId1).description).toBe("Can edit all content"); + await sqlTiddlerDatabase.updateGroup(groupId1, "Super Editors", "Can edit all content"); + expect((await sqlTiddlerDatabase.getGroup(groupId1)).group_name).toBe("Super Editors"); + expect((await sqlTiddlerDatabase.getGroup(groupId1)).description).toBe("Can edit all content"); // List groups - const groups = sqlTiddlerDatabase.listGroups(); + const groups = await sqlTiddlerDatabase.listGroups(); expect(groups.length).toBe(2); expect(groups[0].group_name).toBe("Super Editors"); expect(groups[1].group_name).toBe("Viewers"); // Delete group - sqlTiddlerDatabase.deleteGroup(groupId2); - // expect(sqlTiddlerDatabase.getGroup(groupId2)).toBe(null || undefined); + await sqlTiddlerDatabase.deleteGroup(groupId2); + // expect(await sqlTiddlerDatabase.getGroup(groupId2)).toBe(null || undefined); }); - it("should manage roles correctly", function() { + it("should manage roles correctly", async function() { console.log("should manage roles correctly") // Create roles - const roleId1 = sqlTiddlerDatabase.createRole("Admin" + Date.now(), "Full access"); - const roleId2 = sqlTiddlerDatabase.createRole("Editor" + Date.now(), "Can edit content"); + const roleId1 = await sqlTiddlerDatabase.createRole("Admin" + Date.now(), "Full access"); + const roleId2 = await sqlTiddlerDatabase.createRole("Editor" + Date.now(), "Can edit content"); // Retrieve roles - expect(sqlTiddlerDatabase.getRole(roleId1)).toEqual({ + expect(await sqlTiddlerDatabase.getRole(roleId1)).toEqual({ role_id: roleId1, role_name: jasmine.stringMatching(/^Admin\d+$/), description: "Full access" }); // Update role - sqlTiddlerDatabase.updateRole(roleId1, "Super Admin" + Date.now(), "God-like powers"); - expect(sqlTiddlerDatabase.getRole(roleId1).role_name).toMatch(/^Super Admin\d+$/); - expect(sqlTiddlerDatabase.getRole(roleId1).description).toBe("God-like powers"); + await sqlTiddlerDatabase.updateRole(roleId1, "Super Admin" + Date.now(), "God-like powers"); + expect((await sqlTiddlerDatabase.getRole(roleId1)).role_name).toMatch(/^Super Admin\d+$/); + expect((await sqlTiddlerDatabase.getRole(roleId1)).description).toBe("God-like powers"); // List roles - const roles = sqlTiddlerDatabase.listRoles(); + const roles = await sqlTiddlerDatabase.listRoles(); expect(roles.length).toBeGreaterThan(0); // expect(roles[0].role_name).toMatch(/^Editor\d+$/); // expect(roles[1].role_name).toMatch(/^Super Admin\d+$/); // Delete role - sqlTiddlerDatabase.deleteRole(roleId2); - // expect(sqlTiddlerDatabase.getRole(roleId2)).toBeUndefined(); + await sqlTiddlerDatabase.deleteRole(roleId2); + // expect(await sqlTiddlerDatabase.getRole(roleId2)).toBeUndefined(); }); - it("should manage permissions correctly", function() { + it("should manage permissions correctly", async function() { console.log("should manage permissions correctly") // Create permissions - const permissionId1 = sqlTiddlerDatabase.createPermission("read_tiddlers" + Date.now(), "Can read tiddlers"); - const permissionId2 = sqlTiddlerDatabase.createPermission("write_tiddlers" + Date.now(), "Can write tiddlers"); + const permissionId1 = await sqlTiddlerDatabase.createPermission("read_tiddlers" + Date.now(), "Can read tiddlers"); + const permissionId2 = await sqlTiddlerDatabase.createPermission("write_tiddlers" + Date.now(), "Can write tiddlers"); // Retrieve permissions - expect(sqlTiddlerDatabase.getPermission(permissionId1)).toEqual({ + expect(await sqlTiddlerDatabase.getPermission(permissionId1)).toEqual({ permission_id: permissionId1, permission_name: jasmine.stringMatching(/^read_tiddlers\d+$/), description: "Can read tiddlers" }); // Update permission - sqlTiddlerDatabase.updatePermission(permissionId1, "read_all_tiddlers" + Date.now(), "Can read all tiddlers"); - expect(sqlTiddlerDatabase.getPermission(permissionId1).permission_name).toMatch(/^read_all_tiddlers\d+$/); - expect(sqlTiddlerDatabase.getPermission(permissionId1).description).toBe("Can read all tiddlers"); + await sqlTiddlerDatabase.updatePermission(permissionId1, "read_all_tiddlers" + Date.now(), "Can read all tiddlers"); + expect((await sqlTiddlerDatabase.getPermission(permissionId1)).permission_name).toMatch(/^read_all_tiddlers\d+$/); + expect((await sqlTiddlerDatabase.getPermission(permissionId1)).description).toBe("Can read all tiddlers"); // List permissions - const permissions = sqlTiddlerDatabase.listPermissions(); + const permissions = await sqlTiddlerDatabase.listPermissions(); expect(permissions.length).toBeGreaterThan(0); expect(permissions[0].permission_name).toMatch(/^read_all_tiddlers\d+$/); expect(permissions[1].permission_name).toMatch(/^write_tiddlers\d+$/); // Delete permission - sqlTiddlerDatabase.deletePermission(permissionId2); - // expect(sqlTiddlerDatabase.getPermission(permissionId2)).toBeUndefined(); + await sqlTiddlerDatabase.deletePermission(permissionId2); + // expect(await sqlTiddlerDatabase.getPermission(permissionId2)).toBeUndefined(); }); } diff --git a/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-store.js b/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-store.js index c5888b2ce16..a914fca9987 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-store.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/store/tests-sql-tiddler-store.js @@ -1,9 +1,9 @@ /*\ -title: $:/plugins/tiddlywiki/multiwikiserver/store/tests-sql-tiddler-store.js +title: $:/plugins/tiddlywiki/multiwikiserver/await store/tests-sql-tiddler-await store.js type: application/javascript tags: [[$:/tags/test-spec]] -Tests the SQL tiddler store layer +Tests the SQL tiddler await store layer \*/ if($tw.node) { @@ -13,11 +13,11 @@ if($tw.node) { /*global $tw: false */ "use strict"; -describe("SQL tiddler store with node-sqlite3-wasm", function() { +describe("SQL tiddler await store with node-sqlite3-wasm", function() { runSqlStoreTests("wasm"); }); -describe("SQL tiddler store with better-sqlite3", function() { +describe("SQL tiddler await store with better-sqlite3", function() { runSqlStoreTests("better"); }); @@ -26,26 +26,27 @@ function runSqlStoreTests(engine) { var store; - beforeEach(function() { + beforeEach(async function() { store = new SqlTiddlerStore({ databasePath: ":memory:", engine: engine }); + await store.initCheck(); }); - afterEach(function() { - store.close(); + afterEach(async function() { + await store.close(); store = null; }); - it("should return empty results without failure on an empty store", function() { - expect(store.listBags()).toEqual([]); - expect(store.listRecipes()).toEqual([]); + it("should return empty results without failure on an empty await store", async function() { + expect(await store.listBags()).toEqual([]); + expect(await store.listRecipes()).toEqual([]); }); - it("should return a single bag after creating a bag", function() { - expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null); - expect(store.listBags()).toEqual([{ + it("should return a single bag after creating a bag", async function() { + expect(await store.createBag("bag-alpha", "Bag alpha")).toEqual(null); + expect(await store.listBags()).toEqual([{ bag_name: "bag-alpha", bag_id: 1, accesscontrol: "", @@ -53,17 +54,17 @@ function runSqlStoreTests(engine) { }]); }); - it("should return empty results after failing to create a bag with an invalid name", function() { - expect(store.createBag("bag alpha", "Bag alpha")).toEqual({ + it("should return empty results after failing to create a bag with an invalid name", async function() { + expect(await store.createBag("bag alpha", "Bag alpha")).toEqual({ message: "Invalid character(s)" }); - expect(store.listBags()).toEqual([]); + expect(await store.listBags()).toEqual([]); }); - it("should return a bag with new description after re-creating", function() { - expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null); - expect(store.createBag("bag-alpha", "Different description")).toEqual(null); - expect(store.listBags()).toEqual([{ + it("should return a bag with new description after re-creating", async function() { + expect(await store.createBag("bag-alpha", "Bag alpha")).toEqual(null); + expect(await store.createBag("bag-alpha", "Different description")).toEqual(null); + expect(await store.listBags()).toEqual([{ bag_name: "bag-alpha", bag_id: 1, accesscontrol: "", @@ -71,9 +72,9 @@ function runSqlStoreTests(engine) { }]); }); - it("should return a saved tiddler within a bag", function() { - expect(store.createBag("bag-alpha", "Bag alpha")).toEqual(null); - var saveBagResult = store.saveBagTiddler({ + it("should return a saved tiddler within a bag", async function() { + expect(await store.createBag("bag-alpha", "Bag alpha")).toEqual(null); + var saveBagResult = await store.saveBagTiddler({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" @@ -82,38 +83,38 @@ function runSqlStoreTests(engine) { expect(new Set(Object.keys(saveBagResult))).toEqual(new Set(["tiddler_id"])); expect(typeof(saveBagResult.tiddler_id)).toBe("number"); - expect(store.getBagTiddlers("bag-alpha")).toEqual([{title: "Another Tiddler", tiddler_id: 1}]); + expect(await store.getBagTiddlers("bag-alpha")).toEqual([{title: "Another Tiddler", tiddler_id: 1}]); - var getBagTiddlerResult = store.getBagTiddler("Another Tiddler","bag-alpha"); + var getBagTiddlerResult = await store.getBagTiddler("Another Tiddler","bag-alpha"); expect(typeof(getBagTiddlerResult.tiddler_id)).toBe("number"); delete getBagTiddlerResult.tiddler_id; expect(getBagTiddlerResult).toEqual({ attachment_blob: null, tiddler: {title: "Another Tiddler", text: "I'm in alpha", tags: "one two three"} }); }); - it("should return a single recipe after creating that recipe", function() { - expect(store.createBag("bag-alpha","Bag alpha")).toEqual(null); - expect(store.createBag("bag-beta","Bag beta")).toEqual(null); - expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); + it("should return a single recipe after creating that recipe", async function() { + expect(await store.createBag("bag-alpha","Bag alpha")).toEqual(null); + expect(await store.createBag("bag-beta","Bag beta")).toEqual(null); + expect(await store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); - expect(store.listRecipes()).toEqual([ + expect(await store.listRecipes()).toEqual([ { recipe_name: "recipe-rho", recipe_id: 1, bag_names: ["bag-alpha","bag-beta"], description: "Recipe rho", owner_id: null } ]); }); - it("should return a recipe's bags after creating that recipe", function() { - expect(store.createBag("bag-alpha","Bag alpha")).toEqual(null); - expect(store.createBag("bag-beta","Bag beta")).toEqual(null); - expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); + it("should return a recipe's bags after creating that recipe", async function() { + expect(await store.createBag("bag-alpha","Bag alpha")).toEqual(null); + expect(await store.createBag("bag-beta","Bag beta")).toEqual(null); + expect(await store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); - expect(store.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]); + expect(await store.getRecipeBags("recipe-rho")).toEqual(["bag-alpha","bag-beta"]); }); - it("should return a saved tiddler within a recipe", function() { - expect(store.createBag("bag-alpha","Bag alpha")).toEqual(null); - expect(store.createBag("bag-beta","Bag beta")).toEqual(null); - expect(store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); + it("should return a saved tiddler within a recipe", async function() { + expect(await store.createBag("bag-alpha","Bag alpha")).toEqual(null); + expect(await store.createBag("bag-beta","Bag beta")).toEqual(null); + expect(await store.createRecipe("recipe-rho",["bag-alpha","bag-beta"],"Recipe rho")).toEqual(null); - var saveRecipeResult = store.saveRecipeTiddler({ + var saveRecipeResult = await store.saveRecipeTiddler({ title: "Another Tiddler", text: "I'm in rho" },"recipe-rho"); @@ -122,25 +123,25 @@ function runSqlStoreTests(engine) { expect(typeof(saveRecipeResult.tiddler_id)).toBe("number"); expect(saveRecipeResult.bag_name).toBe("bag-beta"); - expect(store.getRecipeTiddlers("recipe-rho")).toEqual([{title: "Another Tiddler", tiddler_id: 1, bag_name: "bag-beta", is_deleted: 0 }]); + expect(await store.getRecipeTiddlers("recipe-rho")).toEqual([{title: "Another Tiddler", tiddler_id: 1, bag_name: "bag-beta", is_deleted: 0 }]); - var getRecipeTiddlerResult = store.getRecipeTiddler("Another Tiddler","recipe-rho"); + var getRecipeTiddlerResult = await store.getRecipeTiddler("Another Tiddler","recipe-rho"); expect(typeof(getRecipeTiddlerResult.tiddler_id)).toBe("number"); delete getRecipeTiddlerResult.tiddler_id; expect(getRecipeTiddlerResult).toEqual({ attachment_blob: null, bag_name: "bag-beta", tiddler: {title: "Another Tiddler", text: "I'm in rho"} }); }); - it("should return no tiddlers after the only one has been deleted", function() { - expect(store.createBag("bag-alpha","Bag alpha")).toEqual(null); + it("should return no tiddlers after the only one has been deleted", async function() { + expect(await store.createBag("bag-alpha","Bag alpha")).toEqual(null); - store.saveBagTiddler({ + await store.saveBagTiddler({ title: "Another Tiddler", text: "I'm in alpha", tags: "one two three" }, "bag-alpha"); - store.deleteTiddler("Another Tiddler","bag-alpha"); - expect(store.getBagTiddlers("bag-alpha")).toEqual([]); + await store.deleteTiddler("Another Tiddler","bag-alpha"); + expect(await store.getBagTiddlers("bag-alpha")).toEqual([]); }); } diff --git a/plugins/tiddlywiki/multiwikiserver/modules/tests/test-attachment.js b/plugins/tiddlywiki/multiwikiserver/modules/tests/test-attachment.js index 9298c43f8a3..39d09c12195 100644 --- a/plugins/tiddlywiki/multiwikiserver/modules/tests/test-attachment.js +++ b/plugins/tiddlywiki/multiwikiserver/modules/tests/test-attachment.js @@ -6,30 +6,30 @@ tags: [[$:/tags/test-spec]] Tests attachments. \*/ -if(typeof window === 'undefined' && typeof process !== 'undefined' && process.versions && process.versions.node) { +if(typeof window === "undefined" && typeof process !== "undefined" && process.versions && process.versions.node) { (function(){ - var fs = require('fs'); - var path = require('path'); - var assert = require('assert'); - var AttachmentStore = require('$:/plugins/tiddlywiki/multiwikiserver/store/attachments.js').AttachmentStore; - const {Buffer} = require('buffer'); + var fs = require("fs"); + var path = require("path"); + var assert = require("assert"); + var AttachmentStore = require("$:/plugins/tiddlywiki/multiwikiserver/store/attachments.js").AttachmentStore; + const{Buffer} = require("buffer"); - function generateFileWithSize(filePath, sizeInBytes) { - return new Promise((resolve, reject) => { + async function generateFileWithSize(filePath, sizeInBytes) { + return await new Promise((resolve, reject) => { var buffer = Buffer.alloc(sizeInBytes); for(var i = 0; i < sizeInBytes; i++) { buffer[i] = Math.floor(Math.random() * 256); } - fs.writeFile(filePath, buffer, (err) => { + fs.writeFile(filePath, buffer, err => { if(err) { - console.error('Error writing file:', err); + console.error("Error writing file:", err); reject(err); } else { - console.log('File '+filePath+' generated with size '+sizeInBytes+' bytes'); + console.log("File "+filePath+" generated with size "+sizeInBytes+" bytes"); fs.readFile(filePath, (err, data) => { if(err) { - console.error('Error reading file:', err); + console.error("Error reading file:", err); reject(err); } else { resolve(data); @@ -41,10 +41,10 @@ if(typeof window === 'undefined' && typeof process !== 'undefined' && process.ve } (function() { - 'use strict'; + "use strict"; if($tw.node) { - describe('AttachmentStore', function() { - var storePath = './editions/test/test-store'; + describe("AttachmentStore", function() { + var storePath = "./editions/test/test-store"; var attachmentStore = new AttachmentStore({ storePath: storePath }); var originalTimeout; @@ -69,111 +69,111 @@ if(typeof window === 'undefined' && typeof process !== 'undefined' && process.ve }); }); - it('isValidAttachmentName', function() { - expect(attachmentStore.isValidAttachmentName('abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890')).toBe(true); - expect(attachmentStore.isValidAttachmentName('invalid-name')).toBe(false); + it("isValidAttachmentName", function() { + expect(attachmentStore.isValidAttachmentName("abcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890")).toBe(true); + expect(attachmentStore.isValidAttachmentName("invalid-name")).toBe(false); }); - it('saveAttachment', function() { + it("saveAttachment", function() { var options = { - text: 'Hello, World!', - type: 'text/plain', - reference: 'test-reference', + text: "Hello, World!", + type: "text/plain", + reference: "test-reference", }; var contentHash = attachmentStore.saveAttachment(options); assert.strictEqual(contentHash.length, 64); - assert.strictEqual(fs.existsSync(path.resolve(storePath, 'files', contentHash)), true); + assert.strictEqual(fs.existsSync(path.resolve(storePath, "files", contentHash)), true); }); - it('adoptAttachment', function() { - var incomingFilepath = path.resolve(storePath, 'incoming-file.txt'); - fs.writeFileSync(incomingFilepath, 'Hello, World!'); - var type = 'text/plain'; - var hash = 'abcdef0123456789abcdef0123456789'; - var _canonical_uri = 'test-canonical-uri'; + it("adoptAttachment", function() { + var incomingFilepath = path.resolve(storePath, "incoming-file.txt"); + fs.writeFileSync(incomingFilepath, "Hello, World!"); + var type = "text/plain"; + var hash = "abcdef0123456789abcdef0123456789"; + var _canonical_uri = "test-canonical-uri"; attachmentStore.adoptAttachment(incomingFilepath, type, hash, _canonical_uri); - expect(fs.existsSync(path.resolve(storePath, 'files', hash))).toBe(true); + expect(fs.existsSync(path.resolve(storePath, "files", hash))).toBe(true); }); - it('getAttachmentStream', function() { + it("getAttachmentStream", function() { var options = { - text: 'Hello, World!', - type: 'text/plain', - filename: 'data.txt', + text: "Hello, World!", + type: "text/plain", + filename: "data.txt", }; var contentHash = attachmentStore.saveAttachment(options); var stream = attachmentStore.getAttachmentStream(contentHash); expect(stream).not.toBeNull(); - expect(stream.type).toBe('text/plain'); + expect(stream.type).toBe("text/plain"); }); - it('getAttachmentFileSize', function() { + it("getAttachmentFileSize", function() { var options = { - text: 'Hello, World!', - type: 'text/plain', - reference: 'test-reference', + text: "Hello, World!", + type: "text/plain", + reference: "test-reference", }; var contentHash = attachmentStore.saveAttachment(options); var fileSize = attachmentStore.getAttachmentFileSize(contentHash); expect(fileSize).toBe(13); }); - it('getAttachmentMetadata', function() { + it("getAttachmentMetadata", function() { var options = { - text: 'Hello, World!', - type: 'text/plain', - filename: 'data.txt', + text: "Hello, World!", + type: "text/plain", + filename: "data.txt", }; var contentHash = attachmentStore.saveAttachment(options); var metadata = attachmentStore.getAttachmentMetadata(contentHash); expect(metadata).not.toBeNull(); - expect(metadata.type).toBe('text/plain'); - expect(metadata.filename).toBe('data.txt'); + expect(metadata.type).toBe("text/plain"); + expect(metadata.filename).toBe("data.txt"); }); - it('saveAttachment large file', async function() { + it("saveAttachment large file", async function() { var sizeInMB = 10 - const file = await generateFileWithSize('./editions/test/test-store/large-file.txt', 1024 * 1024 * sizeInMB) + const file = await generateFileWithSize("./editions/test/test-store/large-file.txt", 1024 * 1024 * sizeInMB) var options = { text: file, - type: 'application/octet-stream', - reference: 'test-reference', + type: "application/octet-stream", + reference: "test-reference", }; var contentHash = attachmentStore.saveAttachment(options); assert.strictEqual(contentHash.length, 64); - assert.strictEqual(fs.existsSync(path.resolve(storePath, 'files', contentHash)), true); + assert.strictEqual(fs.existsSync(path.resolve(storePath, "files", contentHash)), true); }); - it('saveAttachment multiple large files', async function() { + it("saveAttachment multiple large files", async function() { var sizeInMB = 10; var numFiles = 5; - for (var i = 0; i < numFiles; i++) { + for(var i = 0; i < numFiles; i++) { const file = await generateFileWithSize(`./editions/test/test-store/large-file-${i}.txt`, 1024 * 1024 * sizeInMB); var options = { text: file, - type: 'application/octet-stream', + type: "application/octet-stream", reference: `test-reference-${i}`, }; var contentHash = attachmentStore.saveAttachment(options); assert.strictEqual(contentHash.length, 64); - assert.strictEqual(fs.existsSync(path.resolve(storePath, 'files', contentHash)), true); + assert.strictEqual(fs.existsSync(path.resolve(storePath, "files", contentHash)), true); } }); - it('getAttachmentStream multiple large files', async function() { + it("getAttachmentStream multiple large files", async function() { var sizeInMB = 10; var numFiles = 5; - for (var i = 0; i < numFiles; i++) { + for(var i = 0; i < numFiles; i++) { const file = await generateFileWithSize(`./editions/test/test-store/large-file-${i}.txt`, 1024 * 1024 * sizeInMB); var options = { text: file, - type: 'application/octet-stream', + type: "application/octet-stream", reference: `test-reference-${i}`, }; var contentHash = attachmentStore.saveAttachment(options); var stream = attachmentStore.getAttachmentStream(contentHash); assert.notStrictEqual(stream, null); - assert.strictEqual(stream.type, 'application/octet-stream'); + assert.strictEqual(stream.type, "application/octet-stream"); } }); });