Skip to content
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

feat(protocol-designer): add python field to commands and timeline #17383

Merged
merged 1 commit into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions step-generation/src/__tests__/glue.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,30 @@ const divideCreator: any = (
}
}

const pythonHelloWorldCreator: any = (
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we typing this and the fn on line 108 as any?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see, this is a mock for the tests, right? should be fine to type as any actually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I was following the pattern in the mocks above. (In my private experimental branch, I have it typed as CommandCreator<{}> -- I'm not sure if that's the right way to write it.)

params: CountParams,
invariantContext: InvariantContext,
prevState: CountState
) => {
return {
commands: [],
warnings: [],
python: 'print("Hello world")',
}
}

const pythonGoodbyeWorldCreator: any = (
params: CountParams,
invariantContext: InvariantContext,
prevState: CountState
) => {
return {
commands: [],
warnings: [],
python: 'print("Goodbye world")',
}
}

function mockNextRobotStateAndWarningsSingleCommand(
command: CountCommand,
invariantContext: any,
Expand Down Expand Up @@ -177,6 +201,9 @@ describe('reduceCommandCreators', () => {
{ command: 'multiply', params: { value: 2 } },
],
warnings: [],
// Note no `python` field here.
// Existing CommandCreators that don't emit Python should behave exactly the same as before.
// This test makes sure we do NOT produce results like `python:'undefined'` or `python:''` or `python:'\n'`.
})
})

Expand Down Expand Up @@ -226,6 +253,43 @@ describe('reduceCommandCreators', () => {
],
})
})

it('Python commands are joined together', () => {
const initialState: any = {}
const result: any = reduceCommandCreators(
[
curryCommandCreator(pythonHelloWorldCreator, {}),
curryCommandCreator(pythonGoodbyeWorldCreator, {}),
],
invariantContext,
initialState
)

expect(result).toEqual({
commands: [],
warnings: [],
python: 'print("Hello world")\nprint("Goodbye world")',
})
})

it('Python commands mixed with non-Python commands', () => {
const initialState: any = {}
const result: any = reduceCommandCreators(
[
curryCommandCreator(addCreator, { value: 1 }),
curryCommandCreator(pythonHelloWorldCreator, {}),
],
invariantContext,
initialState
)

expect(result).toEqual({
commands: [{ command: 'add', params: { value: 1 } }],
warnings: [],
python: 'print("Hello world")',
// should only get 1 line of Python with no stray newlines or `undefined`s.
})
})
})

describe('commandCreatorsTimeline', () => {
Expand All @@ -236,6 +300,7 @@ describe('commandCreatorsTimeline', () => {
curryCommandCreator(addCreatorWithWarning, { value: 4 }),
curryCommandCreator(divideCreator, { value: 0 }),
curryCommandCreator(multiplyCreator, { value: 3 }),
curryCommandCreator(pythonHelloWorldCreator, {}),
],
invariantContext,
initialState
Expand Down Expand Up @@ -263,6 +328,7 @@ describe('commandCreatorsTimeline', () => {
],
},
// no more steps in the timeline, stopped by error
// python output is suppressed too
],
})
})
Expand All @@ -275,6 +341,7 @@ describe('commandCreatorsTimeline', () => {
curryCommandCreator(addCreatorWithWarning, { value: 3 }),
curryCommandCreator(multiplyCreator, { value: 2 }),
curryCommandCreator(addCreatorWithWarning, { value: 1 }),
curryCommandCreator(pythonHelloWorldCreator, {}),
],
invariantContext,
initialState
Expand Down Expand Up @@ -309,6 +376,13 @@ describe('commandCreatorsTimeline', () => {
},
],
},
// Python hello world
{
robotState: { count: 17 },
commands: [],
warnings: [],
python: 'print("Hello world")',
},
])
})
})
2 changes: 2 additions & 0 deletions step-generation/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ export interface CommandsAndRobotState {
commands: CreateCommand[]
robotState: RobotState
warnings?: CommandCreatorWarning[]
python?: string
}

export interface CommandCreatorErrorResponse {
Expand All @@ -629,6 +630,7 @@ export interface CommandCreatorErrorResponse {
export interface CommandsAndWarnings {
commands: CreateCommand[]
warnings?: CommandCreatorWarning[]
python?: string
}
export type CommandCreatorResult =
| CommandsAndWarnings
Expand Down
1 change: 1 addition & 0 deletions step-generation/src/utils/commandCreatorsTimeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const commandCreatorsTimeline = (
commands: commandCreatorResult.commands,
robotState: nextRobotStateAndWarnings.robotState,
warnings: commandCreatorResult.warnings,
python: commandCreatorResult.python,
}
return {
timeline: [...acc.timeline, nextResult],
Expand Down
7 changes: 7 additions & 0 deletions step-generation/src/utils/reduceCommandCreators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ interface CCReducerAcc {
commands: CreateCommand[]
errors: CommandCreatorError[]
warnings: CommandCreatorWarning[]
python?: string
}
export const reduceCommandCreators = (
commandCreators: CurriedCommandCreator[],
Expand All @@ -36,6 +37,10 @@ export const reduceCommandCreators = (
}
}
const allCommands = [...prev.commands, ...next.commands]
const allPython = [
...(prev.python ? [prev.python] : []),
...(next.python ? [next.python] : []),
].join('\n')
const updates = getNextRobotStateAndWarnings(
next.commands,
invariantContext,
Expand All @@ -50,6 +55,7 @@ export const reduceCommandCreators = (
...(next.warnings || []),
...updates.warnings,
],
...(allPython && { python: allPython }),
}
},
{
Expand All @@ -69,5 +75,6 @@ export const reduceCommandCreators = (
return {
commands: result.commands,
warnings: result.warnings,
...(result.python && { python: result.python }),
}
}
Loading