|
| 1 | +import { mkdtempSync, mkdirSync, rmSync, writeFileSync } from "node:fs"; |
| 2 | +import { tmpdir } from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterAll, expect, test } from "bun:test"; |
| 5 | +import { commuteBrief, destinationPlaces, detectTrip, loadCommute, renderBrief, saveCommute } from "./commute.ts"; |
| 6 | + |
| 7 | +const dir = mkdtempSync(join(tmpdir(), "commute-")); |
| 8 | +afterAll(() => rmSync(dir, { recursive: true, force: true })); |
| 9 | + |
| 10 | +const HOME = { name: "home", lat: 1.5, lon: 2.5, radiusM: 100 }; |
| 11 | +const OFFICE = { name: "office", lat: 1.8, lon: 2.8, radiusM: 100 }; |
| 12 | +const OTHER = { name: "gym", lat: 5, lon: 5, radiusM: 100 }; |
| 13 | + |
| 14 | +test("destinationPlaces keeps only places named home or office", () => { |
| 15 | + expect(destinationPlaces([HOME, OFFICE, OTHER])).toEqual([HOME, OFFICE]); |
| 16 | + expect(destinationPlaces([OTHER])).toEqual([]); |
| 17 | + expect(destinationPlaces([{ ...HOME, name: "Home" }, { ...OFFICE, name: "OFFICE" }])).toEqual([ |
| 18 | + { ...HOME, name: "Home" }, |
| 19 | + { ...OFFICE, name: "OFFICE" }, |
| 20 | + ]); |
| 21 | +}); |
| 22 | + |
| 23 | +test("detectTrip needs a meaningfully closer fix outside the place", () => { |
| 24 | + const far = { latitude: 41, longitude: -74 }; |
| 25 | + const closer = { latitude: 40.9, longitude: -73.9 }; |
| 26 | + expect(detectTrip(far, closer, [HOME])).toEqual(HOME); |
| 27 | + // A few meters closer is jitter, not a trip. |
| 28 | + expect(detectTrip({ latitude: 41, longitude: -74 }, { latitude: 40.9999, longitude: -73.9999 }, [HOME])).toBeUndefined(); |
| 29 | + // The margin grows with the fixes' reported error. |
| 30 | + expect(detectTrip({ latitude: 41, longitude: -74, accuracy: 200 }, { latitude: 40.9999, longitude: -73.9999, accuracy: 200 }, [HOME])).toBeUndefined(); |
| 31 | + // Moving away is not a trip. |
| 32 | + expect(detectTrip(closer, far, [HOME])).toBeUndefined(); |
| 33 | + // Already inside the place is not a new trip. |
| 34 | + expect(detectTrip({ latitude: 40.9, longitude: -73.9 }, { latitude: 1.5001, longitude: 2.5001 }, [HOME])).toBeUndefined(); |
| 35 | +}); |
| 36 | + |
| 37 | +test("detectTrip picks the destination gained on most", () => { |
| 38 | + const prev = { latitude: 0, longitude: 0 }; |
| 39 | + const now = { latitude: 1, longitude: 0 }; // 111 km east |
| 40 | + const home = { name: "home", lat: 2, lon: 0, radiusM: 100 }; // 111 km closer |
| 41 | + const office = { name: "office", lat: 5, lon: 5, radiusM: 100 }; // ~74 km closer |
| 42 | + expect(detectTrip(prev, now, [home, office])).toEqual(home); |
| 43 | +}); |
| 44 | + |
| 45 | +test("the commute state persists and a corrupt file reads empty", () => { |
| 46 | + expect(loadCommute(dir)).toEqual({}); |
| 47 | + const state = { fix: { latitude: 1, longitude: 2, at: 3, accuracy: 5 }, trip: { place: "home", sentAt: 0 }, lastBriefAt: 5 }; |
| 48 | + saveCommute(dir, state); |
| 49 | + expect(loadCommute(dir)).toEqual(state); |
| 50 | + writeFileSync(join(dir, "commute.json"), "not json"); |
| 51 | + expect(loadCommute(dir)).toEqual({}); |
| 52 | +}); |
| 53 | + |
| 54 | +function homeWith(contents: Record<string, string>): string { |
| 55 | + const home = mkdtempSync(join(dir, "home-")); |
| 56 | + mkdirSync(join(home, "state")); |
| 57 | + for (const [name, body] of Object.entries(contents)) writeFileSync(join(home, "state", name), body); |
| 58 | + return home; |
| 59 | +} |
| 60 | + |
| 61 | +test("commuteBrief lists open decisions and finished work since the last card", () => { |
| 62 | + const home = homeWith({ |
| 63 | + "alpha.status": "done [at=101]: PR https://github.com/i098/Crewship/pull/1507\n", |
| 64 | + "beta.status": "needs-decision [at=200]: which option for the fallback file=/home/secret/path.txt\n", |
| 65 | + "gamma.status": "done [at=50]: older work, before the last card\n", |
| 66 | + "delta.status": "needs-decision [at=150]: first ask\nresolved [at=151]: answered\n", |
| 67 | + }); |
| 68 | + const b = commuteBrief(home, 100); |
| 69 | + expect(b.finished).toEqual(["alpha: PR https://github.com/i098/Crewship/pull/1507"]); |
| 70 | + // gamma is before `since`; delta resolved, so it is not open. |
| 71 | + expect(b.decisions).toEqual(["beta: needs-decision: which option for the fallback file="]); |
| 72 | + expect(b.decisions[0]).not.toContain("/home/secret"); |
| 73 | +}); |
| 74 | + |
| 75 | +test("a keyed decision stays open until its own resolved line", () => { |
| 76 | + const home = homeWith({ |
| 77 | + "one.status": "needs-decision [at=1] [key=alpha]: pick a\nneeds-decision [at=2] [key=beta]: pick b\nresolved [key=alpha] [at=3]: answered\n", |
| 78 | + }); |
| 79 | + const b = commuteBrief(home, 0); |
| 80 | + expect(b.decisions).toEqual(["one: needs-decision: pick b"]); |
| 81 | +}); |
| 82 | + |
| 83 | +test("finished work sorts newest first and caps the card", () => { |
| 84 | + const home = homeWith({ |
| 85 | + "a.status": "done [at=1]: first\n", |
| 86 | + "b.status": "done [at=2]: second\n", |
| 87 | + "c.status": "done [at=3]: third\n", |
| 88 | + }); |
| 89 | + const b = commuteBrief(home, 0); |
| 90 | + expect(b.finished).toEqual([ |
| 91 | + "c: third", |
| 92 | + "b: second", |
| 93 | + "a: first", |
| 94 | + ]); |
| 95 | +}); |
| 96 | + |
| 97 | +test("commuteBrief tolerates a missing home and a corrupt status file", () => { |
| 98 | + expect(commuteBrief(join(dir, "nope"), 0)).toEqual({ decisions: [], finished: [] }); |
| 99 | + const home = homeWith({ "bad.status": "\u0000not utf8" }); |
| 100 | + expect(commuteBrief(home, 0)).toEqual({ decisions: [], finished: [] }); |
| 101 | +}); |
| 102 | + |
| 103 | +test("renderBrief shapes one card with both sections", () => { |
| 104 | + const out = renderBrief({ decisions: ["beta: needs-decision: which option"], finished: ["alpha: PR https://github.com/i098/Crewship/pull/1507"] }); |
| 105 | + expect(out).toContain("Commute brief"); |
| 106 | + expect(out).toContain("Open decisions:"); |
| 107 | + expect(out).toContain("- beta: needs-decision: which option"); |
| 108 | + expect(out).toContain("Finished work since the last brief:"); |
| 109 | + expect(out).toContain("- alpha: PR https://github.com/i098/Crewship/pull/1507"); |
| 110 | + const empty = renderBrief({ decisions: [], finished: [] }); |
| 111 | + expect(empty).toContain("- none"); |
| 112 | +}); |
0 commit comments