mirror of
https://github.com/kennethnym/aris.git
synced 2026-03-20 09:01:19 +00:00
Compare commits
4 Commits
feat/locat
...
feat/tfl-s
| Author | SHA1 | Date | |
|---|---|---|---|
|
54e4b0dcf7
|
|||
| d1102fe1ac | |||
| db0c57f04b | |||
| 9e3fe2ea16 |
@@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aris/core": "workspace:*",
|
"@aris/core": "workspace:*",
|
||||||
"@aris/source-location": "workspace:*",
|
"@aris/source-location": "workspace:*",
|
||||||
|
"@aris/source-tfl": "workspace:*",
|
||||||
"@aris/source-weatherkit": "workspace:*",
|
"@aris/source-weatherkit": "workspace:*",
|
||||||
"@hono/trpc-server": "^0.3",
|
"@hono/trpc-server": "^0.3",
|
||||||
"@trpc/server": "^11",
|
"@trpc/server": "^11",
|
||||||
|
|||||||
162
apps/aris-backend/src/feed/service.test.ts
Normal file
162
apps/aris-backend/src/feed/service.test.ts
Normal file
@@ -0,0 +1,162 @@
|
|||||||
|
import { describe, expect, mock, test } from "bun:test"
|
||||||
|
|
||||||
|
import { LocationService } from "../location/service.ts"
|
||||||
|
import { FeedEngineService } from "./service.ts"
|
||||||
|
|
||||||
|
describe("FeedEngineService", () => {
|
||||||
|
test("engineForUser creates engine on first call", () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const engine = service.engineForUser("user-1")
|
||||||
|
|
||||||
|
expect(engine).toBeDefined()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("engineForUser returns same engine for same user", () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const engine1 = service.engineForUser("user-1")
|
||||||
|
const engine2 = service.engineForUser("user-1")
|
||||||
|
|
||||||
|
expect(engine1).toBe(engine2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("engineForUser returns different engines for different users", () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const engine1 = service.engineForUser("user-1")
|
||||||
|
const engine2 = service.engineForUser("user-2")
|
||||||
|
|
||||||
|
expect(engine1).not.toBe(engine2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("engineForUser registers sources from all providers", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const engine = service.engineForUser("user-1")
|
||||||
|
const result = await engine.refresh()
|
||||||
|
|
||||||
|
expect(result.errors).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("engineForUser works with empty providers array", async () => {
|
||||||
|
const service = new FeedEngineService([])
|
||||||
|
|
||||||
|
const engine = service.engineForUser("user-1")
|
||||||
|
const result = await engine.refresh()
|
||||||
|
|
||||||
|
expect(result.errors).toHaveLength(0)
|
||||||
|
expect(result.items).toHaveLength(0)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("refresh returns feed result", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const result = await service.refresh("user-1")
|
||||||
|
|
||||||
|
expect(result).toHaveProperty("context")
|
||||||
|
expect(result).toHaveProperty("items")
|
||||||
|
expect(result).toHaveProperty("errors")
|
||||||
|
expect(result.context.time).toBeInstanceOf(Date)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("refresh uses location from LocationService", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
const location = {
|
||||||
|
lat: 51.5074,
|
||||||
|
lng: -0.1278,
|
||||||
|
accuracy: 10,
|
||||||
|
timestamp: new Date(),
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create engine first, then update location
|
||||||
|
service.engineForUser("user-1")
|
||||||
|
locationService.updateUserLocation("user-1", location)
|
||||||
|
|
||||||
|
const result = await service.refresh("user-1")
|
||||||
|
|
||||||
|
expect(result.context.location).toEqual(location)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("subscribe receives updates", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
const callback = mock()
|
||||||
|
|
||||||
|
service.subscribe("user-1", callback)
|
||||||
|
|
||||||
|
// Push location to trigger update
|
||||||
|
locationService.updateUserLocation("user-1", {
|
||||||
|
lat: 51.5074,
|
||||||
|
lng: -0.1278,
|
||||||
|
accuracy: 10,
|
||||||
|
timestamp: new Date(),
|
||||||
|
})
|
||||||
|
|
||||||
|
// Wait for async update propagation
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
|
|
||||||
|
expect(callback).toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("subscribe returns unsubscribe function", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
const callback = mock()
|
||||||
|
|
||||||
|
const unsubscribe = service.subscribe("user-1", callback)
|
||||||
|
|
||||||
|
unsubscribe()
|
||||||
|
|
||||||
|
locationService.updateUserLocation("user-1", {
|
||||||
|
lat: 51.5074,
|
||||||
|
lng: -0.1278,
|
||||||
|
accuracy: 10,
|
||||||
|
timestamp: new Date(),
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
|
|
||||||
|
expect(callback).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removeUser stops engine and removes it", async () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
const callback = mock()
|
||||||
|
|
||||||
|
service.subscribe("user-1", callback)
|
||||||
|
|
||||||
|
service.removeUser("user-1")
|
||||||
|
|
||||||
|
// Push location - should not trigger update since engine is stopped
|
||||||
|
locationService.feedSourceForUser("user-1")
|
||||||
|
locationService.updateUserLocation("user-1", {
|
||||||
|
lat: 51.5074,
|
||||||
|
lng: -0.1278,
|
||||||
|
accuracy: 10,
|
||||||
|
timestamp: new Date(),
|
||||||
|
})
|
||||||
|
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, 10))
|
||||||
|
|
||||||
|
expect(callback).not.toHaveBeenCalled()
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removeUser allows new engine to be created", () => {
|
||||||
|
const locationService = new LocationService()
|
||||||
|
const service = new FeedEngineService([locationService])
|
||||||
|
|
||||||
|
const engine1 = service.engineForUser("user-1")
|
||||||
|
service.removeUser("user-1")
|
||||||
|
const engine2 = service.engineForUser("user-1")
|
||||||
|
|
||||||
|
expect(engine1).not.toBe(engine2)
|
||||||
|
})
|
||||||
|
})
|
||||||
75
apps/aris-backend/src/feed/service.ts
Normal file
75
apps/aris-backend/src/feed/service.ts
Normal file
@@ -0,0 +1,75 @@
|
|||||||
|
import { FeedEngine, type FeedResult, type FeedSource, type FeedSubscriber } from "@aris/core"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Provides a FeedSource instance for a user.
|
||||||
|
*/
|
||||||
|
export interface FeedSourceProvider {
|
||||||
|
feedSourceForUser(userId: string): FeedSource
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages FeedEngine instances per user.
|
||||||
|
*
|
||||||
|
* Receives FeedSource instances from injected providers and wires them
|
||||||
|
* into per-user engines. Engines are auto-started on creation.
|
||||||
|
*/
|
||||||
|
export class FeedEngineService {
|
||||||
|
private engines = new Map<string, FeedEngine>()
|
||||||
|
|
||||||
|
constructor(private readonly providers: FeedSourceProvider[]) {}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get or create a FeedEngine for a user.
|
||||||
|
* Automatically registers sources and starts the engine.
|
||||||
|
*/
|
||||||
|
engineForUser(userId: string): FeedEngine {
|
||||||
|
let engine = this.engines.get(userId)
|
||||||
|
if (!engine) {
|
||||||
|
engine = this.createEngine(userId)
|
||||||
|
this.engines.set(userId, engine)
|
||||||
|
}
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Refresh a user's feed.
|
||||||
|
*/
|
||||||
|
async refresh(userId: string): Promise<FeedResult> {
|
||||||
|
const engine = this.engineForUser(userId)
|
||||||
|
return engine.refresh()
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Subscribe to feed updates for a user.
|
||||||
|
* Returns unsubscribe function.
|
||||||
|
*/
|
||||||
|
subscribe(userId: string, callback: FeedSubscriber): () => void {
|
||||||
|
const engine = this.engineForUser(userId)
|
||||||
|
return engine.subscribe(callback)
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Remove a user's FeedEngine.
|
||||||
|
* Stops the engine and cleans up resources.
|
||||||
|
*/
|
||||||
|
removeUser(userId: string): void {
|
||||||
|
const engine = this.engines.get(userId)
|
||||||
|
if (engine) {
|
||||||
|
engine.stop()
|
||||||
|
this.engines.delete(userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private createEngine(userId: string): FeedEngine {
|
||||||
|
const engine = new FeedEngine()
|
||||||
|
|
||||||
|
for (const provider of this.providers) {
|
||||||
|
const source = provider.feedSourceForUser(userId)
|
||||||
|
engine.register(source)
|
||||||
|
}
|
||||||
|
|
||||||
|
engine.start()
|
||||||
|
|
||||||
|
return engine
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,11 +1,13 @@
|
|||||||
import { LocationSource, type Location } from "@aris/source-location"
|
import { LocationSource, type Location } from "@aris/source-location"
|
||||||
|
|
||||||
|
import type { FeedSourceProvider } from "../feed/service.ts"
|
||||||
|
|
||||||
import { UserNotFoundError } from "../lib/error.ts"
|
import { UserNotFoundError } from "../lib/error.ts"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Manages LocationSource instances per user.
|
* Manages LocationSource instances per user.
|
||||||
*/
|
*/
|
||||||
export class LocationService {
|
export class LocationService implements FeedSourceProvider {
|
||||||
private sources = new Map<string, LocationSource>()
|
private sources = new Map<string, LocationSource>()
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
206
apps/aris-backend/src/tfl/service.test.ts
Normal file
206
apps/aris-backend/src/tfl/service.test.ts
Normal file
@@ -0,0 +1,206 @@
|
|||||||
|
import type { Context } from "@aris/core"
|
||||||
|
import type { ITflApi, StationLocation, TflLineId, TflLineStatus } from "@aris/source-tfl"
|
||||||
|
|
||||||
|
import { describe, expect, test } from "bun:test"
|
||||||
|
|
||||||
|
import { UserNotFoundError } from "../lib/error.ts"
|
||||||
|
import { TflService } from "./service.ts"
|
||||||
|
|
||||||
|
class StubTflApi implements ITflApi {
|
||||||
|
private statuses: TflLineStatus[]
|
||||||
|
|
||||||
|
constructor(statuses: TflLineStatus[] = []) {
|
||||||
|
this.statuses = statuses
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchLineStatuses(lines?: TflLineId[]): Promise<TflLineStatus[]> {
|
||||||
|
if (lines) {
|
||||||
|
return this.statuses.filter((s) => lines.includes(s.lineId))
|
||||||
|
}
|
||||||
|
return this.statuses
|
||||||
|
}
|
||||||
|
|
||||||
|
async fetchStations(): Promise<StationLocation[]> {
|
||||||
|
return [
|
||||||
|
{
|
||||||
|
id: "940GZZLUKSX",
|
||||||
|
name: "King's Cross",
|
||||||
|
lat: 51.5308,
|
||||||
|
lng: -0.1238,
|
||||||
|
lines: ["northern", "victoria"],
|
||||||
|
},
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function createContext(): Context {
|
||||||
|
return { time: new Date("2026-01-15T12:00:00Z") }
|
||||||
|
}
|
||||||
|
|
||||||
|
const sampleStatuses: TflLineStatus[] = [
|
||||||
|
{
|
||||||
|
lineId: "northern",
|
||||||
|
lineName: "Northern",
|
||||||
|
severity: "minor-delays",
|
||||||
|
description: "Minor delays on the Northern line",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
lineId: "victoria",
|
||||||
|
lineName: "Victoria",
|
||||||
|
severity: "major-delays",
|
||||||
|
description: "Severe delays on the Victoria line",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
lineId: "central",
|
||||||
|
lineName: "Central",
|
||||||
|
severity: "closure",
|
||||||
|
description: "Central line suspended",
|
||||||
|
},
|
||||||
|
]
|
||||||
|
|
||||||
|
describe("TflService", () => {
|
||||||
|
test("feedSourceForUser creates source on first call", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
const source = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
expect(source).toBeDefined()
|
||||||
|
expect(source.id).toBe("tfl")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("feedSourceForUser returns same source for same user", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
const source1 = service.feedSourceForUser("user-1")
|
||||||
|
const source2 = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
expect(source1).toBe(source2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("feedSourceForUser returns different sources for different users", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
const source1 = service.feedSourceForUser("user-1")
|
||||||
|
const source2 = service.feedSourceForUser("user-2")
|
||||||
|
|
||||||
|
expect(source1).not.toBe(source2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("updateLinesOfInterest mutates the existing source in place", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
const original = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
service.updateLinesOfInterest("user-1", ["northern", "victoria"])
|
||||||
|
const after = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
expect(after).toBe(original)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("updateLinesOfInterest throws if source does not exist", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
|
||||||
|
expect(() => service.updateLinesOfInterest("user-1", ["northern"])).toThrow(UserNotFoundError)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removeUser removes the source", () => {
|
||||||
|
const service = new TflService(new StubTflApi())
|
||||||
|
const source1 = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
service.removeUser("user-1")
|
||||||
|
const source2 = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
expect(source1).not.toBe(source2)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("removeUser clears line configuration", async () => {
|
||||||
|
const api = new StubTflApi(sampleStatuses)
|
||||||
|
const service = new TflService(api)
|
||||||
|
service.feedSourceForUser("user-1")
|
||||||
|
service.updateLinesOfInterest("user-1", ["northern"])
|
||||||
|
|
||||||
|
service.removeUser("user-1")
|
||||||
|
const items = await service.feedSourceForUser("user-1").fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(items.length).toBe(3)
|
||||||
|
})
|
||||||
|
|
||||||
|
test("shares single api instance across users", () => {
|
||||||
|
const api = new StubTflApi()
|
||||||
|
const service = new TflService(api)
|
||||||
|
|
||||||
|
service.feedSourceForUser("user-1")
|
||||||
|
service.feedSourceForUser("user-2")
|
||||||
|
|
||||||
|
expect(service.feedSourceForUser("user-1").id).toBe("tfl")
|
||||||
|
expect(service.feedSourceForUser("user-2").id).toBe("tfl")
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("returned source fetches items", () => {
|
||||||
|
test("source returns feed items from api", async () => {
|
||||||
|
const api = new StubTflApi(sampleStatuses)
|
||||||
|
const service = new TflService(api)
|
||||||
|
const source = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
const items = await source.fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(items.length).toBe(3)
|
||||||
|
for (const item of items) {
|
||||||
|
expect(item.type).toBe("tfl-alert")
|
||||||
|
expect(item.id).toMatch(/^tfl-alert-/)
|
||||||
|
expect(typeof item.priority).toBe("number")
|
||||||
|
expect(item.timestamp).toBeInstanceOf(Date)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("source returns items sorted by priority descending", async () => {
|
||||||
|
const api = new StubTflApi(sampleStatuses)
|
||||||
|
const service = new TflService(api)
|
||||||
|
const source = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
const items = await source.fetchItems(createContext())
|
||||||
|
|
||||||
|
for (let i = 1; i < items.length; i++) {
|
||||||
|
expect(items[i - 1]!.priority).toBeGreaterThanOrEqual(items[i]!.priority)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
test("source returns empty array when no disruptions", async () => {
|
||||||
|
const api = new StubTflApi([])
|
||||||
|
const service = new TflService(api)
|
||||||
|
const source = service.feedSourceForUser("user-1")
|
||||||
|
|
||||||
|
const items = await source.fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(items).toEqual([])
|
||||||
|
})
|
||||||
|
|
||||||
|
test("updateLinesOfInterest filters items to configured lines", async () => {
|
||||||
|
const api = new StubTflApi(sampleStatuses)
|
||||||
|
const service = new TflService(api)
|
||||||
|
|
||||||
|
const before = await service.feedSourceForUser("user-1").fetchItems(createContext())
|
||||||
|
expect(before.length).toBe(3)
|
||||||
|
|
||||||
|
service.updateLinesOfInterest("user-1", ["northern"])
|
||||||
|
const after = await service.feedSourceForUser("user-1").fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(after.length).toBe(1)
|
||||||
|
expect(after[0]!.data.line).toBe("northern")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("different users get independent line configs", async () => {
|
||||||
|
const api = new StubTflApi(sampleStatuses)
|
||||||
|
const service = new TflService(api)
|
||||||
|
service.feedSourceForUser("user-1")
|
||||||
|
service.feedSourceForUser("user-2")
|
||||||
|
|
||||||
|
service.updateLinesOfInterest("user-1", ["northern"])
|
||||||
|
service.updateLinesOfInterest("user-2", ["central"])
|
||||||
|
|
||||||
|
const items1 = await service.feedSourceForUser("user-1").fetchItems(createContext())
|
||||||
|
const items2 = await service.feedSourceForUser("user-2").fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(items1.length).toBe(1)
|
||||||
|
expect(items1[0]!.data.line).toBe("northern")
|
||||||
|
expect(items2.length).toBe(1)
|
||||||
|
expect(items2[0]!.data.line).toBe("central")
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
40
apps/aris-backend/src/tfl/service.ts
Normal file
40
apps/aris-backend/src/tfl/service.ts
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
import { TflSource, type ITflApi, type TflLineId } from "@aris/source-tfl"
|
||||||
|
|
||||||
|
import type { FeedSourceProvider } from "../feed/service.ts"
|
||||||
|
|
||||||
|
import { UserNotFoundError } from "../lib/error.ts"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Manages per-user TflSource instances with individual line configuration.
|
||||||
|
*/
|
||||||
|
export class TflService implements FeedSourceProvider {
|
||||||
|
private sources = new Map<string, TflSource>()
|
||||||
|
|
||||||
|
constructor(private readonly api: ITflApi) {}
|
||||||
|
|
||||||
|
feedSourceForUser(userId: string): TflSource {
|
||||||
|
let source = this.sources.get(userId)
|
||||||
|
if (!source) {
|
||||||
|
source = new TflSource({ client: this.api })
|
||||||
|
this.sources.set(userId, source)
|
||||||
|
}
|
||||||
|
return source
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update monitored lines for a user. Mutates the existing TflSource
|
||||||
|
* so that references held by FeedEngine remain valid.
|
||||||
|
* @throws {UserNotFoundError} If no source exists for the user
|
||||||
|
*/
|
||||||
|
updateLinesOfInterest(userId: string, lines: TflLineId[]): void {
|
||||||
|
const source = this.sources.get(userId)
|
||||||
|
if (!source) {
|
||||||
|
throw new UserNotFoundError(userId)
|
||||||
|
}
|
||||||
|
source.setLinesOfInterest(lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
removeUser(userId: string): void {
|
||||||
|
this.sources.delete(userId)
|
||||||
|
}
|
||||||
|
}
|
||||||
1
bun.lock
1
bun.lock
@@ -19,6 +19,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@aris/core": "workspace:*",
|
"@aris/core": "workspace:*",
|
||||||
"@aris/source-location": "workspace:*",
|
"@aris/source-location": "workspace:*",
|
||||||
|
"@aris/source-tfl": "workspace:*",
|
||||||
"@aris/source-weatherkit": "workspace:*",
|
"@aris/source-weatherkit": "workspace:*",
|
||||||
"@hono/trpc-server": "^0.3",
|
"@hono/trpc-server": "^0.3",
|
||||||
"@trpc/server": "^11",
|
"@trpc/server": "^11",
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ export { TflSource } from "./tfl-source.ts"
|
|||||||
export { TflApi } from "./tfl-api.ts"
|
export { TflApi } from "./tfl-api.ts"
|
||||||
export type { TflLineId } from "./tfl-api.ts"
|
export type { TflLineId } from "./tfl-api.ts"
|
||||||
export type {
|
export type {
|
||||||
|
ITflApi,
|
||||||
StationLocation,
|
StationLocation,
|
||||||
TflAlertData,
|
TflAlertData,
|
||||||
TflAlertFeedItem,
|
TflAlertFeedItem,
|
||||||
|
|||||||
@@ -112,6 +112,49 @@ describe("TflSource", () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
describe("setLinesOfInterest", () => {
|
||||||
|
const lineFilteringApi: ITflApi = {
|
||||||
|
async fetchLineStatuses(lines?: TflLineId[]): Promise<TflLineStatus[]> {
|
||||||
|
const all: TflLineStatus[] = [
|
||||||
|
{
|
||||||
|
lineId: "northern",
|
||||||
|
lineName: "Northern",
|
||||||
|
severity: "minor-delays",
|
||||||
|
description: "Delays",
|
||||||
|
},
|
||||||
|
{ lineId: "central", lineName: "Central", severity: "closure", description: "Closed" },
|
||||||
|
]
|
||||||
|
return lines ? all.filter((s) => lines.includes(s.lineId)) : all
|
||||||
|
},
|
||||||
|
async fetchStations(): Promise<StationLocation[]> {
|
||||||
|
return []
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
test("changes which lines are fetched", async () => {
|
||||||
|
const source = new TflSource({ client: lineFilteringApi })
|
||||||
|
const before = await source.fetchItems(createContext())
|
||||||
|
expect(before.length).toBe(2)
|
||||||
|
|
||||||
|
source.setLinesOfInterest(["northern"])
|
||||||
|
const after = await source.fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(after.length).toBe(1)
|
||||||
|
expect(after[0]!.data.line).toBe("northern")
|
||||||
|
})
|
||||||
|
|
||||||
|
test("DEFAULT_LINES_OF_INTEREST restores all lines", async () => {
|
||||||
|
const source = new TflSource({ client: lineFilteringApi, lines: ["northern"] })
|
||||||
|
const filtered = await source.fetchItems(createContext())
|
||||||
|
expect(filtered.length).toBe(1)
|
||||||
|
|
||||||
|
source.setLinesOfInterest([...TflSource.DEFAULT_LINES_OF_INTEREST])
|
||||||
|
const all = await source.fetchItems(createContext())
|
||||||
|
|
||||||
|
expect(all.length).toBe(2)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
describe("fetchItems", () => {
|
describe("fetchItems", () => {
|
||||||
test("returns feed items array", async () => {
|
test("returns feed items array", async () => {
|
||||||
const source = new TflSource({ client: api })
|
const source = new TflSource({ client: api })
|
||||||
|
|||||||
@@ -42,18 +42,46 @@ const SEVERITY_PRIORITY: Record<TflAlertSeverity, number> = {
|
|||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
export class TflSource implements FeedSource<TflAlertFeedItem> {
|
export class TflSource implements FeedSource<TflAlertFeedItem> {
|
||||||
|
static readonly DEFAULT_LINES_OF_INTEREST: readonly TflLineId[] = [
|
||||||
|
"bakerloo",
|
||||||
|
"central",
|
||||||
|
"circle",
|
||||||
|
"district",
|
||||||
|
"hammersmith-city",
|
||||||
|
"jubilee",
|
||||||
|
"metropolitan",
|
||||||
|
"northern",
|
||||||
|
"piccadilly",
|
||||||
|
"victoria",
|
||||||
|
"waterloo-city",
|
||||||
|
"lioness",
|
||||||
|
"mildmay",
|
||||||
|
"windrush",
|
||||||
|
"weaver",
|
||||||
|
"suffragette",
|
||||||
|
"liberty",
|
||||||
|
"elizabeth",
|
||||||
|
]
|
||||||
|
|
||||||
readonly id = "tfl"
|
readonly id = "tfl"
|
||||||
readonly dependencies = ["location"]
|
readonly dependencies = ["location"]
|
||||||
|
|
||||||
private readonly client: ITflApi
|
private readonly client: ITflApi
|
||||||
private readonly lines?: TflLineId[]
|
private lines: TflLineId[]
|
||||||
|
|
||||||
constructor(options: TflSourceOptions) {
|
constructor(options: TflSourceOptions) {
|
||||||
if (!options.client && !options.apiKey) {
|
if (!options.client && !options.apiKey) {
|
||||||
throw new Error("Either client or apiKey must be provided")
|
throw new Error("Either client or apiKey must be provided")
|
||||||
}
|
}
|
||||||
this.client = options.client ?? new TflApi(options.apiKey!)
|
this.client = options.client ?? new TflApi(options.apiKey!)
|
||||||
this.lines = options.lines
|
this.lines = options.lines ?? [...TflSource.DEFAULT_LINES_OF_INTEREST]
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Update the set of monitored lines. Takes effect on the next fetchItems call.
|
||||||
|
*/
|
||||||
|
setLinesOfInterest(lines: TflLineId[]): void {
|
||||||
|
this.lines = lines
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetchItems(context: Context): Promise<TflAlertFeedItem[]> {
|
async fetchItems(context: Context): Promise<TflAlertFeedItem[]> {
|
||||||
|
|||||||
Reference in New Issue
Block a user