Repository navigation
feat(website): read a workflow template as a picture of itself #19009
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { readdirSync, readFileSync } from 'node:fs' | ||
| import { join } from 'node:path' | ||
| import { describe, expect, it } from 'vitest' | ||
|
|
||
| import type { GraphNode } from './workflow-graph' | ||
| import { linkPath, readGraphPicture } from './workflow-graph' | ||
|
|
||
| const GRAPHS = join(process.cwd(), 'public/workflow-graphs') | ||
|
|
||
| const published = readdirSync(GRAPHS).filter((name) => name.endsWith('.json')) | ||
|
|
||
| const graphOf = (name: string) => | ||
| JSON.parse(readFileSync(`${GRAPHS}/${name}`, 'utf8')) as unknown | ||
|
|
||
| const BEFORE = 'https://media.example.com/before.png' | ||
| const AFTER = 'https://media.example.com/after.png' | ||
|
|
||
| describe('readGraphPicture', () => { | ||
| // The reader is only worth anything if it reads the graphs this site | ||
| // actually publishes, so the fixtures are those graphs rather than a hand | ||
| // written one that cannot go stale in the same way. | ||
| it.for(published)('draws %s', (name) => { | ||
| const picture = readGraphPicture(graphOf(name)) | ||
|
|
||
| expect(picture.nodes.length).toBeGreaterThan(0) | ||
| expect(picture.viewBox).toMatch(/^-?[\d.]+ -?[\d.]+ [\d.]+ [\d.]+$/) | ||
| for (const node of picture.nodes) { | ||
| expect(node.width).toBeGreaterThan(0) | ||
| expect(node.height).toBeGreaterThan(0) | ||
| } | ||
| // Every link joins two nodes the picture holds, or it is a line to | ||
| // nowhere. | ||
| for (const link of picture.links) | ||
| for (const end of [link.x1, link.y1, link.x2, link.y2]) | ||
| expect(Number.isFinite(end)).toBe(true) | ||
| }) | ||
|
|
||
| // Anything but a graph is a graph with nothing in it, not a throw: the page | ||
| // fetches this from the network and has to draw something either way. | ||
| it.for([undefined, null, 42, 'graph', {}, { nodes: 'none' }])( | ||
| 'reads %s as an empty picture', | ||
| (source) => { | ||
| const picture = readGraphPicture(source) | ||
|
|
||
| expect(picture.nodes).toEqual([]) | ||
| expect(picture.links).toEqual([]) | ||
| expect(picture.groups).toEqual([]) | ||
| } | ||
| ) | ||
|
|
||
| it('keeps a node the graph drew in its own colours', () => { | ||
| const picture = readGraphPicture({ | ||
| nodes: [ | ||
| { | ||
| id: 1, | ||
| type: 'SaveImage', | ||
| pos: [0, 0], | ||
| size: [200, 100], | ||
| color: '#123456', | ||
| bgcolor: '#abc' | ||
| } | ||
| ] | ||
| }) | ||
|
|
||
| expect(picture.nodes[0]?.header).toBe('#123456') | ||
| expect(picture.nodes[0]?.body).toBe('#aabbcc') | ||
| }) | ||
|
|
||
| it('fades a node the graph muted rather than dropping it', () => { | ||
| const picture = readGraphPicture({ | ||
| nodes: [ | ||
| { id: 1, type: 'SaveImage', pos: [0, 0], size: [200, 100], mode: 2 } | ||
| ] | ||
| }) | ||
|
|
||
| expect(picture.nodes[0]?.dimmed).toBe(true) | ||
| }) | ||
| }) | ||
|
|
||
| describe('the template’s own samples', () => { | ||
| const withSamples = (samples: readonly string[]) => | ||
| readGraphPicture(graphOf('animate-reference-sheet.json'), samples) | ||
|
|
||
| const hung = (nodes: readonly GraphNode[]) => | ||
| nodes.filter((node) => node.picture) | ||
|
|
||
| it('hangs the result in the node that hands it back', () => { | ||
| const picture = withSamples([AFTER]) | ||
|
|
||
| expect(hung(picture.nodes).map((node) => node.picture?.href)).toEqual([ | ||
| AFTER | ||
| ]) | ||
| expect(hung(picture.nodes)[0]?.role).toBe('output') | ||
| }) | ||
|
|
||
| // One picture is the result. Two are a before and an after, and the before | ||
| // belongs where the run takes it in. | ||
| it('hangs a before at the way in and an after at the way out', () => { | ||
| const picture = withSamples([BEFORE, AFTER]) | ||
| const held = hung(picture.nodes) | ||
|
|
||
| expect(held).toHaveLength(2) | ||
| expect(held.find((node) => node.role === 'input')?.picture?.href).toBe( | ||
| BEFORE | ||
| ) | ||
| expect(held.find((node) => node.role === 'output')?.picture?.href).toBe( | ||
| AFTER | ||
| ) | ||
| }) | ||
|
|
||
| it('hangs nothing when the template has no pictures', () => { | ||
| expect(hung(withSamples([]).nodes)).toEqual([]) | ||
| }) | ||
| }) | ||
|
|
||
| // The preview is meant to read like the editor, so a wire carries the colour | ||
| // the editor gives its kind, and a kind the editor has since added is grey | ||
| // rather than absent. | ||
| describe('the colours a kind is drawn in', () => { | ||
| const slotOf = (type: string) => | ||
| readGraphPicture({ | ||
| nodes: [ | ||
| { | ||
| id: 1, | ||
| type: 'Node', | ||
| pos: [0, 0], | ||
| size: [200, 100], | ||
| inputs: [{ name: 'in', type }] | ||
| } | ||
| ] | ||
| }).nodes[0]?.inputs[0] | ||
|
|
||
| it.for([ | ||
| ['IMAGE', '#64b5f6'], | ||
| ['image', '#64b5f6'], | ||
| ['MODEL', '#b39ddb'], | ||
| ['SOMETHING_NEW', '#8b8b8b'] | ||
| ] as const)('draws %s in %s', ([type, color]) => { | ||
| expect(slotOf(type).color).toBe(color) | ||
| }) | ||
| }) | ||
|
|
||
| describe('linkPath', () => { | ||
| it('leaves one end and arrives at the other', () => { | ||
| const path = linkPath({ | ||
| id: 'l1', | ||
| color: '#fff', | ||
| x1: 0, | ||
| y1: 0, | ||
| x2: 100, | ||
| y2: 50 | ||
| }) | ||
|
|
||
| expect(path.startsWith('M 0 0')).toBe(true) | ||
| expect(path.endsWith('100 50')).toBe(true) | ||
| }) | ||
| }) | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.