Public: anyone can read this; the crew can change it.
A .step/.stp CAD file opens as a part card: schema, product names and entity counts, with the source one tap away #12704
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/12676-a-step-stp-cad-file-opens-as-a-part-card → main 8 commits fork 4 threads · 4 unresolved
docs/design/areas/step.md added +3
| @@ -0,0 +1,3 @@ | ||
| 1 | # STEP part card | |
| 2 | ||
| 3 | The blob page's card for a `.step` or `.stp` file (`src/web/render/step.ts`). It adds no class: facts, entity types and products are `status-rows` (name as `dt`, value as `dd`), and the two section headings are the plain `h3` the other file renderers use (`eml`, `sql`, `openapi`). The card has no styles of its own, so it takes the page's rhythm, tokens and phone fit from `status-rows`. Counts and product numbers sit in the `dd`; "N more" lines are `muted small`. |
docs/features/README.md +1
| @@ -236,6 +236,7 @@ Read with the guide page [Your apps](../guide-your-apps.md). | ||
| 236 | 236 | - [](file-media.md) |
| 237 | 237 | - [](font.md) |
| 238 | 238 | - [](gcode.md) |
| 239 | - [](step.md) | |
| 239 | 240 | - [](geojson.md) |
| 240 | 241 | - [](gpx.md) |
| 241 | 242 | - [](html-preview.md) |
docs/features/step.md added +24
| @@ -0,0 +1,24 @@ | ||
| 1 | # STEP (.step, .stp) as a part card | |
| 2 | ||
| 3 | A `.step` or `.stp` file (ISO 10303-21 text) opens on its blob page as a card of facts about the part. Source is one tap away. No geometry is drawn. | |
| 4 | ||
| 5 | ## On the web | |
| 6 | ||
| 7 | Open the file. The card shows, as rows: | |
| 8 | ||
| 9 | - the header's schema, name, description, time stamp and the system that wrote it; | |
| 10 | - how many entities the file holds, in how many types; | |
| 11 | - the 20 most common entity types with their counts, and "N more types" past those; | |
| 12 | - the PRODUCT names, the first 50, and "N more" past those. | |
| 13 | ||
| 14 | The sample repository of a preview has `part.step` to try it on. | |
| 15 | ||
| 16 | ## What is read | |
| 17 | ||
| 18 | The HEADER section's `FILE_DESCRIPTION`, `FILE_NAME` and `FILE_SCHEMA`, and one scan of DATA for `#n=NAME(` entities and `PRODUCT('name'`. It is a summary, not a parse: a `#n=NAME(` inside a quoted string is counted too. | |
| 19 | ||
| 20 | A file that does not open with `ISO-10303-21;`, has no HEADER, or whose HEADER never ends is shown as text, with the reason. Over 5 MB the card is not made and the source is shown. | |
| 21 | ||
| 22 | ## In the terminal | |
| 23 | ||
| 24 | Open the file from a repository in `forge` over SSH: the same facts, entity types and products as a two-column table (`src/tui/screens/workspace.ts`, sharing `readStep` and `stepFacts` with the web). |
src/checks/looks.ts +1 −1
| @@ -31,7 +31,7 @@ const SAFE = /^[0-9A-Za-z]{1,64}$/; | ||
| 31 | 31 | |
| 32 | 32 | const MAX_BYTES = 1024 * 1024; |
| 33 | 33 | // The forge's own preview takes ~55 looks (`PREVIEW_OWNER_LOOKS`); every one must fit. |
| 34 | const MAX_LOOKS = 64; | |
| 34 | const MAX_LOOKS = 72; | |
| 35 | 35 | const MAX_WORDS = 80; |
| 36 | 36 | |
| 37 | 37 | const words = (v: unknown): string | null => |
src/spawn/preview-config.ts +23
| @@ -42,3 +42,26 @@ weight = 0 | ||
| 42 | 42 | `, |
| 43 | 43 | }, |
| 44 | 44 | ]; |
| 45 | ||
| 46 | /** | |
| 47 | * A STEP file for the sample, so the part card (docs/features/step.md) can be tried and looked at. | |
| 48 | */ | |
| 49 | export const PREVIEW_STEP_FILE: { readonly path: string; readonly content: string } = | |
| 50 | { | |
| 51 | path: "part.step", | |
| 52 | content: `ISO-10303-21; | |
| 53 | HEADER; | |
| 54 | FILE_DESCRIPTION(('A bracket, to try the part card on'),'2;1'); | |
| 55 | FILE_NAME('part.step','2026-01-02T03:04:05',('Ada'),('Acme'),'forge','CAD 1',''); | |
| 56 | FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 }')); | |
| 57 | ENDSEC; | |
| 58 | DATA; | |
| 59 | #1=PRODUCT('Bracket','Bracket','',(#9)); | |
| 60 | #2=PRODUCT('Bolt M6','Bolt M6','',(#9)); | |
| 61 | #3=CARTESIAN_POINT('',(0.,0.,0.)); | |
| 62 | #4=CARTESIAN_POINT('',(1.,0.,0.)); | |
| 63 | #5=ADVANCED_FACE('',(#3),#4,.T.); | |
| 64 | ENDSEC; | |
| 65 | END-ISO-10303-21; | |
| 66 | `, | |
| 67 | }; |
src/spawn/preview-looks.ts +4
| @@ -81,6 +81,9 @@ export const PREVIEW_MILESTONES_PAGE = `${PREVIEW_REPO_HOME}/milestones`; | ||
| 81 | 81 | /** The search page with a query the seed answers, whose phone look shows the fit (#9351). */ |
| 82 | 82 | export const PREVIEW_SEARCH_PAGE = "/search?q=sample"; |
| 83 | 83 | |
| 84 | /** The sample's STEP file, whose part card must fit a 390 px phone. */ | |
| 85 | export const PREVIEW_STEP_PAGE = `${PREVIEW_REPO_HOME}/blob/main/part.step`; | |
| 86 | ||
| 84 | 87 | /** The waves page, whose long headlines must wrap at a 390 px phone (#9325). */ |
| 85 | 88 | export const PREVIEW_WAVES_PAGE = `${PREVIEW_REPO_HOME}/waves`; |
| 86 | 89 | |
| @@ -187,6 +190,7 @@ export const PREVIEW_OWNER_LOOKS: readonly OwnerLook[] = [ | ||
| 187 | 190 | PREVIEW_BETS_PAGE, |
| 188 | 191 | PREVIEW_MILESTONES_PAGE, |
| 189 | 192 | PREVIEW_WAVES_PAGE, |
| 193 | PREVIEW_STEP_PAGE, | |
| 190 | 194 | PREVIEW_PULLS_PAGE, |
| 191 | 195 | PREVIEW_CHANGELOG_PAGE, |
| 192 | 196 | PREVIEW_NEXT_PAGE, |
src/spawn/preview-seed.ts +2 −2
| @@ -54,7 +54,7 @@ import { PREVIEW_SEEDED, getUserSetting, setUserSetting } from "../core/settings | ||
| 54 | 54 | import { createUser, getUserByName, type User } from "../core/users.ts"; |
| 55 | 55 | import { diskSummary, recordReading } from "../machines/disk_readings.ts"; |
| 56 | 56 | import { err, ok, type Result } from "../kernel/result.ts"; |
| 57 | import { PREVIEW_CONFIG_FILES } from "./preview-config.ts"; | |
| 57 | import { PREVIEW_CONFIG_FILES, PREVIEW_STEP_FILE } from "./preview-config.ts"; | |
| 58 | 58 | import { |
| 59 | 59 | PREVIEW_ADMIN, |
| 60 | 60 | PREVIEW_APP, |
| @@ -436,7 +436,7 @@ async function seedConfigFiles(app: App, admin: User): Promise<Result<void, stri | ||
| 436 | 436 | if (repo === undefined) return err("the sample repository is gone"); |
| 437 | 437 | const added = await commitToBranch(app, admin, repo, { |
| 438 | 438 | message: "Config files to try the form on", |
| 439 | files: [...PREVIEW_CONFIG_FILES], | |
| 439 | files: [...PREVIEW_CONFIG_FILES, PREVIEW_STEP_FILE], | |
| 440 | 440 | }); |
| 441 | 441 | if (!added.ok) return err(`the config files: ${JSON.stringify(added.error)}`); |
| 442 | 442 | setUserSetting(app.db, admin.id, CONFIG_SEEDED, new Date().toISOString()); |
src/tui/screens/workspace.ts +25
| @@ -58,6 +58,7 @@ import { detectLanguage, highlightLines } from "../../web/highlight.ts"; | ||
| 58 | 58 | import { MAX_COLUMNS, parseCsv, type Table } from "../../text/csv.ts"; |
| 59 | 59 | import { parseGeoJson, propertyRows } from "../../web/render/geojson.ts"; |
| 60 | 60 | import { gcodeFacts, parseGcode } from "../../web/render/gcode.ts"; |
| 61 | import { readStep, stepFacts } from "../../web/render/step.ts"; | |
| 61 | 62 | import { parseHar, ROW_CAP as HAR_ROW_CAP, duration as harDuration } from "../../web/render/har.ts"; |
| 62 | 63 | import { gpxFacts, parseGpx } from "../../web/render/gpx.ts"; |
| 63 | 64 | import { CASE_CAP, parseJunit } from "../../web/render/junit.ts"; |
| @@ -646,6 +647,25 @@ function gcodeTable(source: string): Table | null { | ||
| 646 | 647 | return { header: ["Fact", "Value"], rows: gcodeFacts(parsed).map(([k, v]) => [k, v]), more: 0, delimiter: ",", columns: 2 }; |
| 647 | 648 | } |
| 648 | 649 | |
| 650 | /** | |
| 651 | * `.step` / `.stp`: a CAD part, drawn as a table of its header facts, entity types and products. | |
| 652 | * Shares the web's reader. | |
| 653 | */ | |
| 654 | function isStepPath(path: string): boolean { | |
| 655 | return /\.(step|stp)$/i.test(path); | |
| 656 | } | |
| 657 | ||
| 658 | function stepTable(source: string): Table | string { | |
| 659 | const card = readStep(source); | |
| 660 | if ("error" in card) return `not drawn as a part card: ${card.error}`; | |
| 661 | const rows: string[][] = [ | |
| 662 | ...stepFacts(card).map(([k, v]) => [k, v]), | |
| 663 | ...card.types.map(([t, c]) => ["Type " + t, c.toLocaleString("en")]), | |
| 664 | ...card.products.map((p, i) => ["Product " + String(i + 1), p]), | |
| 665 | ]; | |
| 666 | return { header: ["Fact", "Value"], rows, more: card.moreProducts, delimiter: ",", columns: 2 }; | |
| 667 | } | |
| 668 | ||
| 649 | 669 | /** `.geojson`: features, drawn as a table of their geometry type and properties. */ |
| 650 | 670 | function isGeoJsonPath(path: string): boolean { |
| 651 | 671 | return /\.geojson$/i.test(path); |
| @@ -906,6 +926,11 @@ class Tab { | ||
| 906 | 926 | } else if (isGcodePath(this.path)) { |
| 907 | 927 | this.table = gcodeTable(new TextDecoder().decode(source)); |
| 908 | 928 | if (this.table !== null) this.grid = layoutTable(this.table); |
| 929 | } else if (isStepPath(this.path)) { | |
| 930 | const part = stepTable(new TextDecoder().decode(source)); | |
| 931 | this.table = typeof part === "string" ? null : part; | |
| 932 | this.fault = typeof part === "string" ? part : ""; | |
| 933 | if (this.table !== null) this.grid = layoutTable(this.table); | |
| 909 | 934 | } else if (isGeoJsonPath(this.path)) { |
| 910 | 935 | this.table = geoJsonTable(new TextDecoder().decode(source)); |
| 911 | 936 | if (this.table !== null) this.grid = layoutTable(this.table); |
src/web/render/kinds.ts +11 −1
| @@ -39,6 +39,7 @@ export type FileKind = | ||
| 39 | 39 | | "mail" |
| 40 | 40 | | "gcode" |
| 41 | 41 | | "dxf" |
| 42 | | "step" | |
| 42 | 43 | | "sarif" |
| 43 | 44 | | "lcov" |
| 44 | 45 | | "junit" |
| @@ -88,6 +89,7 @@ export const RENDER_CAP: Readonly<Record<FileKind, number>> = { | ||
| 88 | 89 | mail: 5 * MB, |
| 89 | 90 | gcode: 50 * MB, |
| 90 | 91 | dxf: 5 * MB, |
| 92 | step: 5 * MB, | |
| 91 | 93 | sarif: 5 * MB, |
| 92 | 94 | lcov: 5 * MB, |
| 93 | 95 | junit: 5 * MB, |
| @@ -223,6 +225,8 @@ const TEXTUAL: Readonly<Record<string, FileKind>> = { | ||
| 223 | 225 | gcode: "gcode", |
| 224 | 226 | gco: "gcode", |
| 225 | 227 | dxf: "dxf", |
| 228 | step: "step", | |
| 229 | stp: "step", | |
| 226 | 230 | sarif: "sarif", |
| 227 | 231 | lcov: "lcov", |
| 228 | 232 | sql: "sql", |
| @@ -276,6 +280,10 @@ const isArchive = (path: string, head: Uint8Array): boolean => { | ||
| 276 | 280 | const isDxfHead = (head: Uint8Array): boolean => |
| 277 | 281 | /^\uFEFF?\s*0\r?\nSECTION\r?\n/.test(new TextDecoder().decode(head.subarray(0, 64))); |
| 278 | 282 | |
| 283 | /** A STEP file opens on `ISO-10303-21;`; a `.step` or `.stp` without it stays text. */ | |
| 284 | const isStepHead = (head: Uint8Array): boolean => | |
| 285 | /^\uFEFF?\s*ISO-10303-21\s*;/.test(new TextDecoder().decode(head.subarray(0, 64))); | |
| 286 | ||
| 279 | 287 | /** For a caller that has only a name: the kind its extension gives. */ |
| 280 | 288 | export const NO_BYTES: Uint8Array = new Uint8Array(0); |
| 281 | 289 | |
| @@ -332,7 +340,9 @@ export function fileKind(path: string, head: Uint8Array): FileKind { | ||
| 332 | 340 | if (isDockerfileName(path)) return "dockerfile"; |
| 333 | 341 | if (isWorkflowName(path)) return "workflow"; |
| 334 | 342 | if (isComposeName(path)) return "compose"; |
| 335 | return TEXTUAL[extension(path)] ?? (isDxfHead(head) ? "dxf" : "text"); | |
| 343 | const ext = extension(path); | |
| 344 | if (ext === "step" || ext === "stp") return isStepHead(head) ? "step" : "text"; | |
| 345 | return TEXTUAL[ext] ?? (isDxfHead(head) ? "dxf" : "text"); | |
| 336 | 346 | } |
| 337 | 347 | |
| 338 | 348 | /** Kinds whose renderer links paths to files in the repository, so the page reads the ref's file list. */ |
src/web/render/renderers.ts +2
| @@ -17,6 +17,7 @@ import { eml } from "./eml.ts"; | ||
| 17 | 17 | import { csv } from "./csv.ts"; |
| 18 | 18 | import { geojson } from "./geojson.ts"; |
| 19 | 19 | import { dxf } from "./dxf.ts"; |
| 20 | import { step } from "./step.ts"; | |
| 20 | 21 | import { gcode } from "./gcode.ts"; |
| 21 | 22 | import { gpx } from "./gpx.ts"; |
| 22 | 23 | import { har } from "./har.ts"; |
| @@ -106,6 +107,7 @@ export const RENDERERS: Partial<Readonly<Record<FileKind, Renderer>>> = { | ||
| 106 | 107 | har, |
| 107 | 108 | gcode, |
| 108 | 109 | dxf, |
| 110 | step, | |
| 109 | 111 | geojson, |
| 110 | 112 | calendar: ics, |
| 111 | 113 | contacts: vcf, |
src/web/render/step.ts added +120
| @@ -0,0 +1,120 @@ | ||
| 1 | /** | |
| 2 | * A STEP file (`.step`, `.stp`, ISO 10303-21) as a part card: the HEADER's | |
| 3 | * description, name, author and schema, the entity types counted in DATA | |
| 4 | * (the top 20), and the PRODUCT names (the first 50). No geometry is drawn | |
| 5 | * and no tree is built: DATA is scanned once with a global pattern and only | |
| 6 | * the counts and names are kept. A file with no HEADER section, or with one | |
| 7 | * that never ends, falls back to the source view. Every string is escaped by | |
| 8 | * `html`. A `#n=NAME(` inside a quoted string would be counted as an entity; | |
| 9 | * the counts are a summary, not a parse. | |
| 10 | */ | |
| 11 | ||
| 12 | import { html } from "../html.ts"; | |
| 13 | import type { Rendered, RenderInput } from "./renderers.ts"; | |
| 14 | import { RENDER_CAP } from "./kinds.ts"; | |
| 15 | import { fileSize } from "../ui.ts"; | |
| 16 | ||
| 17 | export const TOP_TYPES = 20; | |
| 18 | export const PRODUCT_CAP = 50; | |
| 19 | const TEXT_CHARS = 200; | |
| 20 | ||
| 21 | export interface Card { | |
| 22 | readonly description: string[]; | |
| 23 | readonly name: string[]; | |
| 24 | readonly schema: string[]; | |
| 25 | /** Entity types by count, most first, at most TOP_TYPES. */ | |
| 26 | readonly types: [string, number][]; | |
| 27 | readonly typeKinds: number; | |
| 28 | readonly entities: number; | |
| 29 | readonly products: string[]; | |
| 30 | readonly moreProducts: number; | |
| 31 | } | |
| 32 | ||
| 33 | /** The quoted strings of one HEADER statement, `''` read as an apostrophe. */ | |
| 34 | const strings = (statement: string): string[] => | |
| 35 | [...statement.matchAll(/'((?:[^']|'')*)'/g)] | |
| 36 | .map((m) => (m[1] ?? "").replaceAll("''", "'").trim().slice(0, TEXT_CHARS)) | |
| 37 | .filter((s) => s !== ""); | |
| 38 | ||
| 39 | const statement = (header: string, name: string): string => | |
| 40 | new RegExp(`${name}\\s*\\(([\\s\\S]*?)\\)\\s*;`, "i").exec(header)?.[1] ?? ""; | |
| 41 | ||
| 42 | /** Read a STEP file's text. Pure. An error string when it is no part card. */ | |
| 43 | export function readStep(text: string): Card | { error: string } { | |
| 44 | const src = text.replace(/^/, "").replace(/\/\*[\s\S]*?\*\//g, ""); | |
| 45 | if (!/^\s*ISO-10303-21\s*;/.test(src)) return { error: "it does not open with ISO-10303-21" }; | |
| 46 | const start = /\bHEADER\s*;/i.exec(src); | |
| 47 | if (start === null) return { error: "it has no HEADER section" }; | |
| 48 | const rest = src.slice(start.index + start[0].length); | |
| 49 | const end = /\bENDSEC\s*;/i.exec(rest); | |
| 50 | if (end === null) return { error: "its HEADER section never ends" }; | |
| 51 | const header = rest.slice(0, end.index); | |
| 52 | const afterHeader = rest.slice(end.index + end[0].length); | |
| 53 | const data = /\bDATA\s*;/i.exec(afterHeader); | |
| 54 | const body = data === null ? "" : afterHeader.slice(data.index + data[0].length); | |
| 55 | ||
| 56 | const counts = new Map<string, number>(); | |
| 57 | let entities = 0; | |
| 58 | for (const m of body.matchAll(/#\d+\s*=\s*([A-Z][A-Z0-9_]*)\s*\(/gi)) { | |
| 59 | const type = (m[1] ?? "").toUpperCase(); | |
| 60 | counts.set(type, (counts.get(type) ?? 0) + 1); | |
| 61 | entities++; | |
| 62 | } | |
| 63 | const products: string[] = []; | |
| 64 | let productTotal = 0; | |
| 65 | for (const m of body.matchAll(/#\d+\s*=\s*PRODUCT\s*\(\s*'((?:[^']|'')*)'/gi)) { | |
| 66 | productTotal++; | |
| 67 | if (products.length < PRODUCT_CAP) products.push((m[1] ?? "").replaceAll("''", "'").slice(0, TEXT_CHARS)); | |
| 68 | } | |
| 69 | const types = [...counts].sort((a, b) => b[1] - a[1] || a[0].localeCompare(b[0])); | |
| 70 | return { | |
| 71 | description: strings(statement(header, "FILE_DESCRIPTION")), | |
| 72 | name: strings(statement(header, "FILE_NAME")), | |
| 73 | schema: strings(statement(header, "FILE_SCHEMA")), | |
| 74 | types: types.slice(0, TOP_TYPES), | |
| 75 | typeKinds: types.length, | |
| 76 | entities, | |
| 77 | products, | |
| 78 | moreProducts: productTotal - products.length, | |
| 79 | }; | |
| 80 | } | |
| 81 | ||
| 82 | const n = (x: number): string => x.toLocaleString("en"); | |
| 83 | ||
| 84 | /** | |
| 85 | * The card's facts as label and value, in the order both doors show them. | |
| 86 | * Shared with the terminal. | |
| 87 | */ | |
| 88 | export function stepFacts(card: Card): [string, string][] { | |
| 89 | // FILE_NAME: name, time stamp, authors, organisations, preprocessor, system, authorisation. | |
| 90 | const [name, stamp, ...made] = card.name; | |
| 91 | const facts: [string, string][] = [ | |
| 92 | ["Schema", card.schema.join(", ")], | |
| 93 | ["Name", name ?? ""], | |
| 94 | ["Description", card.description.join(" · ")], | |
| 95 | ["Written", stamp ?? ""], | |
| 96 | ["Made with", made.join(" · ")], | |
| 97 | ]; | |
| 98 | return [...facts.filter(([, v]) => v !== ""), ["Entities", `${n(card.entities)} in ${n(card.typeKinds)} types`]]; | |
| 99 | } | |
| 100 | ||
| 101 | const rows = (list: [string, string][]) => | |
| 102 | html`${list.map(([k, v]) => html`<div class="status-row"><dt>${k}</dt><dd>${v}</dd></div>`)}`; | |
| 103 | ||
| 104 | export function step(input: RenderInput): Rendered { | |
outdated
Principle 3: bare |
||
| 105 | const fall = (why: string): Rendered => ({ content: html``, lines: 0, fallback: `This file is shown as text because ${why}.` }); | |
outdated
Principles 2 and 7: entity types and counts are facts, so make them |
||
| 106 | if (input.size > RENDER_CAP.step || input.body.length > RENDER_CAP.step) return fall("it is too large to summarise"); | |
| 107 | const card = readStep(new TextDecoder().decode(input.body)); | |
| 108 | if ("error" in card) return fall(card.error); | |
outdated
Principle 7: product names are a list of rows; render them as rows, not a |
||
| 109 | const moreTypes = card.typeKinds - card.types.length; | |
| 110 | const content = html`<section> | |
| 111 | <dl class="status-rows">${rows(stepFacts(card))}</dl> | |
| 112 | <h3>Entity types</h3> | |
| 113 | <dl class="status-rows" aria-label="Entity types">${rows(card.types.map(([t, c]) => [t, n(c)]))}</dl> | |
| 114 | ${moreTypes > 0 ? html`<p class="muted small">${n(moreTypes)} more types</p>` : html``} | |
| 115 | ${card.products.length > 0 | |
| 116 | ? html`<h3>Products</h3><dl class="status-rows" aria-label="Products">${rows(card.products.map((p, i) => [String(i + 1), p]))}</dl>${card.moreProducts > 0 ? html`<p class="muted small">${n(card.moreProducts)} more</p>` : html``}` | |
| 117 | : html``} | |
| 118 | </section>`; | |
| 119 | return { content, lines: 0, summary: `${n(card.entities)} entities · ${fileSize(input.size)}` }; | |
| 120 | } |
test/render-kinds.test.ts +2
| @@ -82,6 +82,8 @@ const SAMPLES: Record<string, [Uint8Array, FileKind, string]> = { | ||
| 82 | 82 | gcode: [enc("G28\nG1 X1 E1\n"), "gcode", "application/octet-stream"], |
| 83 | 83 | gco: [enc("G28\n"), "gcode", "application/octet-stream"], |
| 84 | 84 | dxf: [enc("0\nSECTION\n2\nENTITIES\n0\nENDSEC\n0\nEOF\n"), "dxf", "application/octet-stream"], |
| 85 | step: [enc("ISO-10303-21;\nHEADER;\n"), "step", "application/octet-stream"], | |
| 86 | stp: [enc("ISO-10303-21;\nHEADER;\n"), "step", "application/octet-stream"], | |
| 85 | 87 | har: [enc(`{"log":{"entries":[]}}`), "har", "application/octet-stream"], |
| 86 | 88 | sql: [enc("CREATE TABLE a (id int);\n"), "sql", "application/octet-stream"], |
| 87 | 89 | graphql: [enc("type A { b: Int }\n"), "graphql", "application/octet-stream"], |
test/render-step.test.ts added +87
| @@ -0,0 +1,87 @@ | ||
| 1 | /** STEP files as a part card. */ | |
| 2 | import { describe, expect, test } from "bun:test"; | |
| 3 | import { PRODUCT_CAP, readStep, step, TOP_TYPES } from "../src/web/render/step.ts"; | |
| 4 | import { fileKind, RENDER_CAP } from "../src/web/render/kinds.ts"; | |
| 5 | import { RENDERERS } from "../src/web/render/renderers.ts"; | |
| 6 | ||
| 7 | const enc = (s: string): Uint8Array => new TextEncoder().encode(s); | |
| 8 | const HEAD = `ISO-10303-21; | |
| 9 | HEADER; | |
| 10 | FILE_DESCRIPTION(('Bracket','ViewDefinition [x]'),'2;1'); | |
| 11 | FILE_NAME('bracket.stp','2026-01-02T03:04:05',('Ada'),('Acme'),'pre','CAD 1',''); | |
| 12 | FILE_SCHEMA(('AUTOMOTIVE_DESIGN { 1 0 10303 214 }')); | |
| 13 | ENDSEC; | |
| 14 | `; | |
| 15 | const DATA = `DATA; | |
| 16 | #1=PRODUCT('Bracket','Bracket','',(#9)); | |
| 17 | #2=PRODUCT('O''Ring','x','',(#9)); | |
| 18 | #3=CARTESIAN_POINT('',(0.,0.,0.)); | |
| 19 | #4=CARTESIAN_POINT('',(1.,0.,0.)); | |
| 20 | #5 = ADVANCED_FACE('',(#3),#4,.T.); | |
| 21 | ENDSEC; | |
| 22 | END-ISO-10303-21; | |
| 23 | `; | |
| 24 | const draw = (text: string, size = text.length) => | |
| 25 | step({ path: "a.stp", size, body: enc(text), media: "/m", download: "/d" }); | |
| 26 | ||
| 27 | describe("step", () => { | |
| 28 | test("registered, by extension and by first bytes", () => { | |
| 29 | expect(fileKind("a.step", enc(HEAD))).toBe("step"); | |
| 30 | expect(fileKind("a.STP", enc(HEAD))).toBe("step"); | |
| 31 | expect(fileKind("a.stp", enc("hello\n"))).toBe("text"); | |
| 32 | expect(fileKind("a.txt", enc(HEAD))).toBe("text"); | |
| 33 | expect(RENDERERS.step).toBe(step); | |
| 34 | expect(RENDER_CAP.step).toBeGreaterThan(0); | |
| 35 | }); | |
| 36 | ||
| 37 | test("a valid file gives its header, counts and products", () => { | |
| 38 | const c = readStep(HEAD + DATA); | |
| 39 | if ("error" in c) throw new Error(c.error); | |
| 40 | expect(c.entities).toBe(5); | |
| 41 | expect(c.types[0]).toEqual(["CARTESIAN_POINT", 2]); | |
| 42 | expect(c.products).toEqual(["Bracket", "O'Ring"]); | |
| 43 | expect(c.schema[0]).toContain("AUTOMOTIVE_DESIGN"); | |
| 44 | const out = String(draw(HEAD + DATA).content); | |
| 45 | expect(out).toContain("bracket.stp"); | |
| 46 | expect(out).toContain("<dt>CARTESIAN_POINT</dt><dd>2</dd>"); | |
| 47 | expect(out).not.toContain("<ul"); | |
| 48 | expect(out).not.toContain("no geometry"); | |
| 49 | expect(out).toContain("O'Ring"); | |
| 50 | expect(out).toContain("Ada"); | |
| 51 | expect(out).not.toContain("<svg"); | |
| 52 | }); | |
| 53 | ||
| 54 | test("only the top types and the first products are listed", () => { | |
| 55 | let data = "DATA;\n"; | |
| 56 | for (let i = 0; i < 30; i++) data += `#${i + 1}=TYPE_${String(i).padStart(2, "0")}();\n`; | |
| 57 | for (let i = 0; i < 60; i++) data += `#${100 + i}=PRODUCT('p${i}','','',());\n`; | |
| 58 | const c = readStep(HEAD + data + "ENDSEC;\n"); | |
| 59 | if ("error" in c) throw new Error(c.error); | |
| 60 | expect(c.types.length).toBe(TOP_TYPES); | |
| 61 | expect(c.typeKinds).toBe(31); | |
| 62 | expect(c.products.length).toBe(PRODUCT_CAP); | |
| 63 | expect(c.moreProducts).toBe(10); | |
| 64 | }); | |
| 65 | ||
| 66 | test("a truncated data section still draws; a truncated or missing header falls back", () => { | |
| 67 | const cut = String(draw(HEAD + DATA.slice(0, 60)).content); | |
| 68 | expect(cut).toContain("Entity types"); | |
| 69 | expect(draw("ISO-10303-21;\nHEADER;\nFILE_NAME('a'").fallback).toContain("never ends"); | |
| 70 | expect(draw("ISO-10303-21;\nDATA;\n#1=A();\nENDSEC;\n").fallback).toContain("no HEADER"); | |
| 71 | expect(draw("hello").fallback).toContain("ISO-10303-21"); | |
| 72 | }); | |
| 73 | ||
| 74 | test("hostile strings are escaped", () => { | |
| 75 | const evil = HEAD.replace("'Ada'", "'<script>alert(1)</script>'").replace("bracket.stp", "<img src=x onerror=1>"); | |
| 76 | const out = String(draw(evil + DATA.replace("'Bracket','Bracket'", "'<b onclick=1>','x'")).content); | |
| 77 | expect(out).not.toContain("<script>"); | |
| 78 | expect(out).not.toContain("<img"); | |
| 79 | expect(out).not.toContain("<b onclick"); | |
| 80 | expect(out).toContain("<script>"); | |
| 81 | }); | |
| 82 | ||
| 83 | test("over the cap falls back to source", () => { | |
| 84 | const r = draw(HEAD + DATA, RENDER_CAP.step + 1); | |
| 85 | expect(r.fallback).toContain("too large"); | |
| 86 | }); | |
| 87 | }); |
test/tui-workspace-formats.test.ts +21
| @@ -563,6 +563,27 @@ describe("the terminal workspace, by file type", () => { | ||
| 563 | 563 | } |
| 564 | 564 | }); |
| 565 | 565 | |
| 566 | test("a .step opens as its facts, types and products, sharing the web reader, and s flips to the source", async () => { | |
| 567 | const made = await createRepo(forge, alice, { name: "parts", visibility: "public", description: "cad" }); | |
| 568 | if (!made.ok) throw new Error("repo failed"); | |
| 569 | const content = | |
| 570 | "ISO-10303-21;\nHEADER;\nFILE_DESCRIPTION(('Bracket'),'2;1');\nFILE_NAME('b.step','2026-01-02',('Ada'),('Acme'),'p','CAD','');\nFILE_SCHEMA(('AUTOMOTIVE_DESIGN'));\nENDSEC;\nDATA;\n#1=PRODUCT('Widget7','x','',(#9));\n#2=CARTESIAN_POINT('',(0.,0.,0.));\nENDSEC;\nEND-ISO-10303-21;\n"; | |
| 571 | await commitToBranch(forge, alice, made.value, { message: "seed", files: [{ path: "b.step", content }] }); | |
| 572 | const h = boot(); | |
| 573 | try { | |
| 574 | await h.settle(); | |
| 575 | await open(h, "parts"); | |
| 576 | await openFile(h, "b.step"); | |
| 577 | expect(h.text()).toContain("AUTOMOTIVE_DESIGN"); | |
| 578 | expect(h.text()).toContain("Widget7"); | |
| 579 | expect(h.text()).toContain("CARTESIAN_POINT"); | |
| 580 | await h.press("s"); | |
| 581 | expect(h.text()).toContain("FILE_SCHEMA"); | |
| 582 | } finally { | |
| 583 | h.stop(); | |
| 584 | } | |
| 585 | }); | |
| 586 | ||
| 566 | 587 | test("a .geojson opens as its features table, sharing the web parser, and s flips to the source", async () => { |
| 567 | 588 | const made = await createRepo(forge, alice, { name: "places", visibility: "public", description: "geo" }); |
| 568 | 589 | if (!made.ok) throw new Error("repo failed"); |
Principle 1 and 8: this sentence explains the page to itself ("no geometry is drawn"). The page is the card; delete the line. Help, if any, belongs in docs/features/step.md.