|
| 1 | +/* eslint-disable turbo/no-undeclared-env-vars */ |
| 2 | +import { expect, test } from "@playwright/test"; |
| 3 | +import { exec } from "child_process"; |
| 4 | +import { execSync } from "node:child_process"; |
| 5 | +import crypto from "node:crypto"; |
| 6 | +import { mkdir } from "node:fs/promises"; |
| 7 | +import { fileURLToPath } from "node:url"; |
| 8 | +import waitPort from "wait-port"; |
| 9 | +import type { |
| 10 | + TemplateEngine, |
| 11 | + TemplateFramework, |
| 12 | + TemplateType, |
| 13 | + TemplateUI, |
| 14 | +} from "../templates"; |
| 15 | + |
| 16 | +let cwd: string; |
| 17 | +test.beforeEach(async () => { |
| 18 | + cwd = fileURLToPath( |
| 19 | + new URL(`.cache/${crypto.randomUUID()}`, import.meta.url), |
| 20 | + ); |
| 21 | + await mkdir(cwd, { recursive: true }); |
| 22 | +}); |
| 23 | + |
| 24 | +const templateTypes: TemplateType[] = ["streaming", "simple"]; |
| 25 | +const templateFrameworks: TemplateFramework[] = ["nextjs", "express"]; |
| 26 | +const templateEngines: TemplateEngine[] = ["simple", "context"]; |
| 27 | +const templateUIs: TemplateUI[] = ["shadcn", "html"]; |
| 28 | + |
| 29 | +for (const templateType of templateTypes) { |
| 30 | + for (const templateFramework of templateFrameworks) { |
| 31 | + for (const templateEngine of templateEngines) { |
| 32 | + for (const templateUI of templateUIs) { |
| 33 | + const shouldGenerateFrontendEnum = |
| 34 | + templateFramework === "express" || templateFramework === "fastapi" |
| 35 | + ? ["--frontend", "--no-frontend"] |
| 36 | + : [""]; |
| 37 | + for (const shouldGenerateFrontend of shouldGenerateFrontendEnum) { |
| 38 | + if (templateEngine === "context") { |
| 39 | + // we don't test context templates because it needs OPEN_AI_KEY |
| 40 | + continue; |
| 41 | + } |
| 42 | + test(`try create-llama ${templateType} ${templateFramework} ${templateEngine} ${templateUI} ${shouldGenerateFrontend}`, async ({ |
| 43 | + page, |
| 44 | + }) => { |
| 45 | + const createLlama = fileURLToPath( |
| 46 | + new URL("../dist/index.js", import.meta.url), |
| 47 | + ); |
| 48 | + |
| 49 | + const name = [ |
| 50 | + templateType, |
| 51 | + templateFramework, |
| 52 | + templateEngine, |
| 53 | + templateUI, |
| 54 | + shouldGenerateFrontend, |
| 55 | + ].join("-"); |
| 56 | + const command = [ |
| 57 | + "node", |
| 58 | + createLlama, |
| 59 | + name, |
| 60 | + "--template", |
| 61 | + templateType, |
| 62 | + "--framework", |
| 63 | + templateFramework, |
| 64 | + "--engine", |
| 65 | + templateEngine, |
| 66 | + "--ui", |
| 67 | + templateUI, |
| 68 | + "--open-ai-key", |
| 69 | + process.env.OPEN_AI_KEY || "", |
| 70 | + shouldGenerateFrontend, |
| 71 | + "--eslint", |
| 72 | + ].join(" "); |
| 73 | + console.log(`running command '${command}' in ${cwd}`); |
| 74 | + execSync(command, { |
| 75 | + stdio: "inherit", |
| 76 | + cwd, |
| 77 | + }); |
| 78 | + |
| 79 | + const port = Math.floor(Math.random() * 10000) + 10000; |
| 80 | + |
| 81 | + if ( |
| 82 | + shouldGenerateFrontend === "--frontend" && |
| 83 | + templateFramework === "express" |
| 84 | + ) { |
| 85 | + execSync("npm install", { |
| 86 | + stdio: "inherit", |
| 87 | + cwd: `${cwd}/${name}/frontend`, |
| 88 | + }); |
| 89 | + execSync("npm install", { |
| 90 | + stdio: "inherit", |
| 91 | + cwd: `${cwd}/${name}/backend`, |
| 92 | + }); |
| 93 | + } else { |
| 94 | + execSync("npm install", { |
| 95 | + stdio: "inherit", |
| 96 | + cwd: `${cwd}/${name}`, |
| 97 | + }); |
| 98 | + } |
| 99 | + |
| 100 | + if (shouldGenerateFrontend === "--no-frontend") { |
| 101 | + return; |
| 102 | + } |
| 103 | + |
| 104 | + const cp = exec("npm run dev", { |
| 105 | + cwd: |
| 106 | + shouldGenerateFrontend === "--frontend" |
| 107 | + ? `${cwd}/${name}/frontend` |
| 108 | + : `${cwd}/${name}`, |
| 109 | + env: { |
| 110 | + ...process.env, |
| 111 | + PORT: `${port}`, |
| 112 | + }, |
| 113 | + }); |
| 114 | + |
| 115 | + await waitPort({ |
| 116 | + host: "localhost", |
| 117 | + port, |
| 118 | + timeout: 1000 * 60, |
| 119 | + }); |
| 120 | + |
| 121 | + await page.goto(`http://localhost:${port}`); |
| 122 | + await expect(page.getByText("Built by LlamaIndex")).toBeVisible(); |
| 123 | + cp.kill(); |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments