Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wip: test preload files #199

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ The app is a typical Electron dev setup, excepting the use of [esbuild][1].
yarn

# start the development build and watch script
yarn start
yarn dev

# If error with sqlite library versions
yarn run electron-rebuild
Expand Down
9 changes: 5 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
"lint:prettier:write": "prettier . --write",
"lint:types:check": "tsc --noEmit --skipLibCheck",
"postinstall": "yarn run electron-rebuild",
"prestart": "tailwindcss -i ./src/index.css -o ./src/index-compiled.css",
"predev": "tailwindcss -i ./src/index.css -o ./src/index-compiled.css",
"prebuild": "yarn run lint && tailwindcss -i ./src/index.css -o ./src/index-compiled.css",
"start": "node ./scripts/dev.js",
"pretest": "node ./scripts/test.mjs",
"test": "mocha 'src/**/*.test.bundle.js'"
"dev": "node ./scripts/dev.js",
"pretest": "node ./scripts/pretest.mjs",
"test": "electron-mocha --renderer 'src/**/*.test.bundle.js'"
},
"dependencies": {
"ajv": "^8.6.2",
Expand Down Expand Up @@ -50,6 +50,7 @@
"class-variance-authority": "^0.7.0",
"date-fns": "^3.3.1",
"electron": "^28.2.0",
"electron-mocha": "^12.3.0",
"emotion": "^10.0.27",
"esbuild": "^0.20.0",
"evergreen-ui": "^7.1.9",
Expand Down
50 changes: 50 additions & 0 deletions scripts/pretest.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import esbuild from "esbuild";
import fs from "fs";
import path from "path";

// Function to find test files
function findTestFiles(dir, files = [], ignorePreload = false) {
fs.readdirSync(dir, { withFileTypes: true }).forEach((dirent) => {
const res = path.resolve(dir, dirent.name);
if (dirent.isDirectory()) {
if (!(ignorePreload && res.includes("src/preload"))) {
findTestFiles(res, files, ignorePreload);
}
} else if (dirent.name.endsWith(".test.ts")) {
files.push(res);
}
});
return files;
}

// Find browser test files, excluding preload directory
const browserTestFiles = findTestFiles("src", [], true);
console.log(browserTestFiles);

// Find preload test files
const preloadTestFiles = findTestFiles("src/preload");
console.log(preloadTestFiles);

// Bundle browser test files
browserTestFiles.forEach(async (file) => {
await esbuild.build({
entryPoints: [file],
outfile: file.replace(".test.ts", ".test.bundle.js"),
bundle: true,
platform: "node",
external: ["mocha"],
plugins: [],
});
});

// Bundle preload test files
preloadTestFiles.forEach(async (file) => {
await esbuild.build({
entryPoints: [file],
outfile: file.replace(".test.ts", ".test.bundle.js"),
bundle: true,
platform: "node",
external: ["knex", "electron", "electron-store", "better-sqlite3", "mocha"],
plugins: [],
});
});
79 changes: 40 additions & 39 deletions scripts/production.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,48 @@
const fs = require("fs");
const path = require("path");
const esbuild = require("esbuild");

// After successful build, log results
function afterBuild(name) {
return {
name: `after-build-${name}`,
setup(build) {
build.onEnd((result) => {
if (result.errors.length) {
console.error(`${name} bundle completed with errors`, errors);
process.exit(1);
} else {
console.log(`${name} bundle completed`);
}
});
},
};
// Function to find test files
function findTestFiles(dir, files = [], ignorePreload = false) {
fs.readdirSync(dir, { withFileTypes: true }).forEach((dirent) => {
const res = path.resolve(dir, dirent.name);
if (dirent.isDirectory()) {
if (!(ignorePreload && res.includes("src/preload"))) {
findTestFiles(res, files, ignorePreload);
}
} else if (dirent.name.endsWith(".test.ts")) {
files.push(res);
}
});
return files;
}

// build renderer bundle
esbuild.build({
entryPoints: ["src/index.tsx"],
outfile: "src/renderer.bundle.js",
bundle: true,
platform: "browser",
plugins: [afterBuild("renderer")],
});
// Find browser test files, excluding preload directory
const browserTestFiles = findTestFiles("src", [], true);

// Find preload test files
const preloadTestFiles = findTestFiles("src/preload");

// build preload bundle
esbuild.build({
entryPoints: ["src/preload/index.ts"],
outfile: "src/preload.bundle.js",
bundle: true,
platform: "node",
external: ["knex", "electron", "electron-store", "better-sqlite3"],
plugins: [afterBuild("preload")],
// Bundle browser test files
browserTestFiles.forEach(async (file) => {
await esbuild.build({
entryPoints: [file],
outfile: file.replace(".test.ts", ".test.bundle.js"),
bundle: true,
platform: "node", // NOTE: this differs from the build script, which uses "browser", b/c we're running tests in Node
external: ["mocha"],
plugins: [],
});
});

// build electron main bundle
esbuild.build({
entryPoints: ["src/electron/index.js"],
outfile: "src/main.bundle.js",
bundle: true,
platform: "node",
external: ["electron", "electron-store", "better-sqlite3"],
plugins: [afterBuild("main")],
// Bundle preload test files
preloadTestFiles.forEach(async (file) => {
await esbuild.build({
entryPoints: [file],
outfile: file.replace(".test.ts", ".test.bundle.js"),
bundle: true,
platform: "node",
external: ["knex", "electron", "electron-store", "better-sqlite3"],
plugins: [], // Include any necessary plugins here
});
});
30 changes: 0 additions & 30 deletions scripts/test.mjs

This file was deleted.

53 changes: 53 additions & 0 deletions src/preload/client/documents.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { test, suite, before } from "mocha";
import { create } from "../client";

// migrate a temporary db file
// over-ride the electron store setup in client

// client index (src/preload/client/index.ts)
// const settings = new Store({
// name: "settings",
// });

// // todo: validation, put this somewhere proper
// const db = DB(settings.get("DATABASE_URL") as string);

// I need to overide the DATABASE_URL before importing the client

import DB from "better-sqlite3";
import Knex from "knex";
import Store from "electron-store";

function setupClient() {
const settings = new Store({
name: "settings-test",
});

// todo: validation, put this somewhere proper
const db = DB(settings.get("DATABASE_URL") as string);

type IDB = ReturnType<typeof DB>;

// Added knex for search which required lots of query mix and
// matching
// todo: migrate codebase to prefer knex to directly using
// the better-sqlite3 client
const knex = Knex({
client: "better-sqlite3", // or 'better-sqlite3'
connection: {
filename: settings.get("DATABASE_URL") as string,
},
});

return create(db, knex, settings);
}

suite("client.documents", function () {
before("Setup database", function () {
setupClient();
});

test("loads", function () {
throw "Hello mocha";
});
});
27 changes: 16 additions & 11 deletions src/preload/client/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,33 @@ import DB from "better-sqlite3";
import Knex from "knex";

import Store from "electron-store";
const settings = new Store({
name: "settings",
});
// const settings = new Store({
// name: "settings",
// });

// todo: validation, put this somewhere proper
const db = DB(settings.get("DATABASE_URL") as string);
// const db = DB(settings.get("DATABASE_URL") as string);

type IDB = ReturnType<typeof DB>;
type IKnex = ReturnType<typeof Knex>;

// Added knex for search which required lots of query mix and
// matching
// todo: migrate codebase to prefer knex to directly using
// the better-sqlite3 client
const knex = Knex({
client: "better-sqlite3", // or 'better-sqlite3'
connection: {
filename: settings.get("DATABASE_URL") as string,
},
});
// const knex = Knex({
// client: "better-sqlite3", // or 'better-sqlite3'
// connection: {
// filename: settings.get("DATABASE_URL") as string,
// },
// });

export { GetDocumentResponse } from "./documents";

// todo: Given how this client is setup, I wonder if singleton approach is really necessary
let client: IClient;
export function create(): IClient {

export function create(db: IDB, knex: IKnex, settings: Store): IClient {
if (!client) {
client = {
journals: new JournalsClient(db),
Expand Down
2 changes: 2 additions & 0 deletions src/preload/importer/importChronicles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import { DateTime } from "luxon";

import { create } from "../client";

// todo: move create setup to a shared file
const client = create();

Check failure on line 11 in src/preload/importer/importChronicles.ts

View workflow job for this annotation

GitHub Actions / lint

Expected 3 arguments, but got 0.

async function findOrCreate(name: string) {
const journals = await client.journals.list();
Expand Down
26 changes: 25 additions & 1 deletion src/preload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,30 @@ import { contextBridge } from "electron";
import { create } from "./client";
import "./utils.electron";

import DB from "better-sqlite3";
import Knex from "knex";

import Store from "electron-store";
const settings = new Store({
name: "settings",
});

// todo: validation, put this somewhere proper
const db = DB(settings.get("DATABASE_URL") as string);

type IDB = ReturnType<typeof DB>;

// Added knex for search which required lots of query mix and
// matching
// todo: migrate codebase to prefer knex to directly using
// the better-sqlite3 client
const knex = Knex({
client: "better-sqlite3", // or 'better-sqlite3'
connection: {
filename: settings.get("DATABASE_URL") as string,
},
});

contextBridge.exposeInMainWorld("chronicles", {
createClient: create,
createClient: () => create(db, knex, settings),
});
18 changes: 10 additions & 8 deletions src/views/edit/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { test } from "mocha";
import { test, suite } from "mocha";

test("loads");
test("has loading state");
test("saves document");
test("cannot edit while saving");
test("saving a document twice updates it...");
test("saving error is surfaced");
test("loading error is surfaced");
suite("views.edit", function () {
test("loads");
test("has loading state");
test("saves document");
test("cannot edit while saving");
test("saving a document twice updates it...");
test("saving error is surfaced");
test("loading error is surfaced");
});
Loading
Loading