feat: enhance group duty pin command functionality
All checks were successful
CI / lint-and-test (push) Successful in 25s
Docker Build and Release / build-and-push (push) Successful in 56s
Docker Build and Release / release (push) Successful in 9s

- Updated the `pin_duty_cmd` to handle cases where no message ID is found by sending a new duty message, pinning it, saving the pin, and scheduling the next update.
- Improved error handling for message sending and pinning operations, providing appropriate replies based on success or failure.
- Enhanced unit tests to cover the new behavior, ensuring proper functionality and error handling in various scenarios.
This commit is contained in:
2026-02-25 14:43:19 +03:00
parent 3c3a2c507c
commit 8a80af32d8
8 changed files with 286 additions and 9 deletions

View File

@@ -52,7 +52,9 @@ export function buildDayDetailContent(dateKey, duties, eventSummaries) {
? t(lang, "duty.today") + ", " + ddmm
: ddmm;
const dutyList = (duties || []).filter((d) => d.event_type === "duty");
const dutyList = (duties || [])
.filter((d) => d.event_type === "duty")
.sort((a, b) => new Date(a.start_at || 0) - new Date(b.start_at || 0));
const unavailableList = (duties || []).filter((d) => d.event_type === "unavailable");
const vacationList = (duties || []).filter((d) => d.event_type === "vacation");
const summaries = eventSummaries || [];

View File

@@ -0,0 +1,41 @@
/**
* Unit tests for buildDayDetailContent.
* Verifies dutyList is sorted by start_at before display.
*/
import { describe, it, expect, beforeAll } from "vitest";
import { buildDayDetailContent } from "./dayDetail.js";
describe("buildDayDetailContent", () => {
beforeAll(() => {
document.body.innerHTML =
'<div id="calendar"></div><div id="monthTitle"></div>' +
'<div id="dutyList"></div><div id="loading"></div><div id="error"></div>' +
'<div id="accessDenied"></div><div class="header"></div><div class="weekdays"></div>' +
'<button id="prevMonth"></button><button id="nextMonth"></button>';
});
it("sorts duty list by start_at when input order is wrong", () => {
const dateKey = "2025-02-25";
const duties = [
{
event_type: "duty",
full_name: "Петров",
start_at: "2025-02-25T14:00:00",
end_at: "2025-02-25T18:00:00",
},
{
event_type: "duty",
full_name: "Иванов",
start_at: "2025-02-25T09:00:00",
end_at: "2025-02-25T14:00:00",
},
];
const html = buildDayDetailContent(dateKey, duties, []);
expect(html).toContain("Иванов");
expect(html).toContain("Петров");
const ivanovPos = html.indexOf("Иванов");
const petrovPos = html.indexOf("Петров");
expect(ivanovPos).toBeLessThan(petrovPos);
});
});

View File

@@ -115,7 +115,14 @@ function buildDutyItemTimePrefix(item, idx, total, hintDay, sep, fromLabel, toLa
if (endSameDay && endHHMM && endHHMM !== startHHMM) {
timePrefix += " " + toLabel + sep + endHHMM;
}
} else if (startSameDay && startHHMM) {
/* First of multiple, but starts today — show full range */
timePrefix = fromLabel + sep + startHHMM;
if (endSameDay && endHHMM && endHHMM !== startHHMM) {
timePrefix += " " + toLabel + sep + endHHMM;
}
} else if (endHHMM) {
/* Continuation from previous day — only end time */
timePrefix = toLabel + sep + endHHMM;
}
} else if (idx > 0) {

99
webapp/js/hints.test.js Normal file
View File

@@ -0,0 +1,99 @@
/**
* Unit tests for getDutyMarkerRows and buildDutyItemTimePrefix logic.
* Covers: sorting order preservation, idx=0 with total>1 and startSameDay.
*/
import { describe, it, expect, beforeAll } from "vitest";
import { getDutyMarkerRows } from "./hints.js";
const FROM = "from";
const TO = "until";
const SEP = "\u00a0";
describe("getDutyMarkerRows", () => {
beforeAll(() => {
document.body.innerHTML = '<div id="calendar"></div>';
});
it("preserves input order (caller must sort by start_at before passing)", () => {
const hintDay = "2025-02-25";
const duties = [
{
full_name: "Иванов",
start_at: "2025-02-25T14:00:00",
end_at: "2025-02-25T18:00:00",
},
{
full_name: "Петров",
start_at: "2025-02-25T09:00:00",
end_at: "2025-02-25T14:00:00",
},
];
const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO);
expect(rows).toHaveLength(2);
expect(rows[0].fullName).toBe("Иванов");
expect(rows[1].fullName).toBe("Петров");
});
it("first of multiple with startSameDay shows full range (from HH:MM to HH:MM)", () => {
const hintDay = "2025-02-25";
const duties = [
{
full_name: "Иванов",
start_at: "2025-02-25T09:00:00",
end_at: "2025-02-25T14:00:00",
},
{
full_name: "Петров",
start_at: "2025-02-25T14:00:00",
end_at: "2025-02-25T18:00:00",
},
].sort((a, b) => new Date(a.start_at) - new Date(b.start_at));
const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO);
expect(rows).toHaveLength(2);
expect(rows[0].fullName).toBe("Иванов");
expect(rows[0].timePrefix).toContain("09:00");
expect(rows[0].timePrefix).toContain("14:00");
expect(rows[0].timePrefix).toContain(FROM);
expect(rows[0].timePrefix).toContain(TO);
});
it("first of multiple continuation from previous day shows only end time", () => {
const hintDay = "2025-02-25";
const duties = [
{
full_name: "Иванов",
start_at: "2025-02-24T22:00:00",
end_at: "2025-02-25T06:00:00",
},
{
full_name: "Петров",
start_at: "2025-02-25T09:00:00",
end_at: "2025-02-25T14:00:00",
},
].sort((a, b) => new Date(a.start_at) - new Date(b.start_at));
const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO);
expect(rows).toHaveLength(2);
expect(rows[0].fullName).toBe("Иванов");
expect(rows[0].timePrefix).not.toContain(FROM);
expect(rows[0].timePrefix).toContain(TO);
expect(rows[0].timePrefix).toContain("06:00");
});
it("multiple duties in one day — correct order when input is pre-sorted", () => {
const hintDay = "2025-02-25";
const duties = [
{ full_name: "A", start_at: "2025-02-25T09:00:00", end_at: "2025-02-25T12:00:00" },
{ full_name: "B", start_at: "2025-02-25T12:00:00", end_at: "2025-02-25T15:00:00" },
{ full_name: "C", start_at: "2025-02-25T15:00:00", end_at: "2025-02-25T18:00:00" },
].sort((a, b) => new Date(a.start_at) - new Date(b.start_at));
const rows = getDutyMarkerRows(duties, hintDay, SEP, FROM, TO);
expect(rows.map((r) => r.fullName)).toEqual(["A", "B", "C"]);
expect(rows[0].timePrefix).toContain("09:00");
expect(rows[1].timePrefix).toContain("12:00");
expect(rows[2].timePrefix).toContain("15:00");
});
});

12
webapp/package.json Normal file
View File

@@ -0,0 +1,12 @@
{
"name": "duty-teller-webapp",
"type": "module",
"scripts": {
"test": "vitest run",
"test:watch": "vitest"
},
"devDependencies": {
"happy-dom": "^15.0.0",
"vitest": "^2.0.0"
}
}

6
webapp/vitest.config.js Normal file
View File

@@ -0,0 +1,6 @@
export default {
test: {
environment: "happy-dom",
include: ["js/**/*.test.js"],
},
};