diff --git a/app/src/app-config.ts b/app/src/app-config.ts index ef2fd19..456e5d1 100644 --- a/app/src/app-config.ts +++ b/app/src/app-config.ts @@ -82,9 +82,19 @@ export class TestAppConfig implements AppConfig { this.getYoutubeVideoSummary = getYoutubeVideoSummary; } - static async createForVideoId(videoId: string): Promise { - return new TestAppConfig( - await createTestGetYoutubeVideoSummaryForVideoId(videoId), + static async createForVideoId(videoId: string): Promise> { + return createOk( + new TestAppConfig( + await createTestGetYoutubeVideoSummaryForVideoId(videoId), + ), ); } } + +export const TEST_CONFIG_VIDEO_ID = "test"; + +export function getAppConfig(desiredGptModel?: string) { + return Deno.env.get("TEST") === "true" + ? TestAppConfig.createForVideoId(TEST_CONFIG_VIDEO_ID) + : ProdAppConfig.create(desiredGptModel); +} diff --git a/app/test/get-youtube-video-summary.test.ts b/app/test/get-youtube-video-summary.test.ts index 40d4b36..1b4bdf2 100644 --- a/app/test/get-youtube-video-summary.test.ts +++ b/app/test/get-youtube-video-summary.test.ts @@ -9,7 +9,7 @@ import { createFailure, createOk } from "../src/result.ts"; import { assertEquals } from "https://deno.land/std@0.211.0/assert/mod.ts"; import { createPromise } from "./common.ts"; -const SUCCESSFUL_CHAT_GPT_CAPTIONS_SUMMARY = +export const SUCCESSFUL_CHAT_GPT_CAPTIONS_SUMMARY = `The video is a music video for the song "Never Gonna Give You Up" by Rick Astley. The captions display the lyrics of the song, which talk about love and commitment, assuring that the singer will never give up on the person diff --git a/routes/summary/youtube.tsx b/routes/summary/youtube.tsx index 34f3a11..e230d71 100644 --- a/routes/summary/youtube.tsx +++ b/routes/summary/youtube.tsx @@ -1,5 +1,5 @@ import { FreshContext, Handlers, PageProps } from "$fresh/server.ts"; -import { ProdAppConfig } from "../../app/src/app-config.ts"; +import { getAppConfig } from "../../app/src/app-config.ts"; import { InternalFailure } from "../../app/src/failure.ts"; import { Result } from "../../app/src/result.ts"; import { Ok } from "../../app/src/result.ts"; @@ -12,7 +12,7 @@ export const handler: Handlers = { ctx: FreshContext, ) { const videoId = new URL(req.url).searchParams.get("id") || ""; - const appResult = ProdAppConfig.create(); + const appResult = await getAppConfig(); if (appResult.result === Result.Failure) { return ctx.render(appResult); } diff --git a/run.ts b/run.ts index d45c37e..99c6dcc 100644 --- a/run.ts +++ b/run.ts @@ -1,5 +1,5 @@ // Entrypoint to run "just tell me" from the command line interface. -import { ProdAppConfig } from "./app/src/app-config.ts"; +import { getAppConfig } from "./app/src/app-config.ts"; import { Result } from "./app/src/result.ts"; import { parseArgs } from "https://deno.land/std@0.207.0/cli/parse_args.ts"; @@ -16,7 +16,7 @@ async function run() { string: ["model"], }); - const appConfigResult = ProdAppConfig.create(flags.model); + const appConfigResult = await getAppConfig(flags.model); if (appConfigResult.result === Result.Failure) { console.log(appConfigResult.failure, "\n", appConfigResult.message); } else { diff --git a/test/main-test.test.ts b/test/main-test.test.ts new file mode 100644 index 0000000..da9bb70 --- /dev/null +++ b/test/main-test.test.ts @@ -0,0 +1,29 @@ +import { createHandler, ServeHandlerInfo } from "$fresh/server.ts"; +import manifest from "../fresh.gen.ts"; +import config from "../fresh.config.ts"; +import { assert } from "$std/assert/assert.ts"; + +const CONN_INFO: ServeHandlerInfo = { + remoteAddr: { hostname: "127.0.0.1", port: 53496, transport: "tcp" }, +}; + +Deno.test("HTTP tests", async (t) => { + Deno.env.set("TEST", "true"); + + const handler = await createHandler(manifest, config); + + await t.step("GET /summary/youtube?id=test", async () => { + const response = await handler( + new Request("http://127.0.0.1/summary/youtube?id=test"), + CONN_INFO, + ); + const text = await response.text(); + assert( + text.includes( + "The video is a music video for the song "Never Gonna Give You Up"", + ), + ); + }); + + Deno.env.delete("TEST"); +});