Merge branch 'develop' into shortcuts-i18n

This commit is contained in:
Elian Doran
2025-06-04 11:04:41 +03:00
committed by GitHub
29 changed files with 991 additions and 772 deletions

View File

@@ -222,7 +222,6 @@ export function buildFloatingToolbar() {
"|",
"code",
"link",
"bookmark",
"removeFormat",
"internallink",
"cuttonote"
@@ -244,7 +243,7 @@ export function buildFloatingToolbar() {
{
label: "Insert",
icon: "plus",
items: ["internallink", "includeNote", "|", "math", "mermaid", "horizontalLine", "pageBreak"]
items: ["bookmark", "internallink", "includeNote", "|", "math", "mermaid", "horizontalLine", "pageBreak"]
},
"|",
"outdent",

View File

@@ -85,10 +85,10 @@
"jsdom": "26.1.0",
"marked": "15.0.12",
"mime-types": "3.0.1",
"multer": "2.0.0",
"multer": "2.0.1",
"normalize-strings": "1.1.1",
"ollama": "0.5.16",
"openai": "5.0.2",
"openai": "5.1.0",
"rand-token": "1.0.1",
"safe-compare": "1.1.4",
"sanitize-filename": "1.6.3",

View File

@@ -21,6 +21,6 @@ describe("etapi/backup", () => {
const response = await supertest(app)
.put("/etapi/backup/etapi_test")
.auth(USER, token, { "type": "basic"})
.expect(201);
.expect(204);
});
});

View File

@@ -0,0 +1,172 @@
import { Application } from "express";
import { beforeAll, beforeEach, describe, expect, it } from "vitest";
import supertest from "supertest";
import { login } from "./utils.js";
import config from "../../src/services/config.js";
import { randomInt } from "crypto";
let app: Application;
let token: string;
let createdNoteId: string;
let createdBranchId: string;
const USER = "etapi";
type EntityType = "attachments" | "attributes" | "branches" | "notes";
describe("etapi/delete-entities", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
});
beforeEach(async () => {
({ createdNoteId, createdBranchId } = await createNote());
});
it("deletes attachment", async () => {
const attachmentId = await createAttachment();
await deleteEntity("attachments", attachmentId);
await expectNotFound("attachments", attachmentId);
});
it("deletes attribute", async () => {
const attributeId = await createAttribute();
await deleteEntity("attributes", attributeId);
await expectNotFound("attributes", attributeId);
});
it("deletes cloned branch", async () => {
const clonedBranchId = await createClone();
await expectFound("branches", createdBranchId);
await expectFound("branches", clonedBranchId);
await deleteEntity("branches", createdBranchId);
await expectNotFound("branches", createdBranchId);
await expectFound("branches", clonedBranchId);
await expectFound("notes", createdNoteId);
});
it("deletes note with all branches", async () => {
const attributeId = await createAttribute();
const clonedBranchId = await createClone();
await expectFound("notes", createdNoteId);
await expectFound("branches", createdBranchId);
await expectFound("branches", clonedBranchId);
await expectFound("attributes", attributeId);
await deleteEntity("notes", createdNoteId);
await expectNotFound("branches", createdBranchId);
await expectNotFound("branches", clonedBranchId);
await expectNotFound("notes", createdNoteId);
await expectNotFound("attributes", attributeId);
});
});
async function createNote() {
const noteId = `forcedId${randomInt(1000)}`;
const response = await supertest(app)
.post("/etapi/create-note")
.auth(USER, token, { "type": "basic"})
.send({
"noteId": noteId,
"parentNoteId": "root",
"title": "Hello",
"type": "text",
"content": "Hi there!",
"dateCreated": "2023-08-21 23:38:51.123+0200",
"utcDateCreated": "2023-08-21 23:38:51.123Z"
})
.expect(201);
expect(response.body.note.noteId).toStrictEqual(noteId);
return {
createdNoteId: response.body.note.noteId,
createdBranchId: response.body.branch.branchId
};
}
async function createClone() {
const response = await supertest(app)
.post("/etapi/branches")
.auth(USER, token, { "type": "basic"})
.send({
noteId: createdNoteId,
parentNoteId: "_hidden"
})
.expect(201);
expect(response.body.parentNoteId).toStrictEqual("_hidden");
return response.body.branchId;
}
async function createAttribute() {
const attributeId = `forcedId${randomInt(1000)}`;
const response = await supertest(app)
.post("/etapi/attributes")
.auth(USER, token, { "type": "basic"})
.send({
"attributeId": attributeId,
"noteId": createdNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": true
})
.expect(201);
expect(response.body.attributeId).toStrictEqual(attributeId);
return response.body.attributeId;
}
async function createAttachment() {
const response = await supertest(app)
.post("/etapi/attachments")
.auth(USER, token, { "type": "basic"})
.send({
"ownerId": createdNoteId,
"role": "file",
"mime": "plain/text",
"title": "my attachment",
"content": "my text"
})
.expect(201);
return response.body.attachmentId;
}
async function deleteEntity(entity: EntityType, id: string) {
// Delete twice to test idempotency.
for (let i=0; i < 2; i++) {
await supertest(app)
.delete(`/etapi/${entity}/${id}`)
.auth(USER, token, { "type": "basic"})
.expect(204);
}
}
const MISSING_ENTITY_ERROR_CODES: Record<EntityType, string> = {
attachments: "ATTACHMENT_NOT_FOUND",
attributes: "ATTRIBUTE_NOT_FOUND",
branches: "BRANCH_NOT_FOUND",
notes: "NOTE_NOT_FOUND"
}
async function expectNotFound(entity: EntityType, id: string) {
const response = await supertest(app)
.get(`/etapi/${entity}/${id}`)
.auth(USER, token, { "type": "basic"})
.expect(404);
expect(response.body.code).toStrictEqual(MISSING_ENTITY_ERROR_CODES[entity]);
}
async function expectFound(entity: EntityType, id: string) {
await supertest(app)
.get(`/etapi/${entity}/${id}`)
.auth(USER, token, { "type": "basic"})
.expect(200);
}

View File

@@ -0,0 +1,103 @@
import { beforeAll, describe, expect, it } from "vitest";
import config from "../../src/services/config.js";
import { login } from "./utils.js";
import { Application } from "express";
import supertest from "supertest";
import date_notes from "../../src/services/date_notes.js";
import cls from "../../src/services/cls.js";
let app: Application;
let token: string;
const USER = "etapi";
describe("etapi/get-date-notes", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
});
it("obtains inbox", async () => {
await supertest(app)
.get("/etapi/inbox/2022-01-01")
.auth(USER, token, { "type": "basic"})
.expect(200);
});
describe("days", () => {
it("obtains day from calendar", async () => {
await supertest(app)
.get("/etapi/calendar/days/2022-01-01")
.auth(USER, token, { "type": "basic"})
.expect(200);
});
it("detects invalid date", async () => {
const response = await supertest(app)
.get("/etapi/calendar/days/2022-1")
.auth(USER, token, { "type": "basic"})
.expect(400);
expect(response.body.code).toStrictEqual("DATE_INVALID");
});
});
describe("weeks", () => {
beforeAll(() => {
cls.init(() => {
const rootCalendarNote = date_notes.getRootCalendarNote();
rootCalendarNote.setLabel("enableWeekNote");
});
});
it("obtains week calendar", async () => {
await supertest(app)
.get("/etapi/calendar/weeks/2022-W01")
.auth(USER, token, { "type": "basic"})
.expect(200);
});
it("detects invalid date", async () => {
const response = await supertest(app)
.get("/etapi/calendar/weeks/2022-1")
.auth(USER, token, { "type": "basic"})
.expect(400);
expect(response.body.code).toStrictEqual("WEEK_INVALID");
});
});
describe("months", () => {
it("obtains month calendar", async () => {
await supertest(app)
.get("/etapi/calendar/months/2022-01")
.auth(USER, token, { "type": "basic"})
.expect(200);
});
it("detects invalid month", async () => {
const response = await supertest(app)
.get("/etapi/calendar/months/2022-1")
.auth(USER, token, { "type": "basic"})
.expect(400);
expect(response.body.code).toStrictEqual("MONTH_INVALID");
});
});
describe("years", () => {
it("obtains year calendar", async () => {
await supertest(app)
.get("/etapi/calendar/years/2022")
.auth(USER, token, { "type": "basic"})
.expect(200);
});
it("detects invalid year", async () => {
const response = await supertest(app)
.get("/etapi/calendar/years/202")
.auth(USER, token, { "type": "basic"})
.expect(400);
expect(response.body.code).toStrictEqual("YEAR_INVALID");
});
});
});

View File

@@ -0,0 +1,98 @@
import { Application } from "express";
import { beforeAll, describe, expect, it } from "vitest";
import supertest from "supertest";
import { createNote, login } from "./utils.js";
import config from "../../src/services/config.js";
let app: Application;
let token: string;
let parentNoteId: string;
describe("etapi/get-inherited-attribute-cloned", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
parentNoteId = await createNote(app, token);
});
it("gets inherited attribute", async () => {
// Create an inheritable attribute on the parent note.
let response = await supertest(app)
.post("/etapi/attributes")
.auth("etapi", token, { "type": "basic"})
.send({
"noteId": parentNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": true,
"position": 10
})
.expect(201);
const parentAttributeId = response.body.attributeId;
expect(parentAttributeId).toBeTruthy();
// Create a subnote.
response = await supertest(app)
.post("/etapi/create-note")
.auth("etapi", token, { "type": "basic"})
.send({
"parentNoteId": parentNoteId,
"title": "Hello",
"type": "text",
"content": "Hi there!"
})
.expect(201);
const childNoteId = response.body.note.noteId;
// Create child attribute
response = await supertest(app)
.post("/etapi/attributes")
.auth("etapi", token, { "type": "basic"})
.send({
"noteId": childNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": false,
"position": 10
})
.expect(201);
const childAttributeId = response.body.attributeId;
expect(parentAttributeId).toBeTruthy();
// Clone child to parent
response = await supertest(app)
.post("/etapi/branches")
.auth("etapi", token, { "type": "basic"})
.send({
noteId: childNoteId,
parentNoteId: parentNoteId
})
.expect(200);
parentNoteId = response.body.parentNoteId;
// Check attribute IDs
response = await supertest(app)
.get(`/etapi/notes/${childNoteId}`)
.auth("etapi", token, { "type": "basic"})
.expect(200);
expect(response.body.noteId).toStrictEqual(childNoteId);
expect(response.body.attributes).toHaveLength(2);
expect(hasAttribute(response.body.attributes, parentAttributeId));
expect(hasAttribute(response.body.attributes, childAttributeId));
});
function hasAttribute(list: object[], attributeId: string) {
for (let i = 0; i < list.length; i++) {
if (list[i]["attributeId"] === attributeId) {
return true;
}
}
return false;
}
});

View File

@@ -0,0 +1,60 @@
import { Application } from "express";
import { beforeAll, describe, expect, it } from "vitest";
import supertest from "supertest";
import { createNote, login } from "./utils.js";
import config from "../../src/services/config.js";
let app: Application;
let token: string;
let parentNoteId: string;
describe("etapi/get-inherited-attribute", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
parentNoteId = await createNote(app, token);
});
it("gets inherited attribute", async () => {
// Create an inheritable attribute on the parent note.
let response = await supertest(app)
.post("/etapi/attributes")
.auth("etapi", token, { "type": "basic"})
.send({
"noteId": parentNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": true
})
.expect(201);
const createdAttributeId = response.body.attributeId;
expect(createdAttributeId).toBeTruthy();
// Create a subnote.
response = await supertest(app)
.post("/etapi/create-note")
.auth("etapi", token, { "type": "basic"})
.send({
"parentNoteId": parentNoteId,
"title": "Hello",
"type": "text",
"content": "Hi there!"
})
.expect(201);
const createdNoteId = response.body.note.noteId;
// Check the attribute is inherited.
response = await supertest(app)
.get(`/etapi/notes/${createdNoteId}`)
.auth("etapi", token, { "type": "basic"})
.expect(200);
expect(response.body.noteId).toStrictEqual(createdNoteId);
expect(response.body.attributes).toHaveLength(1);
expect(response.body.attributes[0].attributeId === createdAttributeId);
});
});

View File

@@ -21,6 +21,6 @@ describe("etapi/refresh-note-ordering/root", () => {
await supertest(app)
.post("/etapi/refresh-note-ordering/root")
.auth(USER, token, { "type": "basic"})
.expect(200);
.expect(204);
});
});

View File

@@ -0,0 +1,77 @@
import { Application } from "express";
import { beforeAll, describe, expect, it } from "vitest";
import supertest from "supertest";
import { createNote, login } from "./utils.js";
import config from "../../src/services/config.js";
let app: Application;
let token: string;
const USER = "etapi";
let createdNoteId: string;
let createdAttributeId: string;
describe("etapi/patch-attribute", () => {
beforeAll(async () => {
config.General.noAuthentication = false;
const buildApp = (await (import("../../src/app.js"))).default;
app = await buildApp();
token = await login(app);
createdNoteId = await createNote(app, token);
// Create an attribute
const response = await supertest(app)
.post(`/etapi/attributes`)
.auth(USER, token, { "type": "basic"})
.send({
"noteId": createdNoteId,
"type": "label",
"name": "mylabel",
"value": "val",
"isInheritable": true
});
createdAttributeId = response.body.attributeId;
expect(createdAttributeId).toBeTruthy();
});
it("changes name and value", async () => {
const state = {
value: "CHANGED"
};
await supertest(app)
.patch(`/etapi/attributes/${createdAttributeId}`)
.auth(USER, token, { "type": "basic"})
.send(state)
.expect(200);
// Ensure it got changed.
const response = await supertest(app)
.get(`/etapi/attributes/${createdAttributeId}`)
.auth(USER, token, { "type": "basic"});
expect(response.body).toMatchObject(state);
});
it("forbids setting disallowed property", async () => {
const response = await supertest(app)
.patch(`/etapi/attributes/${createdAttributeId}`)
.auth(USER, token, { "type": "basic"})
.send({
noteId: "root"
})
.expect(400);
expect(response.body.code).toStrictEqual("PROPERTY_NOT_ALLOWED");
});
it("forbids setting wrong data type", async () => {
const response = await supertest(app)
.patch(`/etapi/attributes/${createdAttributeId}`)
.auth(USER, token, { "type": "basic"})
.send({
value: null
})
.expect(400);
expect(response.body.code).toStrictEqual("PROPERTY_VALIDATION_ERROR");
});
});

View File

@@ -34,7 +34,7 @@ describe("etapi/patch-note", () => {
});
it("obtains correct note information", async () => {
expectNoteToMatch({
await expectNoteToMatch({
title: "Hello",
type: "code",
mime: "application/json"

View File

@@ -13,6 +13,7 @@
<link rel="shortcut icon" href="../favicon.ico">
<% } %>
<script src="<%= appPath %>/share.js" type="module"></script>
<link href="<%= assetPath %>/src/share.css" rel="stylesheet">
<% if (!note.isLabelTruthy("shareOmitDefaultCss")) { %>
<link href="<%= assetPath %>/stylesheets/share.css" rel="stylesheet">
<% } %>

View File

@@ -4,10 +4,10 @@ import eu from "./etapi_utils.js";
import backupService from "../services/backup.js";
function register(router: Router) {
eu.route(router, "put", "/etapi/backup/:backupName", async (req, res, next) => {
await backupService.backupNow(req.params.backupName);
res.sendStatus(204);
eu.route(router, "put", "/etapi/backup/:backupName", (req, res, next) => {
backupService.backupNow(req.params.backupName)
.then(() => res.sendStatus(204))
.catch(() => res.sendStatus(500));
});
}

View File

@@ -6,7 +6,7 @@ import etapiTokenService from "../services/etapi_tokens.js";
import config from "../services/config.js";
import type { NextFunction, Request, RequestHandler, Response, Router } from "express";
import type { ValidatorMap } from "./etapi-interface.js";
import type { ApiRequestHandler } from "../routes/route_api.js";
import type { ApiRequestHandler, SyncRouteRequestHandler } from "../routes/route_api.js";
const GENERIC_CODE = "GENERIC";
type HttpMethod = "all" | "get" | "post" | "put" | "delete" | "patch" | "options" | "head";
@@ -73,11 +73,11 @@ function processRequest(req: Request, res: Response, routeHandler: ApiRequestHan
}
}
function route(router: Router, method: HttpMethod, path: string, routeHandler: ApiRequestHandler) {
function route(router: Router, method: HttpMethod, path: string, routeHandler: SyncRouteRequestHandler) {
router[method](path, checkEtapiAuth, (req: Request, res: Response, next: NextFunction) => processRequest(req, res, routeHandler, next, method, path));
}
function NOT_AUTHENTICATED_ROUTE(router: Router, method: HttpMethod, path: string, middleware: RequestHandler[], routeHandler: RequestHandler) {
function NOT_AUTHENTICATED_ROUTE(router: Router, method: HttpMethod, path: string, middleware: RequestHandler[], routeHandler: SyncRouteRequestHandler) {
router[method](path, ...middleware, (req: Request, res: Response, next: NextFunction) => processRequest(req, res, routeHandler, next, method, path));
}

View File

@@ -15,46 +15,46 @@ function isValidDate(date: string) {
}
function register(router: Router) {
eu.route(router, "get", "/etapi/inbox/:date", async (req, res, next) => {
eu.route(router, "get", "/etapi/inbox/:date", (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = await specialNotesService.getInboxNote(date);
const note = specialNotesService.getInboxNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/days/:date", async (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/days/:date", (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = await dateNotesService.getDayNote(date);
const note = dateNotesService.getDayNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", async (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/week-first-day/:date", (req, res, next) => {
const { date } = req.params;
if (!isValidDate(date)) {
throw getDateInvalidError(date);
}
const note = await dateNotesService.getWeekFirstDayNote(date);
const note = dateNotesService.getWeekFirstDayNote(date);
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/weeks/:week", async (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/weeks/:week", (req, res, next) => {
const { week } = req.params;
if (!/[0-9]{4}-W[0-9]{2}/.test(week)) {
throw getWeekInvalidError(week);
}
const note = await dateNotesService.getWeekNote(week);
const note = dateNotesService.getWeekNote(week);
if (!note) {
throw getWeekNotFoundError(week);
@@ -63,14 +63,14 @@ function register(router: Router) {
res.json(mappers.mapNoteToPojo(note));
});
eu.route(router, "get", "/etapi/calendar/months/:month", async (req, res, next) => {
eu.route(router, "get", "/etapi/calendar/months/:month", (req, res, next) => {
const { month } = req.params;
if (!/[0-9]{4}-[0-9]{2}/.test(month)) {
throw getMonthInvalidError(month);
}
const note = await dateNotesService.getMonthNote(month);
const note = dateNotesService.getMonthNote(month);
res.json(mappers.mapNoteToPojo(note));
});

View File

@@ -9,6 +9,7 @@ import searchService from "./search/services/search.js";
import SearchContext from "./search/search_context.js";
import hiddenSubtree from "./hidden_subtree.js";
import { t } from "i18next";
import { BNote } from "./backend_script_entrypoint.js";
const { LBTPL_NOTE_LAUNCHER, LBTPL_CUSTOM_WIDGET, LBTPL_SPACER, LBTPL_SCRIPT } = hiddenSubtree;
function getInboxNote(date: string) {
@@ -17,7 +18,7 @@ function getInboxNote(date: string) {
throw new Error("Unable to find workspace note");
}
let inbox;
let inbox: BNote;
if (!workspaceNote.isRoot()) {
inbox = workspaceNote.searchNoteInSubtree("#workspaceInbox");

View File

@@ -11,7 +11,6 @@ export default defineConfig(() => ({
setupFiles: ["./spec/setup.ts"],
environment: "node",
include: ['{src,spec}/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
reporters: ['default'],
coverage: {
reportsDirectory: './test-output/vitest/coverage',
provider: 'v8' as const,