Files
STTracker/packages/core/test/user-layer-fixtures.ts
T

66 lines
1.7 KiB
TypeScript

import type { Db } from "@sttracker/db/db";
import { newId, nowIso } from "@sttracker/db/ids";
import { toJson } from "@sttracker/db/json";
/** Insert a user and return its id. */
export async function makeUser(db: Db, username = "ada", preferences: Record<string, unknown> = {}): Promise<string> {
const id = newId();
const now = nowIso();
await db.k
.insertInto("users")
.values({
id,
username,
display_name: username,
password_hash: "x",
role: "member",
preferences: toJson(preferences),
created_at: now,
updated_at: now,
})
.execute();
return id;
}
/** Insert a work (shared unless `ownerUserId` is set) and return its id. */
export async function makeWork(
db: Db,
opts: {
title?: string;
mediaType?: string;
year?: number | null;
parentId?: string | null;
ownerUserId?: string | null;
externalIds?: Record<string, string>;
} = {},
): Promise<string> {
const id = newId();
const now = nowIso();
await db.k
.insertInto("works")
.values({
id,
media_type: opts.mediaType ?? "book",
title: opts.title ?? "Dune",
original_title: "",
year: opts.year ?? null,
release_date: null,
creators: toJson([]),
description: "",
cover_url: "",
cover_attachment_id: null,
metadata: toJson({}),
parent_id: opts.parentId ?? null,
position: null,
owner_user_id: opts.ownerUserId ?? null,
merged_into_id: null,
created_at: now,
updated_at: now,
})
.execute();
for (const [namespace, value] of Object.entries(opts.externalIds ?? {})) {
await db.k.insertInto("external_ids").values({ id: newId(), work_id: id, namespace, value }).execute();
}
return id;
}