Public: anyone can read this; the crew can change it.
Describe an app in one sentence in the terminal and it is made, named from the sentence, with the crew on it, as New app does on the web #8266
mason/forge:mason/8047-describe-an-app-in-one-sentence-in-the-t → main 2 commits fork
10 files
changed +297
−81
src/actions/apps.ts +29
| @@ -65,6 +65,7 @@ import { | ||
| 65 | 65 | requireWriter, |
| 66 | 66 | type AccessError, |
| 67 | 67 | } from "./access.ts"; |
| 68 | import { deriveAppName } from "../web/pages/apps-name.ts"; | |
| 68 | 69 | import { assignIssue, openIssue } from "./issues.ts"; |
| 69 | 70 | import { |
| 70 | 71 | commitToBranch, |
| @@ -436,6 +437,34 @@ export async function newApp( | ||
| 436 | 437 | }); |
| 437 | 438 | } |
| 438 | 439 | |
| 440 | /** How many suffixed names (`x-2`, `x-3`, …) a derived name will try before giving up. */ | |
| 441 | const MAX_NAME_ATTEMPTS = 30; | |
| 442 | ||
| 443 | /** | |
| 444 | * `newApp` named from a sentence: an explicit `name` is never second-guessed; | |
| 445 | * left blank, the description names the app, and a derived name that | |
| 446 | * collides tries `base-2`, `base-3`, … on `name_taken`. A sentence with | |
| 447 | * nothing a name can be made from is refused as `no_name`. | |
| 448 | */ | |
| 449 | export async function newAppFromSentence( | |
| 450 | app: App, | |
| 451 | actor: User | undefined, | |
| 452 | input: Omit<NewAppInput, "name"> & { name?: string }, | |
| 453 | onPhase: (name: string, ms: number) => void = () => {}, | |
| 454 | ): Promise<Result<NewAppResult, NewAppError | "unverified" | "no_name">> { | |
| 455 | const explicit = (input.name ?? "").trim(); | |
| 456 | const base = explicit === "" ? deriveAppName(input.description ?? "") : explicit; | |
| 457 | if (base === undefined) return err("no_name"); | |
| 458 | let made = await newApp(app, actor, { ...input, name: base }, onPhase); | |
| 459 | for ( | |
| 460 | let n = 2; | |
| 461 | explicit === "" && !made.ok && made.error === "name_taken" && n <= MAX_NAME_ATTEMPTS; | |
| 462 | n++ | |
| 463 | ) | |
| 464 | made = await newApp(app, actor, { ...input, name: `${base}-${n}` }, onPhase); | |
| 465 | return made; | |
| 466 | } | |
| 467 | ||
| 439 | 468 | export interface AppView { |
| 440 | 469 | readonly app: AppRecord; |
| 441 | 470 | readonly environments: EnvStatus[]; |
src/actions/new_app_words.ts added +39
| @@ -0,0 +1,39 @@ | ||
| 1 | /** | |
| 2 | * What "New app" says when it is refused, in one place: the web form and the | |
| 3 | * terminal screen both read it, so the same refusal is the same words. | |
| 4 | */ | |
| 5 | ||
| 6 | import type { App } from "../app.ts"; | |
| 7 | import { storageLimitMessage } from "../core/storage.ts"; | |
| 8 | import { getUserByName, type User } from "../core/users.ts"; | |
| 9 | import { appLimitWords } from "./app_limit.ts"; | |
| 10 | import { NO_SUCH_AGENT } from "./crew_limit_words.ts"; | |
| 11 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "./repo_words.ts"; | |
| 12 | import { repositoryLimitMessage } from "./repos.ts"; | |
| 13 | ||
| 14 | /** Nothing in the sentence a slug can carry: punctuation only, or a script `[a-z0-9]` cannot. */ | |
| 15 | export const NO_NAME_ERROR = | |
| 16 | "That doesn't leave anything to make a name from — type one below."; | |
| 17 | ||
| 18 | export const NEW_APP_ERRORS: Record<string, string> = { | |
| 19 | name_taken: "That owner already has a repository by that name.", | |
| 20 | invalid_name: REPO_NAME_RULES, | |
| 21 | git_init_failed: REPO_INIT_FAILED, | |
| 22 | invalid_slug: | |
| 23 | "That name makes no usable hostname — start it with a letter or digit.", | |
| 24 | slug_taken: | |
| 25 | "Another app already answers to that hostname. Pick another name.", | |
| 26 | already_app: "That repository is already an app.", | |
| 27 | scaffold_failed: "The starter app could not be committed.", | |
| 28 | no_such_agent: NO_SUCH_AGENT, | |
| 29 | }; | |
| 30 | ||
| 31 | /** The words for a refused `newAppFromSentence`, for `actor` creating under `owner` (blank: themselves). */ | |
| 32 | export function newAppWords(app: App, actor: User, owner: string, error: string): string { | |
| 33 | if (error === "no_name") return NO_NAME_ERROR; | |
| 34 | if (error === "repository_limit") return `Not made: ${repositoryLimitMessage(app, actor)}.`; | |
| 35 | if (error === "storage_limit") return `Not made: ${storageLimitMessage(app, actor.id)}.`; | |
| 36 | const ownerId = getUserByName(app.db, owner || actor.username)?.id; | |
| 37 | if (error === "app_limit" && ownerId !== undefined) return appLimitWords(app, ownerId); | |
| 38 | return NEW_APP_ERRORS[error] ?? "That app could not be created."; | |
| 39 | } |
src/tui/forge.ts +8
| @@ -76,6 +76,7 @@ import { issuesScreen, newIssueScreen } from "./screens/issues.ts"; | ||
| 76 | 76 | import { limbsScreen } from "./screens/limbs.ts"; |
| 77 | 77 | import { newPullScreen } from "./screens/newpull.ts"; |
| 78 | 78 | import { newForgeScreen } from "./screens/newforge.ts"; |
| 79 | import { newAppScreen } from "./screens/newapp.ts"; | |
| 79 | 80 | import { newOrgScreen } from "./screens/neworg.ts"; |
| 80 | 81 | import { migrateScreen } from "./screens/migrate.ts"; |
| 81 | 82 | import { newRepoScreen } from "./screens/newrepo.ts"; |
| @@ -221,6 +222,13 @@ export function forgeCommands(session: Session): (ctx: Ctx) => Command[] { | ||
| 221 | 222 | hint: "an organization you own, for a client's apps", |
| 222 | 223 | run: () => ctx.push(newOrgScreen(session, ctx)), |
| 223 | 224 | }); |
| 225 | if (session.user !== undefined && session.user.kind !== "agent") | |
| 226 | commands.push({ | |
| 227 | id: "new-app", | |
| 228 | title: "New app", | |
| 229 | hint: "say what it does in a sentence; the crew starts on it", | |
| 230 | run: () => ctx.push(newAppScreen(session, ctx)), | |
| 231 | }); | |
| 224 | 232 | if (session.signedIn) |
| 225 | 233 | commands.push({ |
| 226 | 234 | id: "migrate", |
src/tui/screens/newapp.ts added +75
| @@ -0,0 +1,75 @@ | ||
| 1 | /** | |
| 2 | * "New app" in the terminal: say what it does in a sentence and the app is | |
| 3 | * made, named from the sentence, with the crew on it — `newAppFromSentence`, | |
| 4 | * as the web form calls it, so the name, the limits and the refusals are the | |
| 5 | * same words. A person in organizations is first asked whose it is. | |
| 6 | */ | |
| 7 | ||
| 8 | import { newAppFromSentence } from "../../actions/apps.ts"; | |
| 9 | import { NO_NAME_ERROR, newAppWords } from "../../actions/new_app_words.ts"; | |
| 10 | import { appsRuntime } from "../../apps/runtime.ts"; | |
| 11 | import { listOrgsForUser } from "../../core/orgs.ts"; | |
| 12 | import { deriveAppName } from "../../web/pages/apps-name.ts"; | |
| 13 | import type { Ctx, Screen } from "../app/app.ts"; | |
| 14 | import type { Session } from "../session.ts"; | |
| 15 | import { later, promptScreen, runAsync } from "./common.ts"; | |
| 16 | import { repoScreen } from "./repo.ts"; | |
| 17 | import { local } from "./where.ts"; | |
| 18 | ||
| 19 | /** Nothing drawn behind the prompt: the palette has closed already. */ | |
| 20 | const backdrop: Screen = { title: "New app", keys: [], render() {}, key: () => false }; | |
| 21 | ||
| 22 | export function newAppScreen(f: Session, ctx: Ctx): Screen { | |
| 23 | const owners = f.user | |
| 24 | ? [f.user.username, ...listOrgsForUser(f.db, f.user.id).map((o) => o.username)] | |
| 25 | : []; | |
| 26 | ||
| 27 | const sentence = (owner: string): Screen => | |
| 28 | promptScreen(ctx, backdrop, { | |
| 29 | title: "New app", | |
| 30 | label: "What should it do?", | |
| 31 | placeholder: "a shared shopping list for the flat", | |
| 32 | hint: "One sentence; it names the app and the crew starts on it.", | |
| 33 | onSubmit: (text) => { | |
| 34 | if (deriveAppName(text) === undefined) return NO_NAME_ERROR; | |
| 35 | return Promise.resolve().then(() => make(owner, text)); | |
| 36 | }, | |
| 37 | }); | |
| 38 | ||
| 39 | const make = (owner: string, text: string): void => { | |
| 40 | ctx.toast("Making your app… the crew starts on it next."); | |
| 41 | runAsync(ctx, async () => { | |
| 42 | const made = await newAppFromSentence(f.app, f.user, { | |
| 43 | owner, | |
| 44 | description: text, | |
| 45 | pickAgent: true, | |
| 46 | }); | |
| 47 | if (!made.ok) { | |
| 48 | ctx.toast(newAppWords(f.app, f.user!, owner, made.error), "danger"); | |
| 49 | return; | |
| 50 | } | |
| 51 | const { repo, app: record } = made.value; | |
| 52 | const url = appsRuntime(f.app)?.urlFor(record, "production"); | |
| 53 | ctx.toast( | |
| 54 | url === undefined | |
| 55 | ? `${repo.name} is an app, but nothing is serving apps on this forge.` | |
| 56 | : `${repo.name} is starting at ${url}.`, | |
| 57 | "success", | |
| 58 | ); | |
| 59 | ctx.push(repoScreen(f, ctx, local(f, repo))); | |
| 60 | }); | |
| 61 | }; | |
| 62 | ||
| 63 | if (owners.length < 2) return sentence(owners[0] ?? ""); | |
| 64 | return promptScreen(ctx, backdrop, { | |
| 65 | title: "New app", | |
| 66 | label: "Whose is it?", | |
| 67 | value: owners[0] ?? "", | |
| 68 | hint: `You, or one of: ${owners.slice(1).join(", ")}`, | |
| 69 | onSubmit: (value) => { | |
| 70 | const owner = value === "" ? owners[0]! : value; | |
| 71 | if (!owners.includes(owner)) return `Pick one of: ${owners.join(", ")}`; | |
| 72 | return later(ctx, () => sentence(owner)); | |
| 73 | }, | |
| 74 | }); | |
| 75 | } |
src/web/pages/apps.ts +30 −78
| @@ -12,15 +12,9 @@ | ||
| 12 | 12 | * 404 for everyone. |
| 13 | 13 | */ |
| 14 | 14 | |
| 15 | import { storageLimitMessage } from "../../core/storage.ts"; | |
| 16 | import { repositoryLimitMessage } from "../../actions/repos.ts"; | |
| 17 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "../../actions/repo_words.ts"; | |
| 18 | import { appLimitWords } from "../../actions/app_limit.ts"; | |
| 19 | import { NO_SUCH_AGENT } from "../../actions/crew_limit_words.ts"; | |
| 20 | import { getUserByName } from "../../core/users.ts"; | |
| 21 | 15 | import { |
| 22 | 16 | appView, |
| 23 | newApp, | |
| 17 | newAppFromSentence, | |
| 24 | 18 | openEnvUrl, |
| 25 | 19 | redeployEnv, |
| 26 | 20 | stopEnv, |
| @@ -28,7 +22,8 @@ import { | ||
| 28 | 22 | } from "../../actions/apps.ts"; |
| 29 | 23 | import { listAllAgents } from "../../actions/crew.ts"; |
| 30 | 24 | import { planningAvailable } from "../../crew/plan.ts"; |
| 31 | import { appNameJs, deriveAppName } from "./apps-name.ts"; | |
| 25 | import { NO_NAME_ERROR, newAppWords } from "../../actions/new_app_words.ts"; | |
| 26 | import { appNameJs } from "./apps-name.ts"; | |
| 32 | 27 | import { STARTERS, starterFromQuery, starterHref } from "./apps-starters.ts"; |
| 33 | 28 | import { previewEnv, type EnvName } from "../../apps/data.ts"; |
| 34 | 29 | import { envRefusalText, redeployedText, stoppedText } from "../../actions/env_words.ts"; |
| @@ -443,26 +438,6 @@ export function newAppPage(ctx: Ctx): Response { | ||
| 443 | 438 | return response; |
| 444 | 439 | } |
| 445 | 440 | |
| 446 | /** Nothing in the sentence a slug can carry: punctuation only, or a script `[a-z0-9]` cannot. */ | |
| 447 | const NO_NAME_ERROR = | |
| 448 | "That doesn't leave anything to make a name from — type one below."; | |
| 449 | ||
| 450 | /** How many suffixed names (`x-2`, `x-3`, …) a derived name will try before giving up. */ | |
| 451 | const MAX_NAME_ATTEMPTS = 30; | |
| 452 | ||
| 453 | const NEW_APP_ERRORS: Record<string, string> = { | |
| 454 | name_taken: "That owner already has a repository by that name.", | |
| 455 | invalid_name: REPO_NAME_RULES, | |
| 456 | git_init_failed: REPO_INIT_FAILED, | |
| 457 | invalid_slug: | |
| 458 | "That name makes no usable hostname — start it with a letter or digit.", | |
| 459 | slug_taken: | |
| 460 | "Another app already answers to that hostname. Pick another name.", | |
| 461 | already_app: "That repository is already an app.", | |
| 462 | scaffold_failed: "The starter app could not be committed.", | |
| 463 | no_such_agent: NO_SUCH_AGENT, | |
| 464 | }; | |
| 465 | ||
| 466 | 441 | // POST /apps/new |
| 467 | 442 | export async function newAppSubmit(ctx: Ctx): Promise<Response> { |
| 468 | 443 | if (!ctx.auth.user) return signInFirst(ctx); |
| @@ -478,52 +453,35 @@ export async function newAppSubmit(ctx: Ctx): Promise<Response> { | ||
| 478 | 453 | agent: field(form, "agent"), |
| 479 | 454 | }; |
| 480 | 455 | const pickAgent = values.agent === PICK_AGENT; |
| 481 | // An explicit name is never second-guessed; left blank, the sentence | |
| 482 | // names the app — and the server derives that name itself (never trusting | |
| 483 | // whatever the field carried in): the field is only a client-side preview. | |
| 484 | const explicitName = values.name.trim(); | |
| 485 | const base = explicitName === "" ? deriveAppName(values.description) : explicitName; | |
| 486 | if (base === undefined) { | |
| 487 | return page( | |
| 488 | { | |
| 489 | ctx, | |
| 490 | title: "New app", | |
| 491 | body: newAppForm(ctx, values, plannerOffered(ctx), NO_NAME_ERROR), | |
| 492 | scriptJs: appNameJs, | |
| 493 | }, | |
| 494 | 422, | |
| 495 | ); | |
| 496 | } | |
| 497 | const rest = { | |
| 498 | owner: values.owner, | |
| 499 | description: values.description, | |
| 500 | visibility: values.visibility === "public" ? "public" : "private", | |
| 501 | ...(pickAgent | |
| 502 | ? { pickAgent: true } | |
| 503 | : values.agent === "" | |
| 504 | ? {} | |
| 505 | : { agent: values.agent }), | |
| 506 | } as const; | |
| 507 | let made = await newApp(ctx.app, ctx.auth.user, { ...rest, name: base }, phase); | |
| 508 | // A derived name that collides tries `base-2`, `base-3`, … — the same | |
| 509 | // `name_taken` check an explicit name gets, just run more than once. | |
| 510 | for ( | |
| 511 | let n = 2; | |
| 512 | explicitName === "" && !made.ok && made.error === "name_taken" && n <= MAX_NAME_ATTEMPTS; | |
| 513 | n++ | |
| 514 | ) { | |
| 515 | made = await newApp(ctx.app, ctx.auth.user, { | |
| 516 | ...rest, | |
| 517 | name: `${base}-${n}`, | |
| 518 | }, phase); | |
| 519 | } | |
| 456 | const made = await newAppFromSentence( | |
| 457 | ctx.app, | |
| 458 | ctx.auth.user, | |
| 459 | { | |
| 460 | owner: values.owner, | |
| 461 | name: values.name, | |
| 462 | description: values.description, | |
| 463 | visibility: values.visibility === "public" ? "public" : "private", | |
| 464 | ...(pickAgent | |
| 465 | ? { pickAgent: true } | |
| 466 | : values.agent === "" | |
| 467 | ? {} | |
| 468 | : { agent: values.agent }), | |
| 469 | }, | |
| 470 | phase, | |
| 471 | ); | |
| 520 | 472 | if (!made.ok) { |
| 473 | if (made.error === "no_name") | |
| 474 | return page( | |
| 475 | { | |
| 476 | ctx, | |
| 477 | title: "New app", | |
| 478 | body: newAppForm(ctx, values, plannerOffered(ctx), NO_NAME_ERROR), | |
| 479 | scriptJs: appNameJs, | |
| 480 | }, | |
| 481 | 422, | |
| 482 | ); | |
| 521 | 483 | if (made.error === "forbidden_owner" || made.error === "unauthorized") |
| 522 | 484 | return new Response("forbidden", { status: 403 }); |
| 523 | const ownerId = getUserByName( | |
| 524 | ctx.app.db, | |
| 525 | values.owner || ctx.auth.user.username, | |
| 526 | )?.id; | |
| 527 | 485 | return page( |
| 528 | 486 | { |
| 529 | 487 | ctx, |
| @@ -532,13 +490,7 @@ export async function newAppSubmit(ctx: Ctx): Promise<Response> { | ||
| 532 | 490 | ctx, |
| 533 | 491 | values, |
| 534 | 492 | plannerOffered(ctx), |
| 535 | made.error === "repository_limit" | |
| 536 | ? `Not made: ${repositoryLimitMessage(ctx.app, ctx.auth.user!)}.` | |
| 537 | : made.error === "storage_limit" | |
| 538 | ? `Not made: ${storageLimitMessage(ctx.app, ctx.auth.user!.id)}.` | |
| 539 | : made.error === "app_limit" && ownerId !== undefined | |
| 540 | ? appLimitWords(ctx.app, ownerId) | |
| 541 | : (NEW_APP_ERRORS[made.error] ?? "That app could not be created."), | |
| 493 | newAppWords(ctx.app, ctx.auth.user, values.owner, made.error), | |
| 542 | 494 | ), |
| 543 | 495 | scriptJs: appNameJs, |
| 544 | 496 | }, |
test/crew-limit-words.test.ts +1 −1
| @@ -19,7 +19,7 @@ const read = (file: string): string => readFileSync(join(root, file), "utf8"); | ||
| 19 | 19 | const readers: Record<string, Record<string, string>> = { |
| 20 | 20 | "src/actions/migrate.ts": { AT_REPOSITORY_LIMIT, NO_SUCH_AGENT }, |
| 21 | 21 | "src/actions/repos.ts": { AT_REPOSITORY_LIMIT }, |
| 22 | "src/web/pages/apps.ts": { NO_SUCH_AGENT }, | |
| 22 | "src/actions/new_app_words.ts": { NO_SUCH_AGENT }, | |
| 23 | 23 | "src/fleet/verbs.ts": { AGENT_NOT_ON_CREW }, |
| 24 | 24 | "src/tui/screens/common.ts": { AGENT_NOT_ON_CREW }, |
| 25 | 25 | "src/http/mcp/tools/assign.ts": { AGENT_NOT_ON_CREW }, |
test/palette-roster.test.ts +2
| @@ -58,6 +58,7 @@ test("a site-admin person is offered the whole roster", () => { | ||
| 58 | 58 | ["settings", "Repository settings", "owner/name"], |
| 59 | 59 | ["new-repo", "New repository", undefined], |
| 60 | 60 | ["new-org", "New organization", "an organization you own, for a client's apps"], |
| 61 | ["new-app", "New app", "say what it does in a sentence; the crew starts on it"], | |
| 61 | 62 | ["migrate", "Migrate a repository", "bring one in from an origin or a URL, pick its branch, put a crew on it"], |
| 62 | 63 | ["new-forge", "New forge", "spawn a client's forge and get its claim link"], |
| 63 | 64 | ["asked", "Asked", "your thoughts and what became of them"], |
| @@ -188,6 +189,7 @@ test("a member person is offered the signed-in roster without the admin entries" | ||
| 188 | 189 | ["settings", "Repository settings", "owner/name"], |
| 189 | 190 | ["new-repo", "New repository", undefined], |
| 190 | 191 | ["new-org", "New organization", "an organization you own, for a client's apps"], |
| 192 | ["new-app", "New app", "say what it does in a sentence; the crew starts on it"], | |
| 191 | 193 | ["migrate", "Migrate a repository", "bring one in from an origin or a URL, pick its branch, put a crew on it"], |
| 192 | 194 | ["new-forge", "New forge", "spawn a client's forge and get its claim link"], |
| 193 | 195 | ["asked", "Asked", "your thoughts and what became of them"], |
test/repo-words.test.ts +1 −1
| @@ -10,7 +10,7 @@ import { join } from "node:path"; | ||
| 10 | 10 | import { REPO_INIT_FAILED, REPO_NAME_RULES } from "../src/actions/repo_words.ts"; |
| 11 | 11 | |
| 12 | 12 | const WORDS = { REPO_NAME_RULES, REPO_INIT_FAILED }; |
| 13 | const READERS = ["src/actions/migrate.ts", "src/web/pages/apps.ts", "src/web/pages/site.ts"]; | |
| 13 | const READERS = ["src/actions/migrate.ts", "src/actions/new_app_words.ts", "src/web/pages/site.ts"]; | |
| 14 | 14 | |
| 15 | 15 | describe("a repository's refusal words", () => { |
| 16 | 16 | for (const [name, words] of Object.entries(WORDS)) { |
test/tui-apps.test.ts +1 −1
| @@ -463,7 +463,7 @@ describe("terminal: apps, runs and the crew", () => { | ||
| 463 | 463 | await h.until("On you"); |
| 464 | 464 | // alice is the site admin, so the crew entry would have been there. |
| 465 | 465 | const ids = forgeCommands(h.session)(h.app.ctx).map((c) => c.id); |
| 466 | for (const gone of ["apps", "app", "runs", "new-app", "crew"]) | |
| 466 | for (const gone of ["apps", "app", "runs", "crew"]) | |
| 467 | 467 | expect(ids).not.toContain(gone); |
| 468 | 468 | expect(ids).toContain("repo"); |
| 469 | 469 | expect(ids).toContain("pulls"); |
test/tui-newapp.test.ts added +111
| @@ -0,0 +1,111 @@ | ||
| 1 | /** | |
| 2 | * "New app" in the terminal: a sentence makes an app named from it, a second | |
| 3 | * identical sentence makes `<name>-2`, and an empty sentence is refused with | |
| 4 | * the web form's words. | |
| 5 | */ | |
| 6 | import { afterAll, beforeAll, describe, expect, test } from "bun:test"; | |
| 7 | import { mkdtemp, rm } from "node:fs/promises"; | |
| 8 | import { tmpdir } from "node:os"; | |
| 9 | import { join } from "node:path"; | |
| 10 | import { NO_NAME_ERROR } from "../src/actions/new_app_words.ts"; | |
| 11 | import { createApp, type App } from "../src/app.ts"; | |
| 12 | import { getAppByRepo } from "../src/core/apps.ts"; | |
| 13 | import { getRepo } from "../src/core/repos.ts"; | |
| 14 | import { createUser, type User } from "../src/core/users.ts"; | |
| 15 | import { createForgeApp, forgeCommands, Session } from "../src/tui/forge.ts"; | |
| 16 | import { newAppScreen } from "../src/tui/screens/newapp.ts"; | |
| 17 | import { headlessTerminal } from "../src/tui/term/terminal.ts"; | |
| 18 | import { quietLogs } from "./helpers/globals.ts"; | |
| 19 | import { settleApp } from "./helpers/tui.ts"; | |
| 20 | ||
| 21 | quietLogs(); | |
| 22 | ||
| 23 | let dir: string; | |
| 24 | let app: App; | |
| 25 | let alice: User; | |
| 26 | ||
| 27 | const identity = (user?: User) => ({ | |
| 28 | sshUsername: user?.username ?? "git", | |
| 29 | remoteAddress: "127.0.0.1", | |
| 30 | fingerprint: "SHA256:test", | |
| 31 | user, | |
| 32 | }); | |
| 33 | ||
| 34 | beforeAll(async () => { | |
| 35 | dir = await mkdtemp(join(tmpdir(), "forge-tui-newapp-")); | |
| 36 | app = createApp({ dataDir: dir, url: "https://forge.test", port: 0 }); | |
| 37 | const made = await createUser(app.db, { | |
| 38 | origin: "seed", | |
| 39 | trusted: true, | |
| 40 | username: "alice", | |
| 41 | adminIfFirst: true, | |
| 42 | email: "[email protected]", | |
| 43 | password: "a-good-passphrase", | |
| 44 | }); | |
| 45 | if (!made.ok) throw new Error(made.error); | |
| 46 | alice = made.value; | |
| 47 | }); | |
| 48 | afterAll(async () => { | |
| 49 | app.db.close(); | |
| 50 | await rm(dir, { recursive: true, force: true }); | |
| 51 | }); | |
| 52 | ||
| 53 | async function withScreen( | |
| 54 | body: (h: { text: () => string; feed: (s: string) => Promise<void> }) => Promise<void>, | |
| 55 | ) { | |
| 56 | const terminal = headlessTerminal(120, 30); | |
| 57 | const { app: tui, session } = createForgeApp(app, identity(alice), { terminal, exitHooks: false }); | |
| 58 | tui.start(); | |
| 59 | tui.ctx.push(newAppScreen(session, tui.ctx)); | |
| 60 | try { | |
| 61 | await settleApp(tui); | |
| 62 | await body({ | |
| 63 | text: () => tui.screen.toText(), | |
| 64 | feed: async (s) => { | |
| 65 | terminal.feed(s); | |
| 66 | await settleApp(tui); | |
| 67 | }, | |
| 68 | }); | |
| 69 | } finally { | |
| 70 | tui.stop?.(); | |
| 71 | } | |
| 72 | } | |
| 73 | ||
| 74 | /** The work runs after the screen settles; wait for the repository it makes. */ | |
| 75 | async function madeBy(name: string): Promise<void> { | |
| 76 | for (let i = 0; i < 100 && !getRepo(app.db, "alice", name); i++) await Bun.sleep(50); | |
| 77 | } | |
| 78 | ||
| 79 | const SENTENCE = "a shared shopping list for the flat"; | |
| 80 | ||
| 81 | describe("the new app screen", () => { | |
| 82 | test("the palette offers it to a signed-in person, and never to an agent or a guest", () => { | |
| 83 | const ids = (u?: User) => forgeCommands(new Session(app, identity(u)))({} as never).map((c) => c.id); | |
| 84 | expect(ids(alice)).toContain("new-app"); | |
| 85 | expect(ids({ ...alice, kind: "agent" })).not.toContain("new-app"); | |
| 86 | expect(ids()).not.toContain("new-app"); | |
| 87 | }); | |
| 88 | ||
| 89 | test("an empty sentence is refused with the web's no-name words, and nothing is made", () => | |
| 90 | withScreen(async ({ text, feed }) => { | |
| 91 | await feed("\r"); | |
| 92 | expect(text()).toContain(NO_NAME_ERROR.slice(0, 30)); | |
| 93 | })); | |
| 94 | ||
| 95 | test("a sentence makes an app named from it, and a second makes name-2", async () => { | |
| 96 | await withScreen(async ({ feed }) => { | |
| 97 | await feed(`${SENTENCE}\r`); | |
| 98 | await madeBy("shared-shopping"); | |
| 99 | }); | |
| 100 | const first = getRepo(app.db, "alice", "shared-shopping"); | |
| 101 | expect(first).toBeDefined(); | |
| 102 | expect(getAppByRepo(app.db, first!.id)).toBeDefined(); | |
| 103 | await withScreen(async ({ feed }) => { | |
| 104 | await feed(`${SENTENCE}\r`); | |
| 105 | await madeBy("shared-shopping-2"); | |
| 106 | }); | |
| 107 | const second = getRepo(app.db, "alice", "shared-shopping-2"); | |
| 108 | expect(second).toBeDefined(); | |
| 109 | expect(getAppByRepo(app.db, second!.id)).toBeDefined(); | |
| 110 | }); | |
| 111 | }); |
new above
offline