Opening KCL sample now overwrites units (#3970)

* Implement basic unit overwriting, update tests

* fix eslint warning

* A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)

* Revert "A snapshot a day keeps the bugs away! 📷🐛 (OS: windows-latest)"

This reverts commit 2ecf012c25.

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
This commit is contained in:
Frank Noirot
2024-09-30 10:12:59 -04:00
committed by GitHub
parent 168f4fc545
commit 3949b6acf4
4 changed files with 83 additions and 26 deletions

View File

@ -4,6 +4,7 @@ import { bracket } from 'lib/exampleKcl'
import * as fsp from 'fs/promises' import * as fsp from 'fs/promises'
import { join } from 'path' import { join } from 'path'
import { FILE_EXT } from 'lib/constants' import { FILE_EXT } from 'lib/constants'
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
test.beforeEach(async ({ context, page }, testInfo) => { test.beforeEach(async ({ context, page }, testInfo) => {
await setup(context, page, testInfo) await setup(context, page, testInfo)
@ -15,8 +16,8 @@ test.afterEach(async ({ page }, testInfo) => {
test.describe('Testing in-app sample loading', () => { test.describe('Testing in-app sample loading', () => {
/** /**
* Note this test implicitly depends on the KCL sample "flange-with-patterns.kcl" * Note this test implicitly depends on the KCL sample "car-wheel.kcl",
* and its title. https://github.com/KittyCAD/kcl-samples/blob/main/flange-with-patterns/flange-with-patterns.kcl * its title, and its units settings. https://github.com/KittyCAD/kcl-samples/blob/main/car-wheel/car-wheel.kcl
*/ */
test('Web: should overwrite current code, cannot create new file', async ({ test('Web: should overwrite current code, cannot create new file', async ({
page, page,
@ -33,8 +34,8 @@ test.describe('Testing in-app sample loading', () => {
// Locators and constants // Locators and constants
const newSample = { const newSample = {
file: 'flange-with-patterns' + FILE_EXT, file: 'car-wheel' + FILE_EXT,
title: 'Flange', title: 'Car Wheel',
} }
const commandBarButton = page.getByRole('button', { name: 'Commands' }) const commandBarButton = page.getByRole('button', { name: 'Commands' })
const samplesCommandOption = page.getByRole('option', { const samplesCommandOption = page.getByRole('option', {
@ -51,9 +52,11 @@ test.describe('Testing in-app sample loading', () => {
page.getByRole('option', { page.getByRole('option', {
name, name,
}) })
const warningText = page.getByText('Overwrite current file?') const warningText = page.getByText('Overwrite current file and units?')
const confirmButton = page.getByRole('button', { name: 'Submit command' }) const confirmButton = page.getByRole('button', { name: 'Submit command' })
const codeLocator = page.locator('.cm-content') const codeLocator = page.locator('.cm-content')
const unitsToast = (unit: UnitLength_type) =>
page.getByText(`Set default unit to "${unit}" for this project`)
await test.step(`Precondition: check the initial code`, async () => { await test.step(`Precondition: check the initial code`, async () => {
await u.openKclCodePanel() await u.openKclCodePanel()
@ -71,12 +74,13 @@ test.describe('Testing in-app sample loading', () => {
await confirmButton.click() await confirmButton.click()
await expect(codeLocator).toContainText('// ' + newSample.title) await expect(codeLocator).toContainText('// ' + newSample.title)
await expect(unitsToast('in')).toBeVisible()
}) })
}) })
/** /**
* Note this test implicitly depends on the KCL samples: * Note this test implicitly depends on the KCL samples:
* "flange-with-patterns.kcl": https://github.com/KittyCAD/kcl-samples/blob/main/flange-with-patterns/flange-with-patterns.kcl * "car-wheel.kcl": https://github.com/KittyCAD/kcl-samples/blob/main/car-wheel/car-wheel.kcl
* "gear-rack.kcl": https://github.com/KittyCAD/kcl-samples/blob/main/gear-rack/gear-rack.kcl * "gear-rack.kcl": https://github.com/KittyCAD/kcl-samples/blob/main/gear-rack/gear-rack.kcl
*/ */
test( test(
@ -97,8 +101,8 @@ test.describe('Testing in-app sample loading', () => {
// Locators and constants // Locators and constants
const sampleOne = { const sampleOne = {
file: 'flange-with-patterns' + FILE_EXT, file: 'car-wheel' + FILE_EXT,
title: 'Flange', title: 'Car Wheel',
} }
const sampleTwo = { const sampleTwo = {
file: 'gear-rack' + FILE_EXT, file: 'gear-rack' + FILE_EXT,
@ -119,9 +123,11 @@ test.describe('Testing in-app sample loading', () => {
name: 'Overwrite', name: 'Overwrite',
}) })
const newFileWarning = page.getByText( const newFileWarning = page.getByText(
'Create a new file with the example code?' 'Create a new file, overwrite project units?'
)
const overwriteWarning = page.getByText(
'Overwrite current file and units?'
) )
const overwriteWarning = page.getByText('Overwrite current file?')
const confirmButton = page.getByRole('button', { name: 'Submit command' }) const confirmButton = page.getByRole('button', { name: 'Submit command' })
const projectMenuButton = page.getByTestId('project-sidebar-toggle') const projectMenuButton = page.getByTestId('project-sidebar-toggle')
const newlyCreatedFile = (name: string) => const newlyCreatedFile = (name: string) =>
@ -129,6 +135,8 @@ test.describe('Testing in-app sample loading', () => {
has: page.getByRole('button', { name }), has: page.getByRole('button', { name }),
}) })
const codeLocator = page.locator('.cm-content') const codeLocator = page.locator('.cm-content')
const unitsToast = (unit: UnitLength_type) =>
page.getByText(`Set default unit to "${unit}" for this project`)
await test.step(`Test setup`, async () => { await test.step(`Test setup`, async () => {
await page.setViewportSize({ width: 1200, height: 500 }) await page.setViewportSize({ width: 1200, height: 500 })
@ -158,6 +166,7 @@ test.describe('Testing in-app sample loading', () => {
await expect(codeLocator).toContainText('// ' + sampleOne.title) await expect(codeLocator).toContainText('// ' + sampleOne.title)
await expect(newlyCreatedFile(sampleOne.file)).toBeVisible() await expect(newlyCreatedFile(sampleOne.file)).toBeVisible()
await expect(projectMenuButton).toContainText(sampleOne.file) await expect(projectMenuButton).toContainText(sampleOne.file)
await expect(unitsToast('in')).toBeVisible()
}) })
await test.step(`Now overwrite the current file`, async () => { await test.step(`Now overwrite the current file`, async () => {
@ -187,6 +196,7 @@ test.describe('Testing in-app sample loading', () => {
await expect(newlyCreatedFile(sampleOne.file)).toBeVisible() await expect(newlyCreatedFile(sampleOne.file)).toBeVisible()
await expect(newlyCreatedFile(sampleTwo.file)).not.toBeVisible() await expect(newlyCreatedFile(sampleTwo.file)).not.toBeVisible()
await expect(projectMenuButton).toContainText(sampleOne.file) await expect(projectMenuButton).toContainText(sampleOne.file)
await expect(unitsToast('mm')).toBeVisible()
}) })
await electronApp.close() await electronApp.close()

View File

@ -4,8 +4,8 @@ interface CommandBarOverwriteWarningProps {
} }
export function CommandBarOverwriteWarning({ export function CommandBarOverwriteWarning({
heading = 'Overwrite current file?', heading = 'Overwrite current file and units?',
message = 'This will permanently replace the current code in the editor.', message = 'This will permanently replace the current code in the editor, and overwrite your current units.',
}: CommandBarOverwriteWarningProps) { }: CommandBarOverwriteWarningProps) {
return ( return (
<> <>

View File

@ -28,6 +28,7 @@ import {
getKclSamplesManifest, getKclSamplesManifest,
KclSamplesManifestItem, KclSamplesManifestItem,
} from 'lib/getKclSamplesManifest' } from 'lib/getKclSamplesManifest'
import { useSettingsAuthContext } from 'hooks/useSettingsAuthContext'
type MachineContext<T extends AnyStateMachine> = { type MachineContext<T extends AnyStateMachine> = {
state: StateFrom<T> state: StateFrom<T>
@ -46,6 +47,7 @@ export const FileMachineProvider = ({
}) => { }) => {
const navigate = useNavigate() const navigate = useNavigate()
const { commandBarSend } = useCommandsContext() const { commandBarSend } = useCommandsContext()
const { settings } = useSettingsAuthContext()
const { project, file } = useRouteLoaderData(PATHS.FILE) as IndexLoaderData const { project, file } = useRouteLoaderData(PATHS.FILE) as IndexLoaderData
const [kclSamples, setKclSamples] = React.useState<KclSamplesManifestItem[]>( const [kclSamples, setKclSamples] = React.useState<KclSamplesManifestItem[]>(
[] []
@ -306,6 +308,18 @@ export const FileMachineProvider = ({
}, },
}) })
} }
// Either way, we want to overwrite the defaultUnit project setting
// with the sample's setting.
if (data.sampleUnits) {
settings.send({
type: 'set.modeling.defaultUnit',
data: {
level: 'project',
value: data.sampleUnits,
},
})
}
}, },
kclSamples.map((sample) => ({ kclSamples.map((sample) => ({
value: sample.file, value: sample.file,

View File

@ -2,11 +2,16 @@ import { CommandBarOverwriteWarning } from 'components/CommandBarOverwriteWarnin
import { Command, CommandArgumentOption } from './commandTypes' import { Command, CommandArgumentOption } from './commandTypes'
import { kclManager } from './singletons' import { kclManager } from './singletons'
import { isDesktop } from './isDesktop' import { isDesktop } from './isDesktop'
import { FILE_EXT } from './constants' import { FILE_EXT, PROJECT_SETTINGS_FILE_NAME } from './constants'
import { UnitLength_type } from '@kittycad/lib/dist/types/src/models'
import { parseProjectSettings } from 'lang/wasm'
import { err } from './trap'
import { projectConfigurationToSettingsPayload } from './settings/settingsUtils'
interface OnSubmitProps { interface OnSubmitProps {
sampleName: string sampleName: string
code: string code: string
sampleUnits?: UnitLength_type
method: 'overwrite' | 'newFile' method: 'overwrite' | 'newFile'
} }
@ -34,7 +39,10 @@ export function kclCommands(
icon: 'code', icon: 'code',
reviewMessage: ({ argumentsToSubmit }) => reviewMessage: ({ argumentsToSubmit }) =>
argumentsToSubmit.method === 'newFile' argumentsToSubmit.method === 'newFile'
? 'Create a new file with the example code?' ? CommandBarOverwriteWarning({
heading: 'Create a new file, overwrite project units?',
message: `This will add the sample as a new file to your project, and replace your current project units with the sample's units.`,
})
: CommandBarOverwriteWarning({}), : CommandBarOverwriteWarning({}),
groupId: 'code', groupId: 'code',
onSubmit(data) { onSubmit(data) {
@ -44,20 +52,45 @@ export function kclCommands(
const sampleCodeUrl = `https://raw.githubusercontent.com/KittyCAD/kcl-samples/main/${encodeURIComponent( const sampleCodeUrl = `https://raw.githubusercontent.com/KittyCAD/kcl-samples/main/${encodeURIComponent(
data.sample.replace(FILE_EXT, '') data.sample.replace(FILE_EXT, '')
)}/${encodeURIComponent(data.sample)}` )}/${encodeURIComponent(data.sample)}`
fetch(sampleCodeUrl) const sampleSettingsFileUrl = `https://raw.githubusercontent.com/KittyCAD/kcl-samples/main/${encodeURIComponent(
.then(async (response) => { data.sample.replace(FILE_EXT, '')
if (!response.ok) { )}/${PROJECT_SETTINGS_FILE_NAME}`
console.error('Failed to fetch sample code:', response.statusText)
return Promise.all([fetch(sampleCodeUrl), fetch(sampleSettingsFileUrl)])
.then(
async ([
codeResponse,
settingsResponse,
]): Promise<OnSubmitProps> => {
if (!(codeResponse.ok && settingsResponse.ok)) {
console.error(
'Failed to fetch sample code:',
codeResponse.statusText
)
return Promise.reject(new Error('Failed to fetch sample code'))
}
const code = await codeResponse.text()
const parsedProjectSettings = parseProjectSettings(
await settingsResponse.text()
)
let projectSettingsPayload: ReturnType<
typeof projectConfigurationToSettingsPayload
> = {}
if (!err(parsedProjectSettings)) {
projectSettingsPayload = projectConfigurationToSettingsPayload(
parsedProjectSettings
)
} }
const code = await response.text()
return { return {
sampleName: data.sample, sampleName: data.sample,
code, code,
method: data.method, method: data.method,
sampleUnits:
projectSettingsPayload.modeling?.defaultUnit || 'mm',
} }
}) }
)
.then((props) => { .then((props) => {
if (props?.code) { if (props?.code) {
onSubmit(props).catch(reportError) onSubmit(props).catch(reportError)