From b88af5e4b3beaf71588cd9692db81557a58b6297 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Mon, 2 Jun 2025 19:02:01 +0300 Subject: [PATCH] test(etapi): port api-metrics --- _regroup/test-etapi/_login.http | 12 ------ _regroup/test-etapi/api-metrics.http | 43 ------------------- apps/server/spec/etapi/api-metrics.spec.ts | 48 ++++++++++++++++++++++ 3 files changed, 48 insertions(+), 55 deletions(-) delete mode 100644 _regroup/test-etapi/_login.http delete mode 100644 _regroup/test-etapi/api-metrics.http create mode 100644 apps/server/spec/etapi/api-metrics.spec.ts diff --git a/_regroup/test-etapi/_login.http b/_regroup/test-etapi/_login.http deleted file mode 100644 index 9976e7cd4..000000000 --- a/_regroup/test-etapi/_login.http +++ /dev/null @@ -1,12 +0,0 @@ -POST {{triliumHost}}/etapi/auth/login -Content-Type: application/json - -{ - "password": "1234" -} - -> {% - client.assert(response.status === 201); - - client.global.set("authToken", response.body.authToken); -%} diff --git a/_regroup/test-etapi/api-metrics.http b/_regroup/test-etapi/api-metrics.http deleted file mode 100644 index 78aee7217..000000000 --- a/_regroup/test-etapi/api-metrics.http +++ /dev/null @@ -1,43 +0,0 @@ -### Test regular API metrics endpoint (requires session authentication) - -### Get metrics from regular API (default Prometheus format) -GET {{triliumHost}}/api/metrics - -> {% -client.test("API metrics endpoint returns Prometheus format by default", function() { - client.assert(response.status === 200, "Response status is not 200"); - client.assert(response.headers["content-type"].includes("text/plain"), "Content-Type should be text/plain"); - client.assert(response.body.includes("trilium_info"), "Should contain trilium_info metric"); - client.assert(response.body.includes("trilium_notes_total"), "Should contain trilium_notes_total metric"); - client.assert(response.body.includes("# HELP"), "Should contain HELP comments"); - client.assert(response.body.includes("# TYPE"), "Should contain TYPE comments"); -}); -%} - -### Get metrics in JSON format -GET {{triliumHost}}/api/metrics?format=json - -> {% -client.test("API metrics endpoint returns JSON when requested", function() { - client.assert(response.status === 200, "Response status is not 200"); - client.assert(response.headers["content-type"].includes("application/json"), "Content-Type should be application/json"); - client.assert(response.body.version, "Version info not present"); - client.assert(response.body.database, "Database info not present"); - client.assert(response.body.timestamp, "Timestamp not present"); - client.assert(typeof response.body.database.totalNotes === 'number', "Total notes should be a number"); - client.assert(typeof response.body.database.activeNotes === 'number', "Active notes should be a number"); - client.assert(response.body.noteTypes, "Note types breakdown not present"); - client.assert(response.body.attachmentTypes, "Attachment types breakdown not present"); - client.assert(response.body.statistics, "Statistics not present"); -}); -%} - -### Test invalid format parameter -GET {{triliumHost}}/api/metrics?format=xml - -> {% -client.test("Invalid format parameter returns error", function() { - client.assert(response.status === 500, "Response status should be 500"); - client.assert(response.body.message.includes("prometheus"), "Error message should mention supported formats"); -}); -%} \ No newline at end of file diff --git a/apps/server/spec/etapi/api-metrics.spec.ts b/apps/server/spec/etapi/api-metrics.spec.ts new file mode 100644 index 000000000..a9c98df87 --- /dev/null +++ b/apps/server/spec/etapi/api-metrics.spec.ts @@ -0,0 +1,48 @@ +import { Application } from "express"; +import { beforeAll, describe, expect, it } from "vitest"; +import buildApp from "../../src/app.js"; +import supertest from "supertest"; + +let app: Application; +let token: string; + +// TODO: This is an API test, not ETAPI. + +describe("api/metrics", () => { + beforeAll(async () => { + app = await buildApp(); + }); + + it("returns Prometheus format by default", async () => { + const response = await supertest(app) + .get("/api/metrics") + .expect(200); + expect(response.headers["content-type"]).toContain("text/plain"); + expect(response.text).toContain("trilium_info"); + expect(response.text).toContain("trilium_notes_total"); + expect(response.text).toContain("# HELP"); + expect(response.text).toContain("# TYPE"); + }); + + it("returns JSON when requested", async() => { + const response = await supertest(app) + .get("/api/metrics?format=json") + .expect(200); + expect(response.headers["content-type"]).toContain("application/json"); + expect(response.body.version).toBeTruthy(); + expect(response.body.database).toBeTruthy(); + expect(response.body.timestamp).toBeTruthy(); + expect(response.body.database.totalNotes).toBeTypeOf("number"); + expect(response.body.database.activeNotes).toBeTypeOf("number"); + expect(response.body.noteTypes).toBeTruthy(); + expect(response.body.attachmentTypes).toBeTruthy(); + expect(response.body.statistics).toBeTruthy(); + }); + + it("returns error on invalid format", async() => { + const response = await supertest(app) + .get("/api/metrics?format=xml") + .expect(500); + expect(response.body.message).toContain("prometheus"); + }); +});