Public: anyone can read this; the crew can change it.

A .ics calendar file opens as a list of its events with date, title and place #14145

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

torque/forge:torque/14123-a-ics-calendar-file-opens-as-a-list-of-i → main 2 commits fork

  • Merged. torque/14123-a-ics-calendar-file-opens-as-a-list-of-i was merged into main as d31e7ff.

Review

Sign in to review this change.
3 files changed +66 −10
src/web/render/ics.ts +9 −2
@@ -1,6 +1,7 @@
11 /**
22 * A calendar file (iCalendar `.ics`, RFC 5545) as a list of its events: start,
3 * end (or "all day"), summary and location, in the file's own order. Lines are unfolded
3 * end (or "all day"), summary and location, sorted by start as written (the
4 * zone is named, never converted; equal starts keep the file's order). Lines are unfolded
45 * (§3.1), VEVENT blocks read, DTSTART/DTEND taken with or without TZID, and
56 * TEXT escapes (`\,` `\;` `\\` `\n`) undone. Every value is escaped on output.
67 * A file that is not a calendar falls back to its source; an event that cannot
@@ -12,7 +13,7 @@ import { fileSize } from "../ui.ts";
1213 import type { Rendered, RenderInput } from "./renderers.ts";
1314
1415 /** Events drawn, whatever the file holds. */
15 export const EVENT_CAP = 500;
16 export const EVENT_CAP = 200;
1617
1718 export interface IcsEvent {
1819 /** `2026-03-01 09:00`, `2026-03-01`, with a zone note when the file gave one. */
@@ -133,6 +134,12 @@ export function parseIcs(source: string): Calendar {
133134 }
134135 }
135136 if (props !== undefined) skipped++;
137 // `2026-03-01` or `2026-03-01 09:00`: the first 16 characters order as written.
138 events.sort((a, b) => {
139 const x = a.start.slice(0, 16);
140 const y = b.start.slice(0, 16);
141 return x < y ? -1 : x > y ? 1 : 0;
142 });
136143 return { events, skipped };
137144 }
138145
test/ics-render.test.ts added +49
@@ -0,0 +1,49 @@
1 /** The calendar view: sorted by start as written, folded lines, a hostile summary, the 200 cap. */
2 import { expect, test } from "bun:test";
3 import { EVENT_CAP, ics, parseIcs } from "../src/web/render/ics.ts";
4
5 const draw = (text: string) =>
6 ics({ path: "cal.ics", size: text.length, body: new TextEncoder().encode(text), media: "/m", download: "/d" });
7 const wrap = (...events: string[]) =>
8 `BEGIN:VCALENDAR\r\n${events.map((e) => `BEGIN:VEVENT\r\n${e}\r\nEND:VEVENT\r\n`).join("")}END:VCALENDAR\r\n`;
9
10 test("events are sorted by start as written; the zone is named, not converted", () => {
11 const cal = parseIcs(
12 wrap(
13 "DTSTART;TZID=Europe/Paris:20260302T140000\r\nSUMMARY:c",
14 "DTSTART:20260301T090000Z\r\nSUMMARY:b",
15 "DTSTART;VALUE=DATE:20260301\r\nSUMMARY:a",
16 ),
17 );
18 if ("error" in cal) throw new Error(cal.error);
19 expect(cal.events.map((e) => e.summary)).toEqual(["a", "b", "c"]);
20 expect(cal.events[1]!.start).toBe("2026-03-01 09:00 UTC");
21 expect(cal.events[2]!.start).toBe("2026-03-02 14:00 Europe/Paris");
22 });
23
24 test("folded lines and escaped commas read as one value", () => {
25 const cal = parseIcs(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:Plan\\, build and a long\r\n title\r\nLOCATION:Room\\, 1"));
26 if ("error" in cal) throw new Error(cal.error);
27 expect(cal.events[0]!.summary).toBe("Plan, build and a long title");
28 expect(cal.events[0]!.location).toBe("Room, 1");
29 });
30
31 test("a hostile summary and location are escaped", () => {
32 const out = String(draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:<script>alert(1)</script>\r\nLOCATION:\"><img src=x onerror=y>")).content);
33 expect(out).not.toContain("<script>");
34 expect(out).not.toContain("<img");
35 expect(out).toContain("&lt;script&gt;");
36 });
37
38 test("more than 200 events show the first 200 sorted, with a count", () => {
39 expect(EVENT_CAP).toBe(200);
40 const many = Array.from({ length: 205 }, (_, i) => `DTSTART:2026${String(12 - (i % 12)).padStart(2, "0")}01T090000Z\r\nSUMMARY:e${i}`);
41 const r = draw(wrap(...many));
42 const out = String(r.content);
43 expect(out.match(/<tr><td>/g)?.length).toBe(200);
44 expect(out).toContain("Showing 200 of 205 events, 5 more");
45 });
46
47 test("a file that is not a calendar falls back with one reason", () => {
48 expect(draw("hello").fallback).toContain("not a calendar");
49 });
test/render-ics.test.ts +8 −8
@@ -40,8 +40,8 @@ describe("calendar files", () => {
4040 );
4141 if ("error" in cal) throw new Error(cal.error);
4242 expect(cal.events.map((e) => [e.allDay, e.start, e.end])).toEqual([
43 [true, "2026-03-01", ""],
4443 [true, "2026-02-28", "2026-03-02"],
44 [true, "2026-03-01", ""],
4545 ]);
4646 const out = String(draw(wrap("DTSTART;VALUE=DATE:20260301\r\nSUMMARY:X")).content);
4747 expect(out).toContain("all day");
@@ -62,14 +62,14 @@ describe("calendar files", () => {
6262 expect(r.summary).toContain("1 event");
6363 });
6464
65 test("events keep the file's own order; non-VEVENT parts are ignored", () => {
65 test("events sort by start; non-VEVENT parts are ignored", () => {
6666 const cal = parseIcs(
6767 "BEGIN:VCALENDAR\r\nBEGIN:VTODO\r\nDTSTART:20250101T000000Z\r\nSUMMARY:todo\r\nEND:VTODO\r\n" +
6868 "BEGIN:VEVENT\r\nDTSTART:20260305T090000Z\r\nSUMMARY:late\r\nEND:VEVENT\r\n" +
6969 "BEGIN:VEVENT\r\nDTSTART;VALUE=DATE:20260301\r\nSUMMARY:early\r\nEND:VEVENT\r\nEND:VCALENDAR\r\n",
7070 );
7171 if ("error" in cal) throw new Error(cal.error);
72 expect(cal.events.map((e) => e.summary)).toEqual(["late", "early"]);
72 expect(cal.events.map((e) => e.summary)).toEqual(["early", "late"]);
7373 });
7474
7575 test("a malformed file shows the rows it could read and one line about the rest", () => {
@@ -85,12 +85,12 @@ describe("calendar files", () => {
8585 expect(String(open.content)).toContain("1 event could not be read");
8686 });
8787
88 test("events in different zones stay in file order, with no note", () => {
88 test("events in different zones sort as written, never converted", () => {
8989 const out = String(
9090 draw(wrap("DTSTART:20260301T090000Z\r\nSUMMARY:a", "DTSTART;TZID=Europe/Paris:20260301T080000\r\nSUMMARY:b")).content,
9191 );
9292 expect(out).not.toContain("time zones");
93 expect(out.indexOf(">a<")).toBeLessThan(out.indexOf(">b<"));
93 expect(out.indexOf(">b<")).toBeLessThan(out.indexOf(">a<"));
9494 });
9595
9696 test("malformed files fall back to text", () => {
@@ -101,11 +101,11 @@ describe("calendar files", () => {
101101 expect(draw("BEGIN:VCALENDAR\r\nEND:VCALENDAR\r\n").fallback).toContain("no events");
102102 });
103103
104 test("capped at 500 events, saying how many more", () => {
104 test("capped at 200 events, saying how many more", () => {
105105 const many = Array.from({ length: EVENT_CAP + 7 }, (_, i) => `DTSTART:20260301T090000Z\r\nSUMMARY:e${i}`);
106106 const out = String(draw(wrap(...many)).content);
107 expect(out).toContain("e499<");
108 expect(out).not.toContain("e500<");
107 expect(out).toContain("e199<");
108 expect(out).not.toContain("e200<");
109109 expect(out).toContain("7 more");
110110 });
111111 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs