bad code on exit-sketch should no delete code (#2890)

* bad code on exitsketch should no delete code

* clean up
This commit is contained in:
Kurt Hutten
2024-07-03 22:03:04 +10:00
committed by GitHub
parent 4e6429de49
commit 220fe5b2b8
2 changed files with 67 additions and 5 deletions

View File

@ -4426,6 +4426,64 @@ test.describe('Sketch tests', () => {
await doSnapAtDifferentScales(page, [0, 10000, 10000]) await doSnapAtDifferentScales(page, [0, 10000, 10000])
}) })
}) })
test("Existing sketch with bad code delete user's code", async ({ page }) => {
// this was a regression https://github.com/KittyCAD/modeling-app/issues/2832
await page.addInitScript(async () => {
localStorage.setItem(
'persistCode',
`const sketch001 = startSketchOn('XZ')
|> startProfileAt([-0.45, 0.87], %)
|> line([1.32, 0.38], %)
|> line([1.02, -1.32], %, $seg01)
|> line([-1.01, -0.77], %)
|> lineTo([profileStartX(%), profileStartY(%)], %)
|> close(%)
const extrude001 = extrude(5, sketch001)
`
)
})
const u = await getUtils(page)
await page.setViewportSize({ width: 1200, height: 500 })
await u.waitForAuthSkipAppStart()
await u.openDebugPanel()
await u.expectCmdLog('[data-message-type="execution-done"]')
await u.closeDebugPanel()
await page.getByRole('button', { name: 'Start Sketch' }).click()
await page.mouse.click(622, 355)
await page.waitForTimeout(800)
await page.getByText(`END')`).click()
await page.keyboard.press('End')
await page.keyboard.press('Enter')
await page.keyboard.type(' |>', { delay: 100 })
await page.waitForTimeout(100)
await expect(page.locator('.cm-lint-marker-error')).toBeVisible()
await page.getByRole('button', { name: 'Exit Sketch' }).click()
await expect(
page.getByRole('button', { name: 'Start Sketch' })
).toBeVisible()
await expect((await u.codeLocator.innerText()).replace(/\s/g, '')).toBe(
`const sketch001 = startSketchOn('XZ')
|> startProfileAt([-0.45, 0.87], %)
|> line([1.32, 0.38], %)
|> line([1.02, -1.32], %, $seg01)
|> line([-1.01, -0.77], %)
|> lineTo([profileStartX(%), profileStartY(%)], %)
|> close(%)
const extrude001 = extrude(5, sketch001)
const sketch002 = startSketchOn(extrude001, 'END')
|>
`.replace(/\s/g, '')
)
})
}) })
test.describe('Testing constraints', () => { test.describe('Testing constraints', () => {

View File

@ -479,11 +479,15 @@ export const ModelingMachineProvider = ({
services: { services: {
'AST-undo-startSketchOn': async ({ sketchDetails }) => { 'AST-undo-startSketchOn': async ({ sketchDetails }) => {
if (!sketchDetails) return if (!sketchDetails) return
const newAst: Program = JSON.parse(JSON.stringify(kclManager.ast)) if (kclManager.ast.body.length) {
const varDecIndex = sketchDetails.sketchPathToNode[1][0] // this assumes no changes have been made to the sketch besides what we did when entering the sketch
// remove body item at varDecIndex // i.e. doesn't account for user's adding code themselves, maybe we need store a flag userEditedSinceSketchMode?
newAst.body = newAst.body.filter((_, i) => i !== varDecIndex) const newAst: Program = JSON.parse(JSON.stringify(kclManager.ast))
await kclManager.executeAstMock(newAst) const varDecIndex = sketchDetails.sketchPathToNode[1][0]
// remove body item at varDecIndex
newAst.body = newAst.body.filter((_, i) => i !== varDecIndex)
await kclManager.executeAstMock(newAst)
}
sceneInfra.setCallbacks({ sceneInfra.setCallbacks({
onClick: () => {}, onClick: () => {},
onDrag: () => {}, onDrag: () => {},