mirror of https://github.com/TriliumNext/Notes
enable jasmine test runs
parent
a68b75f069
commit
e7f11d6687
@ -1,12 +0,0 @@
|
|||||||
const {
|
|
||||||
describeEtapi, postEtapi,
|
|
||||||
putEtapiContent
|
|
||||||
} = require('../support/etapi.js');
|
|
||||||
const {getEtapi} = require("../support/etapi.js");
|
|
||||||
|
|
||||||
describeEtapi("app_info", () => {
|
|
||||||
it("get", async () => {
|
|
||||||
const appInfo = await getEtapi("app-info");
|
|
||||||
expect(appInfo.clipperProtocolVersion).toEqual("1.0");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
import etapi = require("../support/etapi");
|
||||||
|
|
||||||
|
etapi.describeEtapi("app_info", () => {
|
||||||
|
it("get", async () => {
|
||||||
|
const appInfo = await etapi.getEtapi("app-info");
|
||||||
|
expect(appInfo.clipperProtocolVersion).toEqual("1.0");
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,12 +0,0 @@
|
|||||||
const {
|
|
||||||
describeEtapi, postEtapi,
|
|
||||||
getEtapi,
|
|
||||||
} = require('../support/etapi.js');
|
|
||||||
const {putEtapiContent} = require("../support/etapi.js");
|
|
||||||
|
|
||||||
describeEtapi("backup", () => {
|
|
||||||
it("create", async () => {
|
|
||||||
const response = await putEtapiContent("backup/etapi_test");
|
|
||||||
expect(response.status).toEqual(204);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -0,0 +1,8 @@
|
|||||||
|
import etapi = require("../support/etapi");
|
||||||
|
|
||||||
|
etapi.describeEtapi("backup", () => {
|
||||||
|
it("create", async () => {
|
||||||
|
const response = await etapi.putEtapiContent("backup/etapi_test");
|
||||||
|
expect(response.status).toEqual(204);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,24 +0,0 @@
|
|||||||
const {
|
|
||||||
describeEtapi, postEtapi,
|
|
||||||
postEtapiContent,
|
|
||||||
} = require('../support/etapi.js');
|
|
||||||
const fs = require("fs");
|
|
||||||
const path = require("path");
|
|
||||||
const {getEtapiContent} = require("../support/etapi.js");
|
|
||||||
|
|
||||||
describeEtapi("import", () => {
|
|
||||||
it("import", async () => {
|
|
||||||
const zipFileBuffer = fs.readFileSync(path.resolve(__dirname, 'test-export.zip'));
|
|
||||||
|
|
||||||
const response = await postEtapiContent("notes/root/import", zipFileBuffer);
|
|
||||||
expect(response.status).toEqual(201);
|
|
||||||
|
|
||||||
const {note, branch} = await response.json();
|
|
||||||
|
|
||||||
expect(note.title).toEqual("test-export");
|
|
||||||
expect(branch.parentNoteId).toEqual("root");
|
|
||||||
|
|
||||||
const content = await (await getEtapiContent(`notes/${note.noteId}/content`)).text();
|
|
||||||
expect(content).toContain("test export content");
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -0,0 +1,27 @@
|
|||||||
|
import etapi = require("../support/etapi");
|
||||||
|
import fs = require("fs");
|
||||||
|
import path = require("path");
|
||||||
|
|
||||||
|
etapi.describeEtapi("import", () => {
|
||||||
|
it("import", async () => {
|
||||||
|
const zipFileBuffer = fs.readFileSync(
|
||||||
|
path.resolve(__dirname, "test-export.zip")
|
||||||
|
);
|
||||||
|
|
||||||
|
const response = await etapi.postEtapiContent(
|
||||||
|
"notes/root/import",
|
||||||
|
zipFileBuffer
|
||||||
|
);
|
||||||
|
expect(response.status).toEqual(201);
|
||||||
|
|
||||||
|
const { note, branch } = await response.json();
|
||||||
|
|
||||||
|
expect(note.title).toEqual("test-export");
|
||||||
|
expect(branch.parentNoteId).toEqual("root");
|
||||||
|
|
||||||
|
const content = await (
|
||||||
|
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
|
||||||
|
).text();
|
||||||
|
expect(content).toContain("test export content");
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,109 +0,0 @@
|
|||||||
const crypto = require('crypto');
|
|
||||||
const {
|
|
||||||
deleteEtapi,
|
|
||||||
getEtapiResponse,
|
|
||||||
describeEtapi, postEtapi,
|
|
||||||
getEtapi,
|
|
||||||
getEtapiContent,
|
|
||||||
patchEtapi, putEtapi,
|
|
||||||
putEtapiContent
|
|
||||||
} = require('../support/etapi.js');
|
|
||||||
|
|
||||||
describeEtapi("notes", () => {
|
|
||||||
it("create", async () => {
|
|
||||||
const {note, branch} = await postEtapi('create-note', {
|
|
||||||
parentNoteId: 'root',
|
|
||||||
type: 'text',
|
|
||||||
title: 'Hello World!',
|
|
||||||
content: 'Content',
|
|
||||||
prefix: 'Custom prefix'
|
|
||||||
});
|
|
||||||
|
|
||||||
expect(note.title).toEqual("Hello World!");
|
|
||||||
expect(branch.parentNoteId).toEqual("root");
|
|
||||||
expect(branch.prefix).toEqual("Custom prefix");
|
|
||||||
|
|
||||||
const rNote = await getEtapi(`notes/${note.noteId}`);
|
|
||||||
expect(rNote.title).toEqual("Hello World!");
|
|
||||||
|
|
||||||
const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text();
|
|
||||||
expect(rContent).toEqual("Content");
|
|
||||||
|
|
||||||
const rBranch = await getEtapi(`branches/${branch.branchId}`);
|
|
||||||
expect(rBranch.parentNoteId).toEqual("root");
|
|
||||||
expect(rBranch.prefix).toEqual("Custom prefix");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("patch", async () => {
|
|
||||||
const {note} = await postEtapi('create-note', {
|
|
||||||
parentNoteId: 'root',
|
|
||||||
type: 'text',
|
|
||||||
title: 'Hello World!',
|
|
||||||
content: 'Content'
|
|
||||||
});
|
|
||||||
|
|
||||||
await patchEtapi(`notes/${note.noteId}`, {
|
|
||||||
title: 'new title',
|
|
||||||
type: 'code',
|
|
||||||
mime: 'text/apl',
|
|
||||||
dateCreated: '2000-01-01 12:34:56.999+0200',
|
|
||||||
utcDateCreated: '2000-01-01 10:34:56.999Z',
|
|
||||||
});
|
|
||||||
|
|
||||||
const rNote = await getEtapi(`notes/${note.noteId}`);
|
|
||||||
expect(rNote.title).toEqual("new title");
|
|
||||||
expect(rNote.type).toEqual("code");
|
|
||||||
expect(rNote.mime).toEqual("text/apl");
|
|
||||||
expect(rNote.dateCreated).toEqual("2000-01-01 12:34:56.999+0200");
|
|
||||||
expect(rNote.utcDateCreated).toEqual("2000-01-01 10:34:56.999Z");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("update content", async () => {
|
|
||||||
const {note} = await postEtapi('create-note', {
|
|
||||||
parentNoteId: 'root',
|
|
||||||
type: 'text',
|
|
||||||
title: 'Hello World!',
|
|
||||||
content: 'Content'
|
|
||||||
});
|
|
||||||
|
|
||||||
await putEtapiContent(`notes/${note.noteId}/content`, "new content");
|
|
||||||
|
|
||||||
const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).text();
|
|
||||||
expect(rContent).toEqual("new content");
|
|
||||||
});
|
|
||||||
|
|
||||||
it("create / update binary content", async () => {
|
|
||||||
const {note} = await postEtapi('create-note', {
|
|
||||||
parentNoteId: 'root',
|
|
||||||
type: 'file',
|
|
||||||
title: 'Hello World!',
|
|
||||||
content: 'ZZZ'
|
|
||||||
});
|
|
||||||
|
|
||||||
const updatedContent = crypto.randomBytes(16);
|
|
||||||
|
|
||||||
await putEtapiContent(`notes/${note.noteId}/content`, updatedContent);
|
|
||||||
|
|
||||||
const rContent = await (await getEtapiContent(`notes/${note.noteId}/content`)).arrayBuffer();
|
|
||||||
expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent);
|
|
||||||
});
|
|
||||||
|
|
||||||
it("delete note", async () => {
|
|
||||||
const {note} = await postEtapi('create-note', {
|
|
||||||
parentNoteId: 'root',
|
|
||||||
type: 'text',
|
|
||||||
title: 'Hello World!',
|
|
||||||
content: 'Content'
|
|
||||||
});
|
|
||||||
|
|
||||||
await deleteEtapi(`notes/${note.noteId}`);
|
|
||||||
|
|
||||||
const resp = await getEtapiResponse(`notes/${note.noteId}`);
|
|
||||||
expect(resp.status).toEqual(404);
|
|
||||||
|
|
||||||
const error = await resp.json();
|
|
||||||
expect(error.status).toEqual(404);
|
|
||||||
expect(error.code).toEqual("NOTE_NOT_FOUND");
|
|
||||||
expect(error.message).toEqual(`Note '${note.noteId}' not found.`);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
@ -0,0 +1,107 @@
|
|||||||
|
import crypto = require("crypto");
|
||||||
|
import etapi = require("../support/etapi");
|
||||||
|
|
||||||
|
etapi.describeEtapi("notes", () => {
|
||||||
|
it("create", async () => {
|
||||||
|
const { note, branch } = await etapi.postEtapi("create-note", {
|
||||||
|
parentNoteId: "root",
|
||||||
|
type: "text",
|
||||||
|
title: "Hello World!",
|
||||||
|
content: "Content",
|
||||||
|
prefix: "Custom prefix",
|
||||||
|
});
|
||||||
|
|
||||||
|
expect(note.title).toEqual("Hello World!");
|
||||||
|
expect(branch.parentNoteId).toEqual("root");
|
||||||
|
expect(branch.prefix).toEqual("Custom prefix");
|
||||||
|
|
||||||
|
const rNote = await etapi.getEtapi(`notes/${note.noteId}`);
|
||||||
|
expect(rNote.title).toEqual("Hello World!");
|
||||||
|
|
||||||
|
const rContent = await (
|
||||||
|
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
|
||||||
|
).text();
|
||||||
|
expect(rContent).toEqual("Content");
|
||||||
|
|
||||||
|
const rBranch = await etapi.getEtapi(`branches/${branch.branchId}`);
|
||||||
|
expect(rBranch.parentNoteId).toEqual("root");
|
||||||
|
expect(rBranch.prefix).toEqual("Custom prefix");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("patch", async () => {
|
||||||
|
const { note } = await etapi.postEtapi("create-note", {
|
||||||
|
parentNoteId: "root",
|
||||||
|
type: "text",
|
||||||
|
title: "Hello World!",
|
||||||
|
content: "Content",
|
||||||
|
});
|
||||||
|
|
||||||
|
await etapi.patchEtapi(`notes/${note.noteId}`, {
|
||||||
|
title: "new title",
|
||||||
|
type: "code",
|
||||||
|
mime: "text/apl",
|
||||||
|
dateCreated: "2000-01-01 12:34:56.999+0200",
|
||||||
|
utcDateCreated: "2000-01-01 10:34:56.999Z",
|
||||||
|
});
|
||||||
|
|
||||||
|
const rNote = await etapi.getEtapi(`notes/${note.noteId}`);
|
||||||
|
expect(rNote.title).toEqual("new title");
|
||||||
|
expect(rNote.type).toEqual("code");
|
||||||
|
expect(rNote.mime).toEqual("text/apl");
|
||||||
|
expect(rNote.dateCreated).toEqual("2000-01-01 12:34:56.999+0200");
|
||||||
|
expect(rNote.utcDateCreated).toEqual("2000-01-01 10:34:56.999Z");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("update content", async () => {
|
||||||
|
const { note } = await etapi.postEtapi("create-note", {
|
||||||
|
parentNoteId: "root",
|
||||||
|
type: "text",
|
||||||
|
title: "Hello World!",
|
||||||
|
content: "Content",
|
||||||
|
});
|
||||||
|
|
||||||
|
await etapi.putEtapiContent(`notes/${note.noteId}/content`, "new content");
|
||||||
|
|
||||||
|
const rContent = await (
|
||||||
|
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
|
||||||
|
).text();
|
||||||
|
expect(rContent).toEqual("new content");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("create / update binary content", async () => {
|
||||||
|
const { note } = await etapi.postEtapi("create-note", {
|
||||||
|
parentNoteId: "root",
|
||||||
|
type: "file",
|
||||||
|
title: "Hello World!",
|
||||||
|
content: "ZZZ",
|
||||||
|
});
|
||||||
|
|
||||||
|
const updatedContent = crypto.randomBytes(16);
|
||||||
|
|
||||||
|
await etapi.putEtapiContent(`notes/${note.noteId}/content`, updatedContent);
|
||||||
|
|
||||||
|
const rContent = await (
|
||||||
|
await etapi.getEtapiContent(`notes/${note.noteId}/content`)
|
||||||
|
).arrayBuffer();
|
||||||
|
expect(Buffer.from(new Uint8Array(rContent))).toEqual(updatedContent);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("delete note", async () => {
|
||||||
|
const { note } = await etapi.postEtapi("create-note", {
|
||||||
|
parentNoteId: "root",
|
||||||
|
type: "text",
|
||||||
|
title: "Hello World!",
|
||||||
|
content: "Content",
|
||||||
|
});
|
||||||
|
|
||||||
|
await etapi.deleteEtapi(`notes/${note.noteId}`);
|
||||||
|
|
||||||
|
const resp = await etapi.getEtapiResponse(`notes/${note.noteId}`);
|
||||||
|
expect(resp.status).toEqual(404);
|
||||||
|
|
||||||
|
const error = await resp.json();
|
||||||
|
expect(error.status).toEqual(404);
|
||||||
|
expect(error.code).toEqual("NOTE_NOT_FOUND");
|
||||||
|
expect(error.message).toEqual(`Note '${note.noteId}' not found.`);
|
||||||
|
});
|
||||||
|
});
|
||||||
@ -1,184 +0,0 @@
|
|||||||
const {spawn} = require("child_process");
|
|
||||||
const kill = require('tree-kill');
|
|
||||||
|
|
||||||
let etapiAuthToken;
|
|
||||||
|
|
||||||
const getEtapiAuthorizationHeader = () => "Basic " + Buffer.from(`etapi:${etapiAuthToken}`).toString('base64');
|
|
||||||
|
|
||||||
const PORT = '9999';
|
|
||||||
const HOST = 'http://localhost:' + PORT;
|
|
||||||
|
|
||||||
function describeEtapi(description, specDefinitions) {
|
|
||||||
describe(description, () => {
|
|
||||||
let appProcess;
|
|
||||||
|
|
||||||
beforeAll(async () => {
|
|
||||||
appProcess = spawn('npm', ['run', 'start-test-server']);
|
|
||||||
|
|
||||||
await new Promise(res => {
|
|
||||||
appProcess.stdout.on('data', data => {
|
|
||||||
console.log("Trilium: " + data.toString().trim());
|
|
||||||
|
|
||||||
if (data.toString().includes('Listening on port')) {
|
|
||||||
res();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
||||||
await fetch(HOST + '/api/setup/new-document', { method: 'POST' });
|
|
||||||
|
|
||||||
const formData = new URLSearchParams();
|
|
||||||
formData.append('password1', '1234');
|
|
||||||
formData.append('password2', '1234');
|
|
||||||
|
|
||||||
await fetch(HOST + '/set-password', { method: 'POST', body: formData });
|
|
||||||
|
|
||||||
etapiAuthToken = (await (await fetch(HOST + '/etapi/auth/login', {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
},
|
|
||||||
body: JSON.stringify({ password: '1234' })
|
|
||||||
})).json()).authToken;
|
|
||||||
});
|
|
||||||
|
|
||||||
afterAll(() => {
|
|
||||||
console.log("Attempting to kill the Trilium process as part of the cleanup...");
|
|
||||||
kill(appProcess.pid, 'SIGKILL', () => { console.log("Trilium process killed.") });
|
|
||||||
});
|
|
||||||
|
|
||||||
specDefinitions();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getEtapiResponse(url) {
|
|
||||||
return await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getEtapi(url) {
|
|
||||||
const response = await getEtapiResponse(url);
|
|
||||||
return await processEtapiResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getEtapiContent(url) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'GET',
|
|
||||||
headers: {
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
checkStatus(response);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function postEtapi(url, data = {}) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
return await processEtapiResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function postEtapiContent(url, data) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
},
|
|
||||||
body: data
|
|
||||||
});
|
|
||||||
|
|
||||||
checkStatus(response);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function putEtapi(url, data = {}) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
return await processEtapiResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function putEtapiContent(url, data) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'PUT',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/octet-stream",
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
},
|
|
||||||
body: data
|
|
||||||
});
|
|
||||||
|
|
||||||
checkStatus(response);
|
|
||||||
|
|
||||||
return response;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function patchEtapi(url, data = {}) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'PATCH',
|
|
||||||
headers: {
|
|
||||||
"Content-Type": "application/json",
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
},
|
|
||||||
body: JSON.stringify(data)
|
|
||||||
});
|
|
||||||
return await processEtapiResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function deleteEtapi(url) {
|
|
||||||
const response = await fetch(`${HOST}/etapi/${url}`, {
|
|
||||||
method: 'DELETE',
|
|
||||||
headers: {
|
|
||||||
Authorization: getEtapiAuthorizationHeader()
|
|
||||||
}
|
|
||||||
});
|
|
||||||
return await processEtapiResponse(response);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function processEtapiResponse(response) {
|
|
||||||
const text = await response.text();
|
|
||||||
|
|
||||||
if (response.status < 200 || response.status >= 300) {
|
|
||||||
throw new Error(`ETAPI error ${response.status}: ` + text);
|
|
||||||
}
|
|
||||||
|
|
||||||
return text?.trim() ? JSON.parse(text) : null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function checkStatus(response) {
|
|
||||||
if (response.status < 200 || response.status >= 300) {
|
|
||||||
throw new Error(`ETAPI error ${response.status}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
module.exports = {
|
|
||||||
describeEtapi,
|
|
||||||
getEtapi,
|
|
||||||
getEtapiResponse,
|
|
||||||
getEtapiContent,
|
|
||||||
postEtapi,
|
|
||||||
postEtapiContent,
|
|
||||||
putEtapi,
|
|
||||||
putEtapiContent,
|
|
||||||
patchEtapi,
|
|
||||||
deleteEtapi
|
|
||||||
};
|
|
||||||
@ -0,0 +1,224 @@
|
|||||||
|
import child_process = require("child_process");
|
||||||
|
import kill = require("tree-kill");
|
||||||
|
|
||||||
|
let etapiAuthToken: string | undefined;
|
||||||
|
|
||||||
|
const getEtapiAuthorizationHeader = (): string =>
|
||||||
|
"Basic " + Buffer.from(`etapi:${etapiAuthToken}`).toString("base64");
|
||||||
|
|
||||||
|
const PORT: string = "9999";
|
||||||
|
const HOST: string = "http://localhost:" + PORT;
|
||||||
|
|
||||||
|
type SpecDefinitionsFunc = () => void;
|
||||||
|
|
||||||
|
function describeEtapi(
|
||||||
|
description: string,
|
||||||
|
specDefinitions: SpecDefinitionsFunc
|
||||||
|
): void {
|
||||||
|
describe(description, () => {
|
||||||
|
let appProcess: ReturnType<typeof child_process.spawn>;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
appProcess = child_process.spawn("npm", ["run", "start-test-server"]);
|
||||||
|
if (!appProcess) {
|
||||||
|
throw new Error("Failed to start the Trilium process.");
|
||||||
|
}
|
||||||
|
|
||||||
|
await new Promise<void>((res) => {
|
||||||
|
appProcess.stdout!.on("data", (data) => {
|
||||||
|
console.log("Trilium: " + data.toString().trim());
|
||||||
|
|
||||||
|
if (data.toString().includes("Listening on port")) {
|
||||||
|
res();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
await fetch(`${HOST}/api/setup/new-document`, { method: "POST" });
|
||||||
|
|
||||||
|
const formData = new URLSearchParams();
|
||||||
|
formData.append("password1", "1234");
|
||||||
|
formData.append("password2", "1234");
|
||||||
|
|
||||||
|
await fetch(`${HOST}/set-password`, { method: "POST", body: formData });
|
||||||
|
|
||||||
|
etapiAuthToken = (
|
||||||
|
await (
|
||||||
|
await fetch(`${HOST}/etapi/auth/login`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ password: "1234" }),
|
||||||
|
})
|
||||||
|
).json()
|
||||||
|
).authToken;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(() => {
|
||||||
|
console.log(
|
||||||
|
"Attempting to kill the Trilium process as part of the cleanup..."
|
||||||
|
);
|
||||||
|
if (!appProcess.pid) {
|
||||||
|
console.log("Trilium process not found. Cannot kill.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
kill(appProcess.pid, "SIGKILL", (error) => {
|
||||||
|
if (error) {
|
||||||
|
console.error("Failed to kill the Trilium process.", error);
|
||||||
|
}
|
||||||
|
console.log("Trilium process killed.");
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
specDefinitions();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getEtapiResponse(url: string): Promise<Response> {
|
||||||
|
return await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getEtapi(url: string): Promise<any> {
|
||||||
|
const response = await getEtapiResponse(url);
|
||||||
|
return await processEtapiResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function getEtapiContent(url: string): Promise<Response> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "GET",
|
||||||
|
headers: {
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
checkStatus(response);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postEtapi(
|
||||||
|
url: string,
|
||||||
|
data: Record<string, unknown> = {}
|
||||||
|
): Promise<any> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await processEtapiResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function postEtapiContent(
|
||||||
|
url: string,
|
||||||
|
data: BodyInit
|
||||||
|
): Promise<Response> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/octet-stream",
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
body: data,
|
||||||
|
});
|
||||||
|
|
||||||
|
checkStatus(response);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function putEtapi(
|
||||||
|
url: string,
|
||||||
|
data: Record<string, unknown> = {}
|
||||||
|
): Promise<any> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await processEtapiResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function putEtapiContent(
|
||||||
|
url: string,
|
||||||
|
data?: BodyInit
|
||||||
|
): Promise<Response> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "PUT",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/octet-stream",
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
body: data,
|
||||||
|
});
|
||||||
|
|
||||||
|
checkStatus(response);
|
||||||
|
|
||||||
|
return response;
|
||||||
|
}
|
||||||
|
|
||||||
|
async function patchEtapi(
|
||||||
|
url: string,
|
||||||
|
data: Record<string, unknown> = {}
|
||||||
|
): Promise<any> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "PATCH",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
body: JSON.stringify(data),
|
||||||
|
});
|
||||||
|
return await processEtapiResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function deleteEtapi(url: string): Promise<any> {
|
||||||
|
const response = await fetch(`${HOST}/etapi/${url}`, {
|
||||||
|
method: "DELETE",
|
||||||
|
headers: {
|
||||||
|
Authorization: getEtapiAuthorizationHeader(),
|
||||||
|
},
|
||||||
|
});
|
||||||
|
return await processEtapiResponse(response);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function processEtapiResponse(response: Response): Promise<any> {
|
||||||
|
const text = await response.text();
|
||||||
|
|
||||||
|
if (response.status < 200 || response.status >= 300) {
|
||||||
|
throw new Error(`ETAPI error ${response.status}: ${text}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return text?.trim() ? JSON.parse(text) : null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function checkStatus(response: Response): void {
|
||||||
|
if (response.status < 200 || response.status >= 300) {
|
||||||
|
throw new Error(`ETAPI error ${response.status}`);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export {
|
||||||
|
describeEtapi,
|
||||||
|
getEtapi,
|
||||||
|
getEtapiResponse,
|
||||||
|
getEtapiContent,
|
||||||
|
postEtapi,
|
||||||
|
postEtapiContent,
|
||||||
|
putEtapi,
|
||||||
|
putEtapiContent,
|
||||||
|
patchEtapi,
|
||||||
|
deleteEtapi,
|
||||||
|
};
|
||||||
@ -1,12 +1,7 @@
|
|||||||
{
|
{
|
||||||
"spec_dir": "spec",
|
"spec_dir": "spec",
|
||||||
"spec_files": [
|
"spec_files": ["./etapi/*.ts"],
|
||||||
"**/*[sS]pec.js",
|
"helpers": ["helpers/**/*.js"],
|
||||||
"**/*[sS]pec.mjs"
|
|
||||||
],
|
|
||||||
"helpers": [
|
|
||||||
"helpers/**/*.js"
|
|
||||||
],
|
|
||||||
"stopSpecOnExpectationFailure": false,
|
"stopSpecOnExpectationFailure": false,
|
||||||
"random": true
|
"random": true
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,19 @@
|
|||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"moduleResolution": "Node",
|
"moduleResolution": "Node",
|
||||||
"declaration": false,
|
"declaration": false,
|
||||||
"sourceMap": true,
|
"sourceMap": true,
|
||||||
"outDir": "./dist",
|
"outDir": "./dist",
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"noImplicitAny": true,
|
"noImplicitAny": true,
|
||||||
"resolveJsonModule": true,
|
"resolveJsonModule": true,
|
||||||
"lib": ["ES2022"],
|
"lib": ["ES2022"],
|
||||||
"downlevelIteration": true
|
"downlevelIteration": true
|
||||||
},
|
},
|
||||||
"include": [
|
"include": ["./src/**/*.js", "./src/**/*.ts", "./spec/**/*.ts"],
|
||||||
"./src/**/*.js",
|
"exclude": ["./node_modules/**/*"],
|
||||||
"./src/**/*.ts"
|
"ts-node": {
|
||||||
],
|
"files": true
|
||||||
"exclude": ["./node_modules/**/*"],
|
},
|
||||||
"ts-node": {
|
"files": ["src/types.d.ts"]
|
||||||
"files": true
|
}
|
||||||
},
|
|
||||||
"files": [
|
|
||||||
"src/types.d.ts"
|
|
||||||
]
|
|
||||||
}
|
|
||||||
|
|||||||
Loading…
Reference in New Issue