Files
modeling-app/src/lib/utils.test.ts

1310 lines
40 KiB
TypeScript
Raw Normal View History

import {
isOverlap,
roundOff,
simulateOnMouseDragMatch,
onDragNumberCalculation,
hasLeadingZero,
hasDigitsLeftOfDecimal,
isClockwise,
} from './utils'
import { SourceRange, topLevelRange } from '../lang/wasm'
2022-11-26 08:34:23 +11:00
describe('testing isOverlapping', () => {
Rust artifact graph (#5068) * Start porting artifact graph creation to Rust * Add most of artifact graph creation * Add handling loft command from recent PR * Refactor artifact merge code so that it errors when a new artifact type is added * Add sweep subtype * Finish implementation of build artifact graph * Fix wasm.ts to use new combined generated ts-rs file * Fix Rust lints * Fix lints * Fix up replacement code * Add artifact graph to WASM outcome * Add artifact graph to simulation test output * Add new artifact graph output snapshots * Fix wall field and reduce unreachable code * Change field order for subtype * Change subtype to be determined from the request, like the TS * Fix plane sweep_id * Condense code * Change ID types to be properly optional * Change to favor the new ID, the same as TS * Fix to make error impossible * Rename artifact type tag values to match TS * Fix name of field on Cap * Update outputs * Change to use Rust source range * Update output snapshots * Add conversion to mermaid mind map and add to snapshot tests * Add new mermaid mind map output * Add flowchart * Remove raw artifact graph from tests * Remove JSON artifact graph output * Update output file with header * Update output after adding flowchart * Fix flowchart to not have duplicate edges, one in each direction * Fix not not output duplicate edges in flowcharts * Change flowchart edge style to be more obvious when a direction is missing * Update output after deduplication of edges * Fix not not skip sketch-on-face artifacts * Add docs * Fix edge iteration order to be stable * Update output after fixing order * Port TS artifactGraph.test.ts tests to simulation tests * Add grouping segments and solid2ds with their path * Update output flowcharts since grouping paths * Remove TS artifactGraph tests * Remove unused d3 dependencies * Fix to track loft ID on paths * Add command ID to error messages * Move artifact graph test code to a separate file since it's a large file * Reduce function visibility * Remove TS artifact graph code * Fix spelling error with serde * Add TODO for edge cut consumed ID * Add comment about mermaid edge rank * Fix mermaid flowchart edge cuts to appear as children of their edges * Update output since fixing flowchart order * Fix to always build the artifact graph even when there's a KCL error * Add artifact graph to error output * Change optional ID merge to match TS * Remove redundant SourceRange definition * Remove Rust-flavored default source range function * Add helper for source range creation * Update doc comment for the website * Update docs after doc comment change * Fix to save engine responses in execution cache * Remove unused import * Fix to not call WASM function before beforeAll callback is run * Remove more unused imports
2025-01-17 14:34:36 -05:00
testBothOrders(topLevelRange(0, 3), topLevelRange(3, 10))
testBothOrders(topLevelRange(0, 5), topLevelRange(3, 4))
testBothOrders(topLevelRange(0, 5), topLevelRange(5, 10))
testBothOrders(topLevelRange(0, 5), topLevelRange(6, 10), false)
testBothOrders(topLevelRange(0, 5), topLevelRange(-1, 1))
testBothOrders(topLevelRange(0, 5), topLevelRange(-1, 0))
testBothOrders(topLevelRange(0, 5), topLevelRange(-2, -1), false)
2022-11-26 08:34:23 +11:00
})
function testBothOrders(a: SourceRange, b: SourceRange, result = true) {
it(`test is overlapping ${a} ${b}`, () => {
expect(isOverlap(a, b)).toBe(result)
expect(isOverlap(b, a)).toBe(result)
2022-11-26 08:34:23 +11:00
})
}
describe('testing roundOff', () => {
it('defaults to 2 decimal places', () => {
expect(roundOff(1.23456789)).toBe(1.23)
})
it('rounds off to 3 decimal places', () => {
expect(roundOff(1.23456789, 3)).toBe(1.235)
})
it('works with whole numbers', () => {
expect(roundOff(1.23456789, 0)).toBe(1)
})
it('rounds up ok', () => {
expect(roundOff(1.273456789, 1)).toBe(1.3)
})
})
describe('testing hasLeadingZero', () => {
it('.1 should have no leading zero', () => {
const actual = hasLeadingZero('.1')
const expected = false
expect(actual).toBe(expected)
})
it('1.1 should have no leading zero', () => {
const actual = hasLeadingZero('1.1')
const expected = false
expect(actual).toBe(expected)
})
it('0.1 should have leading zero', () => {
const actual = hasLeadingZero('0.1')
const expected = true
expect(actual).toBe(expected)
})
it('10 should have no leading zero', () => {
const actual = hasLeadingZero('10')
const expected = false
expect(actual).toBe(expected)
})
it('0.375 should have leading zero', () => {
const actual = hasLeadingZero('0.375')
const expected = true
expect(actual).toBe(expected)
})
it('-0.375 should have leading zero', () => {
const actual = hasLeadingZero('-0.375')
const expected = true
expect(actual).toBe(expected)
})
it('-0.0 should have leading zero', () => {
const actual = hasLeadingZero('-0.0')
const expected = true
expect(actual).toBe(expected)
})
it('0.0 should have leading zero', () => {
const actual = hasLeadingZero('0.0')
const expected = true
expect(actual).toBe(expected)
})
})
describe('testing hasDigitsLeftOfDecimal', () => {
it('0.25 should be whole', () => {
const actual = hasDigitsLeftOfDecimal('0.25')
const expected = true
expect(actual).toBe(expected)
})
})
describe('testing simulateOnMouseDragMatch', () => {
// positive numbers
it('works with 0.0', () => {
const actual = simulateOnMouseDragMatch('0.0')
const expected = ['0.0']
expect(actual).toStrictEqual(expected)
})
it('works with 0.1', () => {
const actual = simulateOnMouseDragMatch('0.1')
const expected = ['0.1']
expect(actual).toStrictEqual(expected)
})
it('works with 0.10', () => {
const actual = simulateOnMouseDragMatch('0.10')
const expected = ['0.10']
expect(actual).toStrictEqual(expected)
})
it('works with 0.100', () => {
const actual = simulateOnMouseDragMatch('0.100')
const expected = ['0.100']
expect(actual).toStrictEqual(expected)
})
it('works with 0.01', () => {
const actual = simulateOnMouseDragMatch('0.01')
const expected = ['0.01']
expect(actual).toStrictEqual(expected)
})
it('works with 0.001', () => {
const actual = simulateOnMouseDragMatch('0.001')
const expected = ['0.001']
expect(actual).toStrictEqual(expected)
})
it('works with 0.0001', () => {
const actual = simulateOnMouseDragMatch('0.0001')
const expected = ['0.0001']
expect(actual).toStrictEqual(expected)
})
it('works with .0', () => {
const actual = simulateOnMouseDragMatch('.0')
const expected = ['.0']
expect(actual).toStrictEqual(expected)
})
it('works with .1', () => {
const actual = simulateOnMouseDragMatch('.1')
const expected = ['.1']
expect(actual).toStrictEqual(expected)
})
it('works with .10', () => {
const actual = simulateOnMouseDragMatch('.10')
const expected = ['.10']
expect(actual).toStrictEqual(expected)
})
it('works with .100', () => {
const actual = simulateOnMouseDragMatch('.100')
const expected = ['.100']
expect(actual).toStrictEqual(expected)
})
it('works with .1000', () => {
const actual = simulateOnMouseDragMatch('.1000')
const expected = ['.1000']
expect(actual).toStrictEqual(expected)
})
it('works with .01', () => {
const actual = simulateOnMouseDragMatch('.01')
const expected = ['.01']
expect(actual).toStrictEqual(expected)
})
it('works with .001', () => {
const actual = simulateOnMouseDragMatch('.001')
const expected = ['.001']
expect(actual).toStrictEqual(expected)
})
it('works with .0001', () => {
const actual = simulateOnMouseDragMatch('.0001')
const expected = ['.0001']
expect(actual).toStrictEqual(expected)
})
it('works with 1.0', () => {
const actual = simulateOnMouseDragMatch('1.0')
const expected = ['1.0']
expect(actual).toStrictEqual(expected)
})
it('works with 10.0', () => {
const actual = simulateOnMouseDragMatch('10.0')
const expected = ['10.0']
expect(actual).toStrictEqual(expected)
})
it('works with 100.0', () => {
const actual = simulateOnMouseDragMatch('100.0')
const expected = ['100.0']
expect(actual).toStrictEqual(expected)
})
it('works with 1000.0', () => {
const actual = simulateOnMouseDragMatch('1000.0')
const expected = ['1000.0']
expect(actual).toStrictEqual(expected)
})
it('works with 1.1', () => {
const actual = simulateOnMouseDragMatch('1.1')
const expected = ['1.1']
expect(actual).toStrictEqual(expected)
})
it('works with 10.01', () => {
const actual = simulateOnMouseDragMatch('10.01')
const expected = ['10.01']
expect(actual).toStrictEqual(expected)
})
it('works with 1', () => {
const actual = simulateOnMouseDragMatch('1')
const expected = ['1']
expect(actual).toStrictEqual(expected)
})
it('works with 10', () => {
const actual = simulateOnMouseDragMatch('10')
const expected = ['10']
expect(actual).toStrictEqual(expected)
})
it('works with 100', () => {
const actual = simulateOnMouseDragMatch('100')
const expected = ['100']
expect(actual).toStrictEqual(expected)
})
it('works with 1000', () => {
const actual = simulateOnMouseDragMatch('1000')
const expected = ['1000']
expect(actual).toStrictEqual(expected)
})
// negative numbers
it('works with -0.0', () => {
const actual = simulateOnMouseDragMatch('-0.0')
const expected = ['-0.0']
expect(actual).toStrictEqual(expected)
})
it('works with -0.1', () => {
const actual = simulateOnMouseDragMatch('-0.1')
const expected = ['-0.1']
expect(actual).toStrictEqual(expected)
})
it('works with -0.10', () => {
const actual = simulateOnMouseDragMatch('-0.10')
const expected = ['-0.10']
expect(actual).toStrictEqual(expected)
})
it('works with -0.100', () => {
const actual = simulateOnMouseDragMatch('-0.100')
const expected = ['-0.100']
expect(actual).toStrictEqual(expected)
})
it('works with -0.01', () => {
const actual = simulateOnMouseDragMatch('-0.01')
const expected = ['-0.01']
expect(actual).toStrictEqual(expected)
})
it('works with -0.001', () => {
const actual = simulateOnMouseDragMatch('-0.001')
const expected = ['-0.001']
expect(actual).toStrictEqual(expected)
})
it('works with -0.0001', () => {
const actual = simulateOnMouseDragMatch('-0.0001')
const expected = ['-0.0001']
expect(actual).toStrictEqual(expected)
})
it('works with -.0', () => {
const actual = simulateOnMouseDragMatch('-.0')
const expected = ['-.0']
expect(actual).toStrictEqual(expected)
})
it('works with -.1', () => {
const actual = simulateOnMouseDragMatch('-.1')
const expected = ['-.1']
expect(actual).toStrictEqual(expected)
})
it('works with -.10', () => {
const actual = simulateOnMouseDragMatch('-.10')
const expected = ['-.10']
expect(actual).toStrictEqual(expected)
})
it('works with -.100', () => {
const actual = simulateOnMouseDragMatch('-.100')
const expected = ['-.100']
expect(actual).toStrictEqual(expected)
})
it('works with -.1000', () => {
const actual = simulateOnMouseDragMatch('-.1000')
const expected = ['-.1000']
expect(actual).toStrictEqual(expected)
})
it('works with -.01', () => {
const actual = simulateOnMouseDragMatch('-.01')
const expected = ['-.01']
expect(actual).toStrictEqual(expected)
})
it('works with -.001', () => {
const actual = simulateOnMouseDragMatch('-.001')
const expected = ['-.001']
expect(actual).toStrictEqual(expected)
})
it('works with -.0001', () => {
const actual = simulateOnMouseDragMatch('-.0001')
const expected = ['-.0001']
expect(actual).toStrictEqual(expected)
})
it('works with -1.0', () => {
const actual = simulateOnMouseDragMatch('-1.0')
const expected = ['-1.0']
expect(actual).toStrictEqual(expected)
})
it('works with -10.0', () => {
const actual = simulateOnMouseDragMatch('-10.0')
const expected = ['-10.0']
expect(actual).toStrictEqual(expected)
})
it('works with -100.0', () => {
const actual = simulateOnMouseDragMatch('-100.0')
const expected = ['-100.0']
expect(actual).toStrictEqual(expected)
})
it('works with -1000.0', () => {
const actual = simulateOnMouseDragMatch('-1000.0')
const expected = ['-1000.0']
expect(actual).toStrictEqual(expected)
})
it('works with -1.1', () => {
const actual = simulateOnMouseDragMatch('-1.1')
const expected = ['-1.1']
expect(actual).toStrictEqual(expected)
})
it('works with -10.01', () => {
const actual = simulateOnMouseDragMatch('-10.01')
const expected = ['-10.01']
expect(actual).toStrictEqual(expected)
})
it('works with -1', () => {
const actual = simulateOnMouseDragMatch('-1')
const expected = ['-1']
expect(actual).toStrictEqual(expected)
})
it('works with -10', () => {
const actual = simulateOnMouseDragMatch('-10')
const expected = ['-10']
expect(actual).toStrictEqual(expected)
})
it('works with -100', () => {
const actual = simulateOnMouseDragMatch('-100')
const expected = ['-100']
expect(actual).toStrictEqual(expected)
})
it('works with -1000', () => {
const actual = simulateOnMouseDragMatch('-1000')
const expected = ['-1000']
expect(actual).toStrictEqual(expected)
})
it('works with =-500', () => {
const actual = simulateOnMouseDragMatch('=-500')
const expected = ['-500']
expect(actual).toStrictEqual(expected)
})
it('works with =500', () => {
const actual = simulateOnMouseDragMatch('=500')
const expected = ['500']
expect(actual).toStrictEqual(expected)
})
it('works with = 500', () => {
const actual = simulateOnMouseDragMatch('= 500')
const expected = ['500']
expect(actual).toStrictEqual(expected)
})
})
describe('testing onDragNumberCalculation', () => {
// Need to simulate the MouseEvent object with limited values
const positiveHundredsDecimalEvent = new MouseEvent('mousemove', {
shiftKey: true,
metaKey: true,
movementX: 1,
})
const positiveTensDecimalEvent = new MouseEvent('mousemove', {
metaKey: true,
movementX: 1,
})
const positiveTensEvent = new MouseEvent('mousemove', {
shiftKey: true,
movementX: 1,
})
const positiveOnesEvent = new MouseEvent('mousemove', { movementX: 1 })
const negativeHundredsDecimalEvent = new MouseEvent('mousemove', {
shiftKey: true,
metaKey: true,
movementX: -1,
})
const negativeTensDecimalEvent = new MouseEvent('mousemove', {
metaKey: true,
movementX: -1,
})
const negativeTensEvent = new MouseEvent('mousemove', {
shiftKey: true,
movementX: -1,
})
const negativeOnesEvent = new MouseEvent('mousemove', { movementX: -1 })
describe('positive direction', () => {
describe('ones event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.0'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '2'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.5'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.66'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.375'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.1'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.0'
expect(actual).toBe(expected)
})
test('works with 1.0', () => {
const match = simulateOnMouseDragMatch('1.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '2.0'
expect(actual).toBe(expected)
})
test('works with 1.66', () => {
const match = simulateOnMouseDragMatch('1.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '2.66'
expect(actual).toBe(expected)
})
test('works with 0.0000001', () => {
const match = simulateOnMouseDragMatch('0.0000001')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.0000001'
expect(actual).toBe(expected)
})
test('works with 9', () => {
const match = simulateOnMouseDragMatch('9')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '10'
expect(actual).toBe(expected)
})
test('works with 99', () => {
const match = simulateOnMouseDragMatch('99')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '100'
expect(actual).toBe(expected)
})
test('works with .123', () => {
const match = simulateOnMouseDragMatch('.123')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveOnesEvent)
const expected = '1.123'
expect(actual).toBe(expected)
})
})
describe('tens event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.0'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '11'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.5'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.66'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.375'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.1'
expect(actual).toBe(expected)
})
test('works with 90', () => {
const match = simulateOnMouseDragMatch('90')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '100'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.0'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, positiveTensEvent)
const expected = '10.1'
expect(actual).toBe(expected)
})
})
describe('tens decimal event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '0.1'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '1.1'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '0.6'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '0.76'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '0.475'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '0.2'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '.2'
expect(actual).toBe(expected)
})
test('works with 10', () => {
const match = simulateOnMouseDragMatch('10')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '10.1'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '.1'
expect(actual).toBe(expected)
})
test('works with .01', () => {
const match = simulateOnMouseDragMatch('.01')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveTensDecimalEvent
)
const expected = '.11'
expect(actual).toBe(expected)
})
})
describe('hundreds decimal event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '0.01'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '1.01'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '0.51'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '0.67'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '0.385'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '0.11'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '.11'
expect(actual).toBe(expected)
})
test('works with .11', () => {
const match = simulateOnMouseDragMatch('.11')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '.12'
expect(actual).toBe(expected)
})
test('works with 10', () => {
const match = simulateOnMouseDragMatch('10')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '10.01'
expect(actual).toBe(expected)
})
test('works with 10.02', () => {
const match = simulateOnMouseDragMatch('10.02')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
positiveHundredsDecimalEvent
)
const expected = '10.03'
expect(actual).toBe(expected)
})
})
})
// NEGATIVE DIRECTION
describe('negative direction', () => {
describe('ones event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-1.0'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '0'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.5'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.34'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.625'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.9'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-1.0'
expect(actual).toBe(expected)
})
test('works with 1.0', () => {
const match = simulateOnMouseDragMatch('1.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '0.0'
expect(actual).toBe(expected)
})
test('works with 1.66', () => {
const match = simulateOnMouseDragMatch('1.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '0.66'
expect(actual).toBe(expected)
})
test('works with 0.0000001', () => {
const match = simulateOnMouseDragMatch('0.0000001')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.9999999'
expect(actual).toBe(expected)
})
test('works with 9', () => {
const match = simulateOnMouseDragMatch('9')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '8'
expect(actual).toBe(expected)
})
test('works with 100', () => {
const match = simulateOnMouseDragMatch('100')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '99'
expect(actual).toBe(expected)
})
test('works with .123', () => {
const match = simulateOnMouseDragMatch('.123')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeOnesEvent)
const expected = '-0.877'
expect(actual).toBe(expected)
})
})
describe('tens event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-10.0'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9.5'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9.34'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9.625'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9.9'
expect(actual).toBe(expected)
})
test('works with 90', () => {
const match = simulateOnMouseDragMatch('90')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '80'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-10.0'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(matchedText, negativeTensEvent)
const expected = '-9.9'
expect(actual).toBe(expected)
})
})
describe('tens decimal event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '-0.1'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '0.9'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '0.4'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '0.56'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '0.275'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '0.0'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '.0'
expect(actual).toBe(expected)
})
test('works with 10', () => {
const match = simulateOnMouseDragMatch('10')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '9.9'
expect(actual).toBe(expected)
})
test('works with .0', () => {
const match = simulateOnMouseDragMatch('.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '-.1'
expect(actual).toBe(expected)
})
test('works with .01', () => {
const match = simulateOnMouseDragMatch('.01')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeTensDecimalEvent
)
const expected = '-.09'
expect(actual).toBe(expected)
})
})
describe('hundreds decimal event', () => {
test('works with 0.0', () => {
const match = simulateOnMouseDragMatch('0.0')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '-0.01'
expect(actual).toBe(expected)
})
test('works with 1', () => {
const match = simulateOnMouseDragMatch('1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '0.99'
expect(actual).toBe(expected)
})
test('works with 0.5', () => {
const match = simulateOnMouseDragMatch('0.5')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '0.49'
expect(actual).toBe(expected)
})
test('works with 0.66', () => {
const match = simulateOnMouseDragMatch('0.66')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '0.65'
expect(actual).toBe(expected)
})
test('works with 0.375', () => {
const match = simulateOnMouseDragMatch('0.375')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '0.365'
expect(actual).toBe(expected)
})
test('works with 0.1', () => {
const match = simulateOnMouseDragMatch('0.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '0.09'
expect(actual).toBe(expected)
})
test('works with .1', () => {
const match = simulateOnMouseDragMatch('.1')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '.09'
expect(actual).toBe(expected)
})
test('works with .11', () => {
const match = simulateOnMouseDragMatch('.11')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '.1'
expect(actual).toBe(expected)
})
test('works with 10', () => {
const match = simulateOnMouseDragMatch('10')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '9.99'
expect(actual).toBe(expected)
})
test('works with 10.02', () => {
const match = simulateOnMouseDragMatch('10.02')
const matchedText = match ? match[0] : ''
const actual = onDragNumberCalculation(
matchedText,
negativeHundredsDecimalEvent
)
const expected = '10.01'
expect(actual).toBe(expected)
})
})
})
})
Add 3 point arc (#5722) * bare minimum * start of segment util added * remove redundant handle * some probably buggy handling of arc handles, can fix later * probably bug implementation of update args, but data flow through is mostly there can fix bugs after * fix update for arc * fix math for center handle * fix up length indicator * tweak math * stub out xState logic for arc * more progress on adding point and click, implemented more of sketchLineHelper for arc * small unrelated tweak * fix up draft arc bugs * fix arc last click * fix draft segment animation and add comment * add draft point snapping for arcs * add helper stuff to arc * clone arc point and click as base for arc-three-point * rust change for arc three point * can draw three point arc * make arcTo editable * can add new three point arc, so long as it continues existing profile * get overlays working * make snap to for continuing profile work for three point arcs * add draft animation * tangent issue fix * action rename * tmp test fix up * fix silly bug * fix couple problems causing tests to fail * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * fix up * add delet segment test for new segments * update docs * draft segments should look right * add test for dragging new segment handles * arc tools can be chained now * make three point arc can start a new profile (not only extend existing paths) * add test for equiping and unequiping the tool plus drawing with it * fix console noise * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * clean up * update rust/docs * put toolbar mode check into fixture * do thing for lee * use TEST_COLORSs * fix colors * don't await file write * remove commented code * remove unneeded template strings * power to **2 * remove magic numbers * more string templates * some odd bits of clean up * arc should be enable in dev * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * A snapshot a day keeps the bugs away! 📷🐛 (OS: namespace-profile-ubuntu-8-cores) * add new simulation test * fix test code from kwark migration * issues Frank found * fix deleting half complete ark * fix * small fix on dele index * tsc post main merge * fix up snaping to profile start * add cross hari for three point arc * block snapping if it's the only segment * add tests for canceling arcTo halfway through --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
2025-03-18 11:14:12 +11:00
describe('testing isClockwise', () => {
it('returns for counter clockwise points', () => {
// Points in clockwise order (rectangle)
const clockwisePoints: [number, number][] = [
[0, 0], // bottom-left
[10, 0], // bottom-right
[10, 10], // top-right
[0, 10], // top-left
]
expect(isClockwise(clockwisePoints)).toBe(false)
})
it('returns true for clockwise points', () => {
// Points in counter-clockwise order (rectangle)
const counterClockwisePoints: [number, number][] = [
[0, 0], // bottom-left
[0, 10], // top-left
[10, 10], // top-right
[10, 0], // bottom-right
]
expect(isClockwise(counterClockwisePoints)).toBe(true)
})
it('returns false for less than 3 points', () => {
expect(
isClockwise([
[0, 0],
[10, 10],
])
).toBe(false)
expect(isClockwise([[0, 0]])).toBe(false)
expect(isClockwise([])).toBe(false)
})
it('correctly identifies counter-clockwise triangle', () => {
const clockwiseTriangle: [number, number][] = [
[0, 0],
[10, 0],
[5, 10],
]
expect(isClockwise(clockwiseTriangle)).toBe(false)
})
it('correctly identifies clockwise triangle', () => {
const counterClockwiseTriangle: [number, number][] = [
[0, 0],
[5, 10],
[10, 0],
]
expect(isClockwise(counterClockwiseTriangle)).toBe(true)
})
})