feat: add Drone CI pipelines, Jest unit tests, and Prettier check
Some checks failed
continuous-integration/drone/push Build encountered an error
Some checks failed
continuous-integration/drone/push Build encountered an error
Add Drone CI with server build/test and format check pipelines. Add unit tests for password utils and recurrenceExpander. Add check_format script, fix Jest config to ignore dist/, remove dead CaldavService.test.ts, apply Prettier formatting.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
module.exports = {
|
||||
preset: "ts-jest",
|
||||
testEnvironment: "node",
|
||||
testPathIgnorePatterns: ["/node_modules/", "/dist/"],
|
||||
};
|
||||
|
||||
@@ -21,5 +21,4 @@ export class AuthController {
|
||||
res.status(400).json({ error: (error as Error).message });
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -15,7 +15,10 @@ export class CaldavController {
|
||||
const response = await this.caldavService.saveConfig(config);
|
||||
res.json(response);
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error saving config");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error saving config",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to save config" });
|
||||
}
|
||||
}
|
||||
@@ -30,7 +33,10 @@ export class CaldavController {
|
||||
// Don't expose the password to the client
|
||||
res.json(config);
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error loading config");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error loading config",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to load config" });
|
||||
}
|
||||
}
|
||||
@@ -40,7 +46,10 @@ export class CaldavController {
|
||||
await this.caldavService.deleteConfig(req.user!.userId);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error deleting config");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error deleting config",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to delete config" });
|
||||
}
|
||||
}
|
||||
@@ -50,7 +59,10 @@ export class CaldavController {
|
||||
const events = await this.caldavService.pullEvents(req.user!.userId);
|
||||
res.json(events);
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error pulling events");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error pulling events",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to pull events" });
|
||||
}
|
||||
}
|
||||
@@ -60,7 +72,10 @@ export class CaldavController {
|
||||
await this.caldavService.pushAll(req.user!.userId);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error pushing events");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error pushing events",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to push events" });
|
||||
}
|
||||
}
|
||||
@@ -78,7 +93,10 @@ export class CaldavController {
|
||||
await this.caldavService.pushEvent(req.user!.userId, event);
|
||||
res.status(204).send();
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error pushing event");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error pushing event",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to push event" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,7 +40,10 @@ export class EventController {
|
||||
await this.pushToCaldav(userId, event);
|
||||
res.status(201).json(event);
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error creating event");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error creating event",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to create event" });
|
||||
}
|
||||
}
|
||||
@@ -67,7 +70,10 @@ export class EventController {
|
||||
const events = await this.eventService.getAll(req.user!.userId);
|
||||
res.json(events);
|
||||
} catch (error) {
|
||||
log.error({ err: error, userId: req.user?.userId }, "Error getting events");
|
||||
log.error(
|
||||
{ err: error, userId: req.user?.userId },
|
||||
"Error getting events",
|
||||
);
|
||||
res.status(500).json({ error: "Failed to get events" });
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,5 +44,4 @@ export class AuthService {
|
||||
|
||||
return { user, accessToken: "" };
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,11 +0,0 @@
|
||||
// import { createLogger } from "../logging";
|
||||
// import { CaldavService } from "./CaldavService";
|
||||
//
|
||||
// const logger = createLogger("CaldavService-Test");
|
||||
//
|
||||
// const cdService = new CaldavService();
|
||||
//
|
||||
// test("print events", async () => {
|
||||
// const client = await cdService.login();
|
||||
// await cdService.pullEvents(client);
|
||||
// });
|
||||
37
apps/server/src/utils/password.test.ts
Normal file
37
apps/server/src/utils/password.test.ts
Normal file
@@ -0,0 +1,37 @@
|
||||
import { hash, compare } from "./password";
|
||||
|
||||
describe("password", () => {
|
||||
describe("hash()", () => {
|
||||
it("returns a valid bcrypt hash", async () => {
|
||||
const result = await hash("testpassword");
|
||||
expect(result).toMatch(/^\$2b\$/);
|
||||
});
|
||||
|
||||
it("produces different hashes for the same password (salt)", async () => {
|
||||
const hash1 = await hash("samepassword");
|
||||
const hash2 = await hash("samepassword");
|
||||
expect(hash1).not.toBe(hash2);
|
||||
});
|
||||
});
|
||||
|
||||
describe("compare()", () => {
|
||||
it("returns true for the correct password", async () => {
|
||||
const hashed = await hash("correct");
|
||||
const result = await compare("correct", hashed);
|
||||
expect(result).toBe(true);
|
||||
});
|
||||
|
||||
it("returns false for a wrong password", async () => {
|
||||
const hashed = await hash("correct");
|
||||
const result = await compare("wrong", hashed);
|
||||
expect(result).toBe(false);
|
||||
});
|
||||
|
||||
it("handles special characters and unicode", async () => {
|
||||
const password = "p@$$w0rd!#%& äöü 🔑";
|
||||
const hashed = await hash(password);
|
||||
expect(await compare(password, hashed)).toBe(true);
|
||||
expect(await compare("other", hashed)).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
210
apps/server/src/utils/recurrenceExpander.test.ts
Normal file
210
apps/server/src/utils/recurrenceExpander.test.ts
Normal file
@@ -0,0 +1,210 @@
|
||||
import { CalendarEvent } from "@calchat/shared";
|
||||
import { expandRecurringEvents } from "./recurrenceExpander";
|
||||
|
||||
// Helper: create a CalendarEvent with sensible defaults
|
||||
function makeEvent(
|
||||
overrides: Partial<CalendarEvent> & { startTime: Date; endTime: Date },
|
||||
): CalendarEvent {
|
||||
return {
|
||||
id: "evt-1",
|
||||
userId: "user-1",
|
||||
title: "Test Event",
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
// Helper: create a date from "YYYY-MM-DD HH:mm" (local time)
|
||||
function d(dateStr: string): Date {
|
||||
const [datePart, timePart] = dateStr.split(" ");
|
||||
const [y, m, day] = datePart.split("-").map(Number);
|
||||
if (timePart) {
|
||||
const [h, min] = timePart.split(":").map(Number);
|
||||
return new Date(y, m - 1, day, h, min);
|
||||
}
|
||||
return new Date(y, m - 1, day);
|
||||
}
|
||||
|
||||
describe("expandRecurringEvents", () => {
|
||||
// Range: 2025-06-01 to 2025-06-30
|
||||
const rangeStart = d("2025-06-01 00:00");
|
||||
const rangeEnd = d("2025-06-30 23:59");
|
||||
|
||||
describe("non-recurring events", () => {
|
||||
it("returns event within the range", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-10 09:00"),
|
||||
endTime: d("2025-06-10 10:00"),
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].occurrenceStart).toEqual(d("2025-06-10 09:00"));
|
||||
expect(result[0].occurrenceEnd).toEqual(d("2025-06-10 10:00"));
|
||||
});
|
||||
|
||||
it("excludes event outside the range", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-07-05 09:00"),
|
||||
endTime: d("2025-07-05 10:00"),
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(0);
|
||||
});
|
||||
|
||||
it("includes event that starts before range and ends within", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-05-31 22:00"),
|
||||
endTime: d("2025-06-01 02:00"),
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("includes event that spans the entire range", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-05-01 00:00"),
|
||||
endTime: d("2025-07-31 23:59"),
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("returns empty array for empty input", () => {
|
||||
const result = expandRecurringEvents([], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("recurring events", () => {
|
||||
it("expands weekly event to all occurrences in range", () => {
|
||||
// Weekly on Mondays, starting 2025-06-02 (a Monday)
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-02 10:00"),
|
||||
endTime: d("2025-06-02 11:00"),
|
||||
recurrenceRule: "FREQ=WEEKLY;BYDAY=MO",
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
// Mondays in June 2025: 2, 9, 16, 23, 30
|
||||
expect(result).toHaveLength(5);
|
||||
expect(result[0].occurrenceStart).toEqual(d("2025-06-02 10:00"));
|
||||
expect(result[1].occurrenceStart).toEqual(d("2025-06-09 10:00"));
|
||||
expect(result[2].occurrenceStart).toEqual(d("2025-06-16 10:00"));
|
||||
expect(result[3].occurrenceStart).toEqual(d("2025-06-23 10:00"));
|
||||
expect(result[4].occurrenceStart).toEqual(d("2025-06-30 10:00"));
|
||||
});
|
||||
|
||||
it("daily event with UNTIL stops at the right date", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-01 08:00"),
|
||||
endTime: d("2025-06-01 09:00"),
|
||||
recurrenceRule: "FREQ=DAILY;UNTIL=20250605T235959",
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
// June 1, 2, 3, 4, 5
|
||||
expect(result).toHaveLength(5);
|
||||
expect(result[4].occurrenceStart).toEqual(d("2025-06-05 08:00"));
|
||||
});
|
||||
|
||||
it("skips occurrences on exception dates (EXDATE)", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-02 10:00"),
|
||||
endTime: d("2025-06-02 11:00"),
|
||||
recurrenceRule: "FREQ=WEEKLY;BYDAY=MO",
|
||||
exceptionDates: ["2025-06-09", "2025-06-23"],
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
// 5 Mondays minus 2 exceptions = 3
|
||||
expect(result).toHaveLength(3);
|
||||
const dates = result.map((r) => r.occurrenceStart.getDate());
|
||||
expect(dates).toEqual([2, 16, 30]);
|
||||
});
|
||||
|
||||
it("handles RRULE: prefix (strips it)", () => {
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-01 08:00"),
|
||||
endTime: d("2025-06-01 09:00"),
|
||||
recurrenceRule: "RRULE:FREQ=DAILY;COUNT=3",
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(3);
|
||||
});
|
||||
|
||||
it("falls back to single occurrence on invalid RRULE", () => {
|
||||
const consoleSpy = jest.spyOn(console, "error").mockImplementation();
|
||||
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-06-10 09:00"),
|
||||
endTime: d("2025-06-10 10:00"),
|
||||
recurrenceRule: "COMPLETELY_INVALID_RULE",
|
||||
});
|
||||
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
expect(result).toHaveLength(1);
|
||||
expect(result[0].occurrenceStart).toEqual(d("2025-06-10 09:00"));
|
||||
expect(consoleSpy).toHaveBeenCalled();
|
||||
|
||||
consoleSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe("multi-day events", () => {
|
||||
it("finds event starting before range that ends within range", () => {
|
||||
// 3-day recurring event starting May 15, weekly
|
||||
const event = makeEvent({
|
||||
startTime: d("2025-05-15 08:00"),
|
||||
endTime: d("2025-05-18 08:00"),
|
||||
recurrenceRule: "FREQ=WEEKLY",
|
||||
});
|
||||
|
||||
// The occurrence starting May 29 ends June 1 → overlaps with range
|
||||
const result = expandRecurringEvents([event], rangeStart, rangeEnd);
|
||||
|
||||
const starts = result.map((r) => r.occurrenceStart.getDate());
|
||||
// May 29 (ends June 1), June 5, 12, 19, 26
|
||||
expect(starts).toContain(29); // May 29
|
||||
});
|
||||
});
|
||||
|
||||
describe("sorting", () => {
|
||||
it("returns events sorted by occurrenceStart", () => {
|
||||
const laterEvent = makeEvent({
|
||||
id: "evt-later",
|
||||
startTime: d("2025-06-20 14:00"),
|
||||
endTime: d("2025-06-20 15:00"),
|
||||
});
|
||||
const earlierEvent = makeEvent({
|
||||
id: "evt-earlier",
|
||||
startTime: d("2025-06-05 09:00"),
|
||||
endTime: d("2025-06-05 10:00"),
|
||||
});
|
||||
|
||||
// Pass in reverse order
|
||||
const result = expandRecurringEvents(
|
||||
[laterEvent, earlierEvent],
|
||||
rangeStart,
|
||||
rangeEnd,
|
||||
);
|
||||
|
||||
expect(result).toHaveLength(2);
|
||||
expect(result[0].id).toBe("evt-earlier");
|
||||
expect(result[1].id).toBe("evt-later");
|
||||
});
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user