Skip to content

AI-native design editor. Open-source Figma alternative.

test(keyboard): add unit tests for opacity buffer logic

sadkodevd58cf0b9e93e
diff --git a/packages/core/src/editor/index.ts b/packages/core/src/editor/index.ts
index 6f266d20c..d7d955d74 100644
--- a/packages/core/src/editor/index.ts
+++ b/packages/core/src/editor/index.ts
@@ -1,6 +1,7 @@
export { createDefaultEditorState, createEditor } from './create'
export type { Editor } from './create'
export { createTextActions } from './text'
+export { opacityFromBuffer } from './nodes'
export { EDITOR_TOOLS, TOOL_SHORTCUTS } from './tool-registry'
export type { EditorToolDef } from './tool-registry'
export type {
diff --git a/packages/core/src/editor/nodes.ts b/packages/core/src/editor/nodes.ts
index b4042272b..f563ba5c5 100644
--- a/packages/core/src/editor/nodes.ts
+++ b/packages/core/src/editor/nodes.ts
@@ -8,6 +8,14 @@ import { textAutoResizeChanges } from './text/auto-resize'
import type { EditorContext } from './types'
import { createVariableBindingActions } from './variable-bindings'
+export function opacityFromBuffer(buffer: string): number {
+ if (buffer === '0') return 1
+ const n = Number.parseInt(buffer, 10)
+ if (Number.isNaN(n)) return 1
+ const percent = buffer.length === 1 ? n * 10 : n
+ return Math.min(100, Math.max(0, percent)) / 100
+}
+
export function createNodeActions(ctx: EditorContext) {
const layoutModeActions = createLayoutModeActions(ctx)
const nudgeActions = createNudgeActions(ctx)
diff --git a/src/app/shell/keyboard/actions.ts b/src/app/shell/keyboard/actions.ts
index 2d8823afa..b0ac6eb02 100644
--- a/src/app/shell/keyboard/actions.ts
+++ b/src/app/shell/keyboard/actions.ts
@@ -1,5 +1,6 @@
import type { Ref } from 'vue'
+import { opacityFromBuffer } from '@open-pencil/core/editor'
import type { useEditorCommands, useViewportKind } from '@open-pencil/vue'
import type { EditorStore } from '@/app/editor/active-store'
@@ -107,13 +108,7 @@ export function createKeyboardActions({
if (store.state.selectedIds.size === 0) return
opacityBuffer += digit
if (opacityBuffer.length > 3) opacityBuffer = opacityBuffer.slice(-3)
- const n = Number.parseInt(opacityBuffer, 10)
- let percent: number
- if (opacityBuffer === '0') percent = 100
- else if (opacityBuffer.length === 1) percent = n * 10
- else percent = n
- const clamped = Math.min(100, Math.max(0, percent))
- setOpacityTarget(clamped / 100)
+ setOpacityTarget(opacityFromBuffer(opacityBuffer))
runCommand('selection.setOpacity')
clearTimeout(opacityResetTimer)
opacityResetTimer = setTimeout(() => {
diff --git a/tests/engine/editor/opacity.test.ts b/tests/engine/editor/opacity.test.ts
index 495249af6..a4b346e63 100644
--- a/tests/engine/editor/opacity.test.ts
+++ b/tests/engine/editor/opacity.test.ts
@@ -1,6 +1,6 @@
import { describe, test, expect } from 'bun:test'
-import { createEditor } from '@open-pencil/core/editor'
+import { createEditor, opacityFromBuffer } from '@open-pencil/core/editor'
import { getNodeOrThrow } from '#tests/helpers/assert'
@@ -98,3 +98,32 @@ describe('editor.setOpacity', () => {
expect(editor.undo.canUndo).toBe(false)
})
})
+
+describe('opacityFromBuffer', () => {
+ test('single 0 returns 100%', () => {
+ expect(opacityFromBuffer('0')).toBe(1)
+ })
+
+ test('single digit returns decena', () => {
+ expect(opacityFromBuffer('5')).toBe(0.5)
+ expect(opacityFromBuffer('2')).toBe(0.2)
+ expect(opacityFromBuffer('9')).toBe(0.9)
+ })
+
+ test('two digits returns literal percent', () => {
+ expect(opacityFromBuffer('28')).toBe(0.28)
+ expect(opacityFromBuffer('35')).toBe(0.35)
+ expect(opacityFromBuffer('00')).toBe(0)
+ expect(opacityFromBuffer('05')).toBe(0.05)
+ })
+
+ test('three digits clamps to 100%', () => {
+ expect(opacityFromBuffer('100')).toBe(1)
+ expect(opacityFromBuffer('150')).toBe(1)
+ })
+
+ test('invalid buffer returns 100%', () => {
+ expect(opacityFromBuffer('')).toBe(1)
+ expect(opacityFromBuffer('abc')).toBe(1)
+ })
+})