Skip to content

Commit

Permalink
Adds UI configuration (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
reederc42 authored Nov 29, 2023
1 parent 5b58342 commit 176dfee
Show file tree
Hide file tree
Showing 33 changed files with 225 additions and 120 deletions.
13 changes: 13 additions & 0 deletions images/ui/test.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
ARG NODE_VERSION="21.2.0"
ARG CHROME_VERSION="119.0.6045.159-1"
ARG FIREFOX_VERSION="120.0"
ARG EDGE_VERSION=
ARG YARN_VERSION=
ARG CYPRESS_VERSION=

FROM cypress/factory:3.2.0

WORKDIR /ci
COPY ./ui/package.json .
COPY ./ui/package-lock.json .
RUN npm install
2 changes: 2 additions & 0 deletions ui/.gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
dist/
node_modules/
testdata/
src/config.json
cypress/downloads
4 changes: 4 additions & 0 deletions ui/config.default.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"userExpiration": 300000,
"apiExpiration": 120000
}
6 changes: 4 additions & 2 deletions ui/cypress/e2e/spec.cy.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* eslint no-undef: "off" */

import { loremIpsum } from "lorem-ipsum";
import { newUser, existingUser } from "../../src/test-helpers/mock/auth";

import config from "../../src/config.json";
import { newSubject, existingSubject } from "../../src/test-helpers/mock/api";
import { newUser, existingUser } from "../../src/test-helpers/mock/auth";

const userSignInWait = 1000;
const userExpiration = 2000 + 250;
const userExpiration = config.userExpiration + 250;

describe("UI e2e tests", () => {
it("Views subject list", () => {
Expand Down
7 changes: 4 additions & 3 deletions ui/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"main": "src/main.js",
"type": "module",
"scripts": {
"dev": "nodemon tools/dev-server.js",
"build": "node tools/prod.js",
"test": "node --test",
"dev": "nodemon tools/serve.js",
"build": "node tools/build.js",
"test": "node tools/configure.js && node --test",
"lint": "eslint \"./**/*.js\""
},
"repository": {
Expand Down Expand Up @@ -37,7 +37,8 @@
},
"nodemonConfig": {
"ignore": [
"dist/**"
"dist/**",
"src/config.json"
]
},
"eslintConfig": {
Expand Down
2 changes: 1 addition & 1 deletion ui/src/api/mock/subject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as mockSubjects from "./subjects.json";
import { user } from "../../store/user";
import mockSubjects from "./subjects.json";

const timeout = 400;

Expand Down
13 changes: 6 additions & 7 deletions ui/src/auth/mock/user.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import * as mockUsers from "./users.json";
import config from "../../config.json";
import mockUsers from "./users.json";

const timeout = 400;
const refreshExpiration = 2000; // 5 minutes
const tokenExpiration = 1000;

let m = new Map();

Expand Down Expand Up @@ -32,12 +31,12 @@ export const user = {
token: token(
username,
password,
Date.now() + tokenExpiration,
Date.now() + config.apiExpiration,
),
refresh: token(
username,
password,
Date.now() + refreshExpiration,
Date.now() + config.userExpiration,
),
});
} else {
Expand All @@ -58,12 +57,12 @@ export const user = {
token: token(
username,
password,
Date.now() + tokenExpiration,
Date.now() + config.apiExpiration,
),
refresh: token(
username,
password,
Date.now() + refreshExpiration,
Date.now() + config.userExpiration,
),
});
} else {
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/App.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { render } from "reefjs";

import { router, navigate } from "../store/router";

class App extends HTMLElement {
Expand Down
8 changes: 5 additions & 3 deletions ui/src/components/EditSubject.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { render } from "reefjs";
import { extract } from "../store/inject";
import { user, signal as userSignal } from "../store/user";
import ace from "ace-builds/src-min/ace";
import "ace-builds/src-min/theme-github";
import "ace-builds/src-min/mode-markdown";

import { render } from "reefjs";

import { extract } from "../store/inject";
import { user, signal as userSignal } from "../store/user";

const signal = "subject-edited";
const event = new Event("wiki:signal-" + signal, {
bubbles: true,
Expand Down
3 changes: 2 additions & 1 deletion ui/src/components/ListSubjects.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { signal, render } from "reefjs";
import { subjects, signal as subjectsSignal } from "../store/subjects";

import { navigate } from "../store/router";
import { subjects, signal as subjectsSignal } from "../store/subjects";

const errorSignal = "list-subjects-error";

Expand Down
5 changes: 3 additions & 2 deletions ui/src/components/ListSubjects.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { waitFor } from "../test-helpers/waitFor.js";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

describe("List Subjects component", () => {
let dom, window, document;
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/Router.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { component } from "reefjs";

import {
router,
getSubject,
Expand Down
3 changes: 2 additions & 1 deletion ui/src/components/Router.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";

describe("Router component", () => {
Expand Down
5 changes: 3 additions & 2 deletions ui/src/components/Subject.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { render } from "reefjs";
import { setTitle } from "../store/title";

import { inject } from "../store/inject";
import { router } from "../store/router";
import {
subjects as subjectStore,
Subject as StoreSubject,
} from "../store/subjects";
import { router } from "../store/router";
import { setTitle } from "../store/title";
import { user, signal as userSignal } from "../store/user";

const errTimeout = 3000;
Expand Down
5 changes: 3 additions & 2 deletions ui/src/components/Subject.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { waitFor } from "../test-helpers/waitFor.js";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

describe("Subject component", () => {
let dom, window, document;
Expand Down
1 change: 1 addition & 0 deletions ui/src/components/User.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { component, render } from "reefjs";

import { user, signal as userSignal } from "../store/user";

const errTimeout = 3000;
Expand Down
3 changes: 2 additions & 1 deletion ui/src/components/User.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

Expand Down
3 changes: 2 additions & 1 deletion ui/src/components/ViewSubject.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { render } from "reefjs";
import { marked } from "marked";
import { render } from "reefjs";

import { extract } from "../store/inject";

class ViewSubject extends HTMLElement {
Expand Down
5 changes: 3 additions & 2 deletions ui/src/components/ViewSubject.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { waitFor } from "../test-helpers/waitFor.js";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

describe("View Subject component", () => {
let dom, window, document;
Expand Down
5 changes: 3 additions & 2 deletions ui/src/store/router.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, beforeEach, afterEach, test } from "node:test";
import assert from "node:assert";
import { waitFor } from "../test-helpers/waitFor.js";
import { describe, beforeEach, afterEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

describe("router store", () => {
let dom, window, document, previousConsoleError;
Expand Down
5 changes: 3 additions & 2 deletions ui/src/store/subjects.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { waitFor } from "../test-helpers/waitFor.js";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

describe("subjects store", () => {
let dom, window, document;
Expand Down
1 change: 1 addition & 0 deletions ui/src/store/user.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { store } from "reefjs";

import { user as auth } from "../auth/mock/user";

export const signal = "user";
Expand Down
3 changes: 2 additions & 1 deletion ui/src/store/user.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { describe, beforeEach, test } from "node:test";
import assert from "node:assert";
import { describe, beforeEach, test } from "node:test";

import { DOM } from "../test-helpers/dom.js";
import { waitFor } from "../test-helpers/waitFor.js";

Expand Down
2 changes: 1 addition & 1 deletion ui/src/test-helpers/build.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as esbuild from "esbuild";
import esbuild from "esbuild";

export function buildSync(options = {}) {
let baseOptions = {
Expand Down
1 change: 1 addition & 0 deletions ui/src/test-helpers/dom.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JSDOM } from "jsdom";

import { buildSync } from "./build.js";

export class DOM {
Expand Down
3 changes: 2 additions & 1 deletion ui/src/test-helpers/mock/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as mockSubjects from "../../api/mock/subjects.json";
import { loremIpsum } from "lorem-ipsum";

import * as mockSubjects from "../../api/mock/subjects.json";

let a = [];

for (const s of Object.getOwnPropertyNames(mockSubjects)) {
Expand Down
1 change: 1 addition & 0 deletions ui/src/test-helpers/waitFor.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import assert from "node:assert";

import { waitFor as domWaitFor } from "@testing-library/dom";

export async function waitFor(
Expand Down
31 changes: 30 additions & 1 deletion ui/tools/build.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/* eslint-env node */

import fs from "fs";
import fs from "node:fs";

import esbuild from "esbuild";
import yargs from "yargs";
import { hideBin } from "yargs/helpers";

import { prod, dev } from "./build-options.js";
import { defaults, configure } from "./configure.js";

const outdir = "dist";

Expand Down Expand Up @@ -30,3 +36,26 @@ export async function build(options = {}, env = "") {

console.log(`finishing building for ${env}`);
}

async function main() {
const argv = yargs(hideBin(process.argv))
.default("build", "prod")
.default(defaults).argv;

configure(argv);

let options;
switch (argv.build) {
case "prod":
options = prod;
break;
case "dev":
options = dev;
break;
}

await build(options, argv.build);
}

if (process.argv.includes(import.meta.url.substring("file://".length)))
await main();
26 changes: 26 additions & 0 deletions ui/tools/configure.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* eslint-env node */

import fs from "node:fs";

import yargs from "yargs";
import { hideBin } from "yargs/helpers";

const configFile = "./src/config.json";
export const defaults = JSON.parse(fs.readFileSync("config.default.json"));

export function configure(config) {
let mergedConfig = defaults;
for (const v in defaults) {
if (v in config) {
mergedConfig[v] = config[v];
}
}
fs.writeFileSync(configFile, JSON.stringify(mergedConfig));
}

function main() {
const argv = yargs(hideBin(process.argv)).default(defaults).argv;
configure(argv);
}

if (process.argv.includes(import.meta.url.substring("file://".length))) main();
Loading

0 comments on commit 176dfee

Please sign in to comment.