mirror of
https://github.com/kennethnym/aris.git
synced 2026-06-17 21:11:17 +01:00
feat: seed default user sources (#128)
This commit is contained in:
85
apps/freya-backend/src/sources/default-sources.test.ts
Normal file
85
apps/freya-backend/src/sources/default-sources.test.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
import { LocationSource } from "@freya/source-location"
|
||||
import { WebSearchSource } from "@freya/source-web-search"
|
||||
import { describe, expect, test } from "bun:test"
|
||||
|
||||
import type { Database } from "../db/index.ts"
|
||||
|
||||
import { userSources } from "../db/schema.ts"
|
||||
import { DEFAULT_ENABLED_SOURCE_IDS, insertDefaultUserSources } from "./default-sources.ts"
|
||||
|
||||
interface UserSourceInsertRow {
|
||||
userId: string
|
||||
sourceId: string
|
||||
enabled: boolean
|
||||
config: unknown
|
||||
createdAt: Date
|
||||
updatedAt: Date
|
||||
}
|
||||
|
||||
interface RecordingDb {
|
||||
db: Database
|
||||
table: () => unknown
|
||||
rows: () => UserSourceInsertRow[] | undefined
|
||||
conflictTarget: () => readonly unknown[] | undefined
|
||||
}
|
||||
|
||||
function createRecordingDb(): RecordingDb {
|
||||
let insertedTable: unknown
|
||||
let insertedRows: UserSourceInsertRow[] | undefined
|
||||
let target: readonly unknown[] | undefined
|
||||
|
||||
const db = {
|
||||
insert(table: unknown) {
|
||||
insertedTable = table
|
||||
|
||||
return {
|
||||
values(rows: UserSourceInsertRow[]) {
|
||||
insertedRows = rows
|
||||
|
||||
return {
|
||||
async onConflictDoNothing(options: { target: readonly unknown[] }) {
|
||||
target = options.target
|
||||
},
|
||||
}
|
||||
},
|
||||
}
|
||||
},
|
||||
} as unknown as Database
|
||||
|
||||
return {
|
||||
db,
|
||||
table: () => insertedTable,
|
||||
rows: () => insertedRows,
|
||||
conflictTarget: () => target,
|
||||
}
|
||||
}
|
||||
|
||||
describe("default user sources", () => {
|
||||
test("defines location and web search as default enabled sources", () => {
|
||||
expect(DEFAULT_ENABLED_SOURCE_IDS).toEqual([LocationSource.id, WebSearchSource.id])
|
||||
})
|
||||
|
||||
test("inserts default enabled source rows for a user", async () => {
|
||||
const recording = createRecordingDb()
|
||||
|
||||
await insertDefaultUserSources(recording.db, "user-1")
|
||||
|
||||
const rows = recording.rows()
|
||||
if (!rows) {
|
||||
throw new Error("Expected default source rows to be inserted")
|
||||
}
|
||||
|
||||
expect(recording.table()).toBe(userSources)
|
||||
expect(rows).toHaveLength(2)
|
||||
expect(rows.map((row) => row.sourceId)).toEqual([...DEFAULT_ENABLED_SOURCE_IDS])
|
||||
expect(recording.conflictTarget()).toEqual([userSources.userId, userSources.sourceId])
|
||||
|
||||
for (const row of rows) {
|
||||
expect(row.userId).toBe("user-1")
|
||||
expect(row.enabled).toBe(true)
|
||||
expect(row.config).toEqual({})
|
||||
expect(row.createdAt).toBeInstanceOf(Date)
|
||||
expect(row.updatedAt).toBe(row.createdAt)
|
||||
}
|
||||
})
|
||||
})
|
||||
30
apps/freya-backend/src/sources/default-sources.ts
Normal file
30
apps/freya-backend/src/sources/default-sources.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
import { LocationSource } from "@freya/source-location"
|
||||
import { WebSearchSource } from "@freya/source-web-search"
|
||||
|
||||
import type { Database } from "../db/index.ts"
|
||||
|
||||
import { userSources } from "../db/schema.ts"
|
||||
|
||||
export const DEFAULT_ENABLED_SOURCE_IDS = [LocationSource.id, WebSearchSource.id] as const
|
||||
|
||||
export type DefaultEnabledSourceId = (typeof DEFAULT_ENABLED_SOURCE_IDS)[number]
|
||||
|
||||
export async function insertDefaultUserSources(db: Database, userId: string): Promise<void> {
|
||||
const now = new Date()
|
||||
|
||||
await db
|
||||
.insert(userSources)
|
||||
.values(
|
||||
DEFAULT_ENABLED_SOURCE_IDS.map((sourceId) => ({
|
||||
userId,
|
||||
sourceId,
|
||||
enabled: true,
|
||||
config: {},
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})),
|
||||
)
|
||||
.onConflictDoNothing({
|
||||
target: [userSources.userId, userSources.sourceId],
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user