Skip to content

Commit

Permalink
Unify test and prod app config factory functions, add a getter for ap…
Browse files Browse the repository at this point in the history
…p config based on env variable and add fresh http server test
  • Loading branch information
franekmagiera committed Jan 15, 2024
1 parent 2903402 commit c945d14
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
16 changes: 13 additions & 3 deletions app/src/app-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,19 @@ export class TestAppConfig implements AppConfig {
this.getYoutubeVideoSummary = getYoutubeVideoSummary;
}

static async createForVideoId(videoId: string): Promise<AppConfig> {
return new TestAppConfig(
await createTestGetYoutubeVideoSummaryForVideoId(videoId),
static async createForVideoId(videoId: string): Promise<Ok<AppConfig>> {
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);
}
2 changes: 1 addition & 1 deletion app/test/get-youtube-video-summary.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { createFailure, createOk } from "../src/result.ts";
import { assertEquals } from "https://deno.land/[email protected]/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
Expand Down
4 changes: 2 additions & 2 deletions routes/summary/youtube.tsx
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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);
}
Expand Down
4 changes: 2 additions & 2 deletions run.ts
Original file line number Diff line number Diff line change
@@ -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/[email protected]/cli/parse_args.ts";

Expand All @@ -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 {
Expand Down
29 changes: 29 additions & 0 deletions test/main-test.test.ts
Original file line number Diff line number Diff line change
@@ -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 &quot;Never Gonna Give You Up&quot;",
),
);
});

Deno.env.delete("TEST");
});

0 comments on commit c945d14

Please sign in to comment.