@m3ui-vue/m3ui-vue 1.2.1 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/API.md +2914 -348
- package/dist/MButton-CuNzpoxm.js +172 -0
- package/dist/MButton-CuNzpoxm.js.map +1 -0
- package/dist/MDialog-HxEL-m58.js +173 -0
- package/dist/MDialog-HxEL-m58.js.map +1 -0
- package/dist/MIconButton-DoNotQ9v.js +282 -0
- package/dist/MIconButton-DoNotQ9v.js.map +1 -0
- package/dist/MListItem-DnPtv-hY.js +824 -0
- package/dist/MListItem-DnPtv-hY.js.map +1 -0
- package/dist/MLoadingIndicator-BQMNX6Dx.js +115 -0
- package/dist/MLoadingIndicator-BQMNX6Dx.js.map +1 -0
- package/dist/MMenuItem-Bih0h4-n.js +604 -0
- package/dist/MMenuItem-Bih0h4-n.js.map +1 -0
- package/dist/MProgressBar-BCoruxA-.js +184 -0
- package/dist/MProgressBar-BCoruxA-.js.map +1 -0
- package/dist/MSeekBar-LURE1Ah7.js +435 -0
- package/dist/MSeekBar-LURE1Ah7.js.map +1 -0
- package/dist/MSegmentedButton-DL9JtJFX.js +65 -0
- package/dist/MSegmentedButton-DL9JtJFX.js.map +1 -0
- package/dist/MSlider-_J4IS2_z.js +691 -0
- package/dist/MSlider-_J4IS2_z.js.map +1 -0
- package/dist/MSpinner-BhkMYKOM.js +65 -0
- package/dist/MSpinner-BhkMYKOM.js.map +1 -0
- package/dist/MTextField-CkzwNBWS.js +296 -0
- package/dist/MTextField-CkzwNBWS.js.map +1 -0
- package/dist/MTooltip-B67ZDSGp.js +756 -0
- package/dist/MTooltip-B67ZDSGp.js.map +1 -0
- package/dist/_storageKeys-CYXsUBwK.js +18 -0
- package/dist/_storageKeys-CYXsUBwK.js.map +1 -0
- package/dist/_useEscapeStack-CjndQhqg.js +19 -0
- package/dist/_useEscapeStack-CjndQhqg.js.map +1 -0
- package/dist/api.json +1 -1
- package/dist/chart.js +1 -1
- package/dist/code-editor.js +1 -1
- package/dist/components/MAudioPlayer.vue.d.ts +115 -0
- package/dist/components/MBadge.vue.d.ts +1 -1
- package/dist/components/MBottomSheetScaffold.vue.d.ts +1 -1
- package/dist/components/MBox.vue.d.ts +1 -1
- package/dist/components/MButton.vue.d.ts +1 -1
- package/dist/components/MConfirmDialog.vue.d.ts +1 -1
- package/dist/components/MCopyButton.vue.d.ts +2 -2
- package/dist/components/MDataTable.vue.d.ts +2 -2
- package/dist/components/MDialog.vue.d.ts +2 -2
- package/dist/components/MDivider.vue.d.ts +1 -1
- package/dist/components/MDragHandle.vue.d.ts +1 -1
- package/dist/components/MDrawCanvas.vue.d.ts +148 -0
- package/dist/components/MDrawLayers.vue.d.ts +3 -0
- package/dist/components/MDrawToolbar.vue.d.ts +34 -0
- package/dist/components/MExportButton.vue.d.ts +1 -1
- package/dist/components/MFab.vue.d.ts +2 -2
- package/dist/components/MFabMenu.vue.d.ts +2 -2
- package/dist/components/MFixed.vue.d.ts +1 -1
- package/dist/components/MFlex.vue.d.ts +2 -2
- package/dist/components/MFloorPlan.vue.d.ts +160 -0
- package/dist/components/MFlow.vue.d.ts +305 -0
- package/dist/components/MFlowControls.vue.d.ts +33 -0
- package/dist/components/MFlowHandle.vue.d.ts +16 -0
- package/dist/components/MFlowMinimap.vue.d.ts +22 -0
- package/dist/components/MFlowNode.vue.d.ts +35 -0
- package/dist/components/MGantt.vue.d.ts +141 -0
- package/dist/components/MGanttControls.vue.d.ts +32 -0
- package/dist/components/MIconButton.vue.d.ts +3 -1
- package/dist/components/MImageCropper.vue.d.ts +110 -0
- package/dist/components/MInfiniteScroll.vue.d.ts +1 -1
- package/dist/components/MLightbox.vue.d.ts +2 -2
- package/dist/components/MList.vue.d.ts +1 -1
- package/dist/components/MLoadingOverlay.vue.d.ts +1 -1
- package/dist/components/MMap.vue.d.ts +1 -1
- package/dist/components/MMapControl.vue.d.ts +1 -1
- package/dist/components/MMapSearch.vue.d.ts +1 -1
- package/dist/components/MMaskField.vue.d.ts +12 -1
- package/dist/components/MMenu.vue.d.ts +1 -1
- package/dist/components/MNavigationDrawer.vue.d.ts +1 -1
- package/dist/components/MNumberField.vue.d.ts +11 -2
- package/dist/components/MPdfViewer.vue.d.ts +174 -0
- package/dist/components/MPullToRefresh.vue.d.ts +1 -1
- package/dist/components/MResourceTimeline.vue.d.ts +149 -0
- package/dist/components/MScheduler.vue.d.ts +3 -3
- package/dist/components/MScheme.vue.d.ts +1 -1
- package/dist/components/MSearchField.vue.d.ts +1 -1
- package/dist/components/MSeekBar.vue.d.ts +1 -1
- package/dist/components/MSimpleCarousel.vue.d.ts +1 -1
- package/dist/components/MSkeleton.vue.d.ts +1 -1
- package/dist/components/MSlider.vue.d.ts +4 -4
- package/dist/components/MSparkline.vue.d.ts +1 -1
- package/dist/components/MSplitter.vue.d.ts +1 -1
- package/dist/components/MStack.vue.d.ts +2 -2
- package/dist/components/MSwipeToDismiss.vue.d.ts +1 -1
- package/dist/components/MSwitch.vue.d.ts +1 -1
- package/dist/components/MTable.vue.d.ts +1 -1
- package/dist/components/MTagInput.vue.d.ts +12 -1
- package/dist/components/MTextField.vue.d.ts +13 -2
- package/dist/components/MToolbar.vue.d.ts +1 -1
- package/dist/components/MTree.vue.d.ts +2 -2
- package/dist/components/MVideoPlayer.vue.d.ts +253 -0
- package/dist/components/MVirtualTable.vue.d.ts +1 -1
- package/dist/components/MWindow.vue.d.ts +3 -3
- package/dist/components/_MFlowEdge.vue.d.ts +9 -0
- package/dist/components/_MFlowEdgeLabel.vue.d.ts +9 -0
- package/dist/components/_MFlowNodeShell.vue.d.ts +9 -0
- package/dist/components/_MPdfPage.vue.d.ts +76 -0
- package/dist/components/_MTimeBackground.vue.d.ts +14 -0
- package/dist/components/_MTimeHeader.vue.d.ts +20 -0
- package/dist/composables/_effectCanvas.d.ts +23 -0
- package/dist/composables/_flowPanel.d.ts +2 -0
- package/dist/composables/_flowSlot.d.ts +5 -0
- package/dist/composables/_ganttTime.d.ts +41 -0
- package/dist/composables/_storageKeys.d.ts +9 -0
- package/dist/composables/_useTimeAxis.d.ts +90 -0
- package/dist/composables/drawGeometry.d.ts +119 -0
- package/dist/composables/flowFormats.d.ts +79 -0
- package/dist/composables/flowLayout.d.ts +45 -0
- package/dist/composables/flowPaths.d.ts +53 -0
- package/dist/composables/ganttFormats.d.ts +52 -0
- package/dist/composables/ganttSchedule.d.ts +99 -0
- package/dist/composables/themeInitScript.d.ts +2 -0
- package/dist/composables/useConfetti.d.ts +19 -8
- package/dist/composables/useDrawing.d.ts +44 -0
- package/dist/composables/useFireworks.d.ts +37 -0
- package/dist/composables/useFlow.d.ts +266 -0
- package/dist/composables/useGantt.d.ts +87 -0
- package/dist/composables/useLocale.d.ts +163 -0
- package/dist/composables/useMediaPlayer.d.ts +101 -0
- package/dist/composables/usePdf.d.ts +119 -0
- package/dist/composables/useReactions.d.ts +32 -0
- package/dist/composables/useResourceTimeline.d.ts +70 -0
- package/dist/composables/viewportHints.d.ts +2 -0
- package/dist/directives/vSparkle.d.ts +27 -0
- package/dist/draw.d.ts +7 -0
- package/dist/draw.js +1700 -0
- package/dist/draw.js.map +1 -0
- package/dist/flow.d.ts +14 -0
- package/dist/flow.js +3542 -0
- package/dist/flow.js.map +1 -0
- package/dist/flowPaths-CUc6X373.js +161 -0
- package/dist/flowPaths-CUc6X373.js.map +1 -0
- package/dist/gantt.d.ts +12 -0
- package/dist/gantt.js +3575 -0
- package/dist/gantt.js.map +1 -0
- package/dist/index.d.ts +11 -1
- package/dist/locales/index.js +1148 -7
- package/dist/locales/index.js.map +1 -1
- package/dist/m3ui-vue.css +1 -1
- package/dist/m3ui.js +7822 -7809
- package/dist/m3ui.js.map +1 -1
- package/dist/map.js +593 -588
- package/dist/map.js.map +1 -1
- package/dist/media.d.ts +5 -0
- package/dist/media.js +1311 -0
- package/dist/media.js.map +1 -0
- package/dist/pdf.d.ts +4 -0
- package/dist/pdf.js +1871 -0
- package/dist/pdf.js.map +1 -0
- package/dist/plugin.d.ts +10 -0
- package/dist/rich-text-editor.js +28 -26
- package/dist/rich-text-editor.js.map +1 -1
- package/dist/styles.css +1 -1
- package/dist/{useLocale-JYxDDeXC.js → useLocale-BpGxplLV.js} +165 -2
- package/dist/useLocale-BpGxplLV.js.map +1 -0
- package/dist/{useThemeTokens-Bv-Uv8eX.js → useThemeTokens-DQQcaWbE.js} +41 -40
- package/dist/useThemeTokens-DQQcaWbE.js.map +1 -0
- package/package.json +29 -5
- package/src/__tests__/MAudioPlayer.test.ts +75 -0
- package/src/__tests__/MDrawCanvas.test.ts +110 -0
- package/src/__tests__/MFloorPlan.test.ts +97 -0
- package/src/__tests__/MFlow.test.ts +575 -0
- package/src/__tests__/MGantt.test.ts +130 -0
- package/src/__tests__/MImageCropper.test.ts +89 -0
- package/src/__tests__/MPdfViewer.test.ts +235 -0
- package/src/__tests__/MResourceTimeline.test.ts +102 -0
- package/src/__tests__/MSlider.test.ts +36 -0
- package/src/__tests__/MVideoPlayer.test.ts +201 -0
- package/src/__tests__/drawGeometry.test.ts +58 -0
- package/src/__tests__/fieldExpose.test.ts +30 -0
- package/src/__tests__/flowFormats.test.ts +185 -0
- package/src/__tests__/flowLayout.test.ts +113 -0
- package/src/__tests__/ganttFormats.test.ts +76 -0
- package/src/__tests__/ganttSchedule.test.ts +136 -0
- package/src/__tests__/pdfMock.ts +83 -0
- package/src/__tests__/plugin.test.ts +56 -1
- package/src/__tests__/ssr.test.ts +1 -1
- package/src/__tests__/themeInitScript.test.ts +10 -0
- package/src/__tests__/useConfetti.test.ts +33 -2
- package/src/__tests__/useFireworks.test.ts +104 -0
- package/src/__tests__/useMediaPlayer.test.ts +182 -0
- package/src/__tests__/usePdf.test.ts +102 -0
- package/src/__tests__/useReactions.test.ts +51 -0
- package/src/__tests__/vSparkle.test.ts +83 -0
- package/src/components/MAudioPlayer.vue +319 -0
- package/src/components/MDrawCanvas.vue +813 -0
- package/src/components/MDrawLayers.vue +114 -0
- package/src/components/MDrawToolbar.vue +171 -0
- package/src/components/MEmojiSelector.vue +3 -2
- package/src/components/MFloorPlan.vue +581 -0
- package/src/components/MFlow.vue +2049 -0
- package/src/components/MFlowControls.vue +110 -0
- package/src/components/MFlowHandle.vue +60 -0
- package/src/components/MFlowMinimap.vue +134 -0
- package/src/components/MFlowNode.vue +98 -0
- package/src/components/MGantt.vue +1609 -0
- package/src/components/MGanttControls.vue +77 -0
- package/src/components/MIconButton.vue +3 -1
- package/src/components/MImageCropper.vue +507 -0
- package/src/components/MMap.vue +3 -2
- package/src/components/MMaskField.vue +13 -0
- package/src/components/MNumberField.vue +13 -0
- package/src/components/MPdfViewer.vue +1269 -0
- package/src/components/MResourceTimeline.vue +1020 -0
- package/src/components/MSlider.vue +22 -4
- package/src/components/MTagInput.vue +13 -0
- package/src/components/MTextField.vue +24 -4
- package/src/components/MVideoPlayer.vue +839 -0
- package/src/components/_MFlowEdge.vue +100 -0
- package/src/components/_MFlowEdgeLabel.vue +56 -0
- package/src/components/_MFlowNodeShell.vue +111 -0
- package/src/components/_MPdfPage.vue +456 -0
- package/src/components/_MTimeBackground.vue +16 -0
- package/src/components/_MTimeHeader.vue +31 -0
- package/src/composables/_colorUtils.ts +4 -2
- package/src/composables/_effectCanvas.ts +109 -0
- package/src/composables/_flowPanel.ts +13 -0
- package/src/composables/_flowSlot.ts +6 -0
- package/src/composables/_ganttTime.ts +200 -0
- package/src/composables/_ssrState.ts +2 -2
- package/src/composables/_storageKeys.ts +28 -0
- package/src/composables/_useTimeAxis.ts +224 -0
- package/src/composables/drawGeometry.ts +405 -0
- package/src/composables/flowFormats.ts +916 -0
- package/src/composables/flowLayout.ts +297 -0
- package/src/composables/flowPaths.ts +239 -0
- package/src/composables/ganttFormats.ts +366 -0
- package/src/composables/ganttSchedule.ts +403 -0
- package/src/composables/themeInitScript.ts +5 -2
- package/src/composables/useColorPalette.ts +36 -8
- package/src/composables/useConfetti.ts +84 -107
- package/src/composables/useDrawing.ts +53 -0
- package/src/composables/useFireworks.ts +360 -0
- package/src/composables/useFlow.ts +250 -0
- package/src/composables/useGantt.ts +92 -0
- package/src/composables/useLocale.ts +328 -0
- package/src/composables/useMediaPlayer.ts +406 -0
- package/src/composables/usePdf.ts +456 -0
- package/src/composables/useReactions.ts +131 -0
- package/src/composables/useResourceTimeline.ts +76 -0
- package/src/composables/useTheme.ts +21 -4
- package/src/composables/viewportHints.ts +6 -5
- package/src/directives/vSparkle.ts +180 -0
- package/src/draw.ts +25 -0
- package/src/flow.ts +26 -0
- package/src/gantt.ts +12 -0
- package/src/index.ts +11 -1
- package/src/locales/de.ts +163 -0
- package/src/locales/es.ts +163 -0
- package/src/locales/fr.ts +163 -0
- package/src/locales/ja.ts +163 -0
- package/src/locales/ko.ts +163 -0
- package/src/locales/pt.ts +163 -0
- package/src/locales/zh.ts +163 -0
- package/src/media.ts +5 -0
- package/src/pdf.ts +15 -0
- package/src/plugin.ts +21 -5
- package/dist/MMenuItem-DI351kIf.js +0 -929
- package/dist/MMenuItem-DI351kIf.js.map +0 -1
- package/dist/MSlider-BnEy5ieZ.js +0 -1500
- package/dist/MSlider-BnEy5ieZ.js.map +0 -1
- package/dist/MTextField-BBmkaIbO.js +0 -633
- package/dist/MTextField-BBmkaIbO.js.map +0 -1
- package/dist/useLocale-JYxDDeXC.js.map +0 -1
- package/dist/useThemeTokens-Bv-Uv8eX.js.map +0 -1
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest'
|
|
2
|
+
import { addWorkingDays, criticalPath, rollupSummaries, scheduleTasks, workingDays, type GanttTask } from '../composables/ganttSchedule'
|
|
3
|
+
import { snapTime, unitsFor, parseDate, formatDate } from '../composables/_ganttTime'
|
|
4
|
+
|
|
5
|
+
const T = (id: string, start: string, end: string, extra: Partial<GanttTask> = {}): GanttTask => ({ id, name: id, start, end, ...extra })
|
|
6
|
+
const byId = (ts: GanttTask[]) => Object.fromEntries(ts.map((t) => [t.id, t]))
|
|
7
|
+
|
|
8
|
+
describe('gantt time', () => {
|
|
9
|
+
it('parses and formats local dates and datetimes', () => {
|
|
10
|
+
expect(formatDate(parseDate('2026-03-05'), true)).toBe('2026-03-05')
|
|
11
|
+
expect(formatDate(parseDate('2026-03-05T09:30'), false)).toBe('2026-03-05T09:30')
|
|
12
|
+
expect(formatDate(snapTime(parseDate('2026-03-05T09:38'), 15), false)).toBe('2026-03-05T09:45')
|
|
13
|
+
expect(formatDate(snapTime(parseDate('2026-03-05T13:00'), 1440), true)).toBe('2026-03-06')
|
|
14
|
+
})
|
|
15
|
+
it('picks header units that fit their labels', () => {
|
|
16
|
+
expect(unitsFor(1152)).toEqual({ bottom: 'hour', top: 'day', step: 1 })
|
|
17
|
+
expect(unitsFor(240)).toEqual({ bottom: 'hour', top: 'day', step: 6 })
|
|
18
|
+
expect(unitsFor(40)).toMatchObject({ bottom: 'day', top: 'month' })
|
|
19
|
+
expect(unitsFor(20)).toMatchObject({ bottom: 'week', top: 'month' })
|
|
20
|
+
expect(unitsFor(5)).toMatchObject({ bottom: 'month', top: 'year' })
|
|
21
|
+
expect(unitsFor(1.2).bottom).toBe('quarter')
|
|
22
|
+
expect(unitsFor(0.3).bottom).toBe('year')
|
|
23
|
+
})
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
describe('working days', () => {
|
|
27
|
+
it('counts and adds them around weekends and holidays', () => {
|
|
28
|
+
// 2026-03-06 is a Friday
|
|
29
|
+
expect(workingDays('2026-03-06', '2026-03-09')).toBe(2)
|
|
30
|
+
expect(addWorkingDays('2026-03-06', 1)).toBe('2026-03-09')
|
|
31
|
+
expect(addWorkingDays('2026-03-07', 0)).toBe('2026-03-09')
|
|
32
|
+
expect(addWorkingDays('2026-03-06', 1, { holidays: ['2026-03-09'] })).toBe('2026-03-10')
|
|
33
|
+
expect(addWorkingDays('2026-03-09', -1)).toBe('2026-03-06')
|
|
34
|
+
})
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('rollupSummaries', () => {
|
|
38
|
+
it('gives summaries the span of their tasks and weighted progress, nested', () => {
|
|
39
|
+
const tasks = [
|
|
40
|
+
T('p', '2026-01-01', '2026-01-01'),
|
|
41
|
+
T('s', '2026-01-01', '2026-01-01', { parentId: 'p' }),
|
|
42
|
+
T('a', '2026-01-05', '2026-01-06', { parentId: 's', progress: 100 }),
|
|
43
|
+
T('b', '2026-01-07', '2026-01-12', { parentId: 's', progress: 0 }),
|
|
44
|
+
T('c', '2026-01-20', '2026-01-21', { parentId: 'p', progress: 50 }),
|
|
45
|
+
]
|
|
46
|
+
const r = byId(rollupSummaries(tasks))
|
|
47
|
+
expect(r.s).toMatchObject({ start: '2026-01-05', end: '2026-01-12', progress: 25 })
|
|
48
|
+
expect(r.p).toMatchObject({ start: '2026-01-05', end: '2026-01-21' })
|
|
49
|
+
// Unchanged → same array
|
|
50
|
+
const once = rollupSummaries(tasks)
|
|
51
|
+
expect(rollupSummaries(once)).toBe(once)
|
|
52
|
+
})
|
|
53
|
+
})
|
|
54
|
+
|
|
55
|
+
describe('scheduleTasks', () => {
|
|
56
|
+
it('pushes successors past their predecessors, for every dependency type and lag', () => {
|
|
57
|
+
const tasks = [
|
|
58
|
+
T('a', '2026-01-05', '2026-01-09'),
|
|
59
|
+
T('fs', '2026-01-06', '2026-01-07', { dependencies: ['a'] }),
|
|
60
|
+
T('ss', '2026-01-01', '2026-01-02', { dependencies: [{ id: 'a', type: 'SS', lag: 2 }] }),
|
|
61
|
+
T('ff', '2026-01-01', '2026-01-03', { dependencies: [{ id: 'a', type: 'FF' }] }),
|
|
62
|
+
T('late', '2026-02-01', '2026-02-02', { dependencies: ['a'] }),
|
|
63
|
+
T('chain', '2026-01-01', '2026-01-01', { dependencies: ['fs'] }),
|
|
64
|
+
]
|
|
65
|
+
const r = byId(scheduleTasks(tasks))
|
|
66
|
+
expect(r.fs).toMatchObject({ start: '2026-01-10', end: '2026-01-11' })
|
|
67
|
+
expect(r.ss).toMatchObject({ start: '2026-01-07', end: '2026-01-08' })
|
|
68
|
+
expect(r.ff).toMatchObject({ start: '2026-01-07', end: '2026-01-09' })
|
|
69
|
+
// Push mode leaves a task that's already late enough alone; asap pulls it back
|
|
70
|
+
expect(r.late!.start).toBe('2026-02-01')
|
|
71
|
+
expect(byId(scheduleTasks(tasks, { mode: 'asap' })).late!.start).toBe('2026-01-10')
|
|
72
|
+
expect(r.chain!.start).toBe('2026-01-12')
|
|
73
|
+
expect(scheduleTasks([tasks[0]!])).toEqual([tasks[0]])
|
|
74
|
+
})
|
|
75
|
+
it('counts working days with a calendar; a link onto a summary moves each of its tasks', () => {
|
|
76
|
+
const tasks = [
|
|
77
|
+
// Friday
|
|
78
|
+
T('a', '2026-03-06', '2026-03-06'),
|
|
79
|
+
T('b', '2026-03-02', '2026-03-03', { dependencies: ['a'] }),
|
|
80
|
+
T('phase', '2026-01-01', '2026-01-01', { dependencies: ['a'] }),
|
|
81
|
+
T('p1', '2026-03-01', '2026-03-02', { parentId: 'phase' }),
|
|
82
|
+
]
|
|
83
|
+
const r = byId(scheduleTasks(tasks, { calendar: {} }))
|
|
84
|
+
// Two working days from Monday
|
|
85
|
+
expect(r.b).toMatchObject({ start: '2026-03-09', end: '2026-03-10' })
|
|
86
|
+
expect(r.p1!.start).toBe('2026-03-09')
|
|
87
|
+
expect(r.phase).toMatchObject({ start: '2026-03-09', end: r.p1!.end })
|
|
88
|
+
})
|
|
89
|
+
it('survives cycles', () => {
|
|
90
|
+
const tasks = [T('a', '2026-01-01', '2026-01-02', { dependencies: ['b'] }), T('b', '2026-01-01', '2026-01-02', { dependencies: ['a'] })]
|
|
91
|
+
expect(() => scheduleTasks(tasks)).not.toThrow()
|
|
92
|
+
})
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
describe('criticalPath', () => {
|
|
96
|
+
it('finds the chain that sets the end, with slack for the rest', () => {
|
|
97
|
+
const tasks = [
|
|
98
|
+
T('a', '2026-01-01', '2026-01-02'),
|
|
99
|
+
T('b', '2026-01-03', '2026-01-10', { dependencies: ['a'] }),
|
|
100
|
+
T('c', '2026-01-03', '2026-01-04', { dependencies: ['a'] }),
|
|
101
|
+
T('d', '2026-01-11', '2026-01-11', { dependencies: ['b', 'c'] }),
|
|
102
|
+
]
|
|
103
|
+
const cp = criticalPath(tasks)
|
|
104
|
+
expect(cp.tasks).toEqual(['a', 'b', 'd'])
|
|
105
|
+
expect(cp.dependencies.sort()).toEqual(['a>b', 'b>d'])
|
|
106
|
+
expect(cp.slack.c).toBe(6)
|
|
107
|
+
expect(cp.finish).toBe('2026-01-11')
|
|
108
|
+
})
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
describe('criticalPath with a calendar', () => {
|
|
112
|
+
it("doesn't count a weekend between two tasks as slack", () => {
|
|
113
|
+
// Friday, then Monday
|
|
114
|
+
const tasks = [T('a', '2026-03-06', '2026-03-06'), T('b', '2026-03-09', '2026-03-10', { dependencies: ['a'] })]
|
|
115
|
+
expect(criticalPath(tasks).tasks).toEqual(['b'])
|
|
116
|
+
const cp = criticalPath(tasks, { calendar: {} })
|
|
117
|
+
expect(cp.tasks).toEqual(['a', 'b'])
|
|
118
|
+
expect(cp.dependencies).toEqual(['a>b'])
|
|
119
|
+
})
|
|
120
|
+
})
|
|
121
|
+
|
|
122
|
+
describe('criticalPath across weekends', () => {
|
|
123
|
+
it('finds a chain whose links all cross weekends, the way Tempo plans it', () => {
|
|
124
|
+
// Mon–Fri, then Mon–Fri, then a Monday milestone; a side task with real slack
|
|
125
|
+
const tasks = [
|
|
126
|
+
T('a', '2026-03-02', '2026-03-06'),
|
|
127
|
+
T('b', '2026-03-09', '2026-03-13', { dependencies: ['a'] }),
|
|
128
|
+
T('side', '2026-03-09', '2026-03-10', { dependencies: ['a'] }),
|
|
129
|
+
{ id: 'm', name: 'm', start: '2026-03-16', type: 'milestone' as const, dependencies: ['b', 'side'] },
|
|
130
|
+
]
|
|
131
|
+
const cp = criticalPath(tasks, { calendar: {} })
|
|
132
|
+
expect(cp.tasks).toEqual(['a', 'b', 'm'])
|
|
133
|
+
expect(cp.slack.side).toBe(3)
|
|
134
|
+
expect(cp.dependencies.sort()).toEqual(['a>b', 'b>m'])
|
|
135
|
+
})
|
|
136
|
+
})
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
// A fake pdfjs-dist for tests (happy-dom can't run pdf.js): pages with sizes, text and links, an outline, metadata
|
|
2
|
+
// and an optional password, with the calls the library makes.
|
|
3
|
+
import { vi } from 'vitest'
|
|
4
|
+
|
|
5
|
+
export interface FakePage { width: number; height: number; text: { str: string; hasEOL?: boolean }[]; links?: { rect: number[]; url?: string; dest?: unknown }[] }
|
|
6
|
+
|
|
7
|
+
export const fakeDoc = {
|
|
8
|
+
pages: [] as FakePage[],
|
|
9
|
+
password: '' as string,
|
|
10
|
+
outline: [] as unknown[],
|
|
11
|
+
title: 'Fake document',
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
class Viewport {
|
|
15
|
+
constructor(public width: number, public height: number, public scale: number, public rotation: number) {}
|
|
16
|
+
convertToViewportPoint(x: number, y: number) { return [x * this.scale, this.height - y * this.scale] }
|
|
17
|
+
convertToPdfPoint(x: number, y: number) { return [x / this.scale, (this.height - y) / this.scale] }
|
|
18
|
+
get rawDims() { return { pageWidth: this.width / this.scale, pageHeight: this.height / this.scale } }
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function page(i: number) {
|
|
22
|
+
const p = fakeDoc.pages[i]!
|
|
23
|
+
return {
|
|
24
|
+
rotate: 0,
|
|
25
|
+
getViewport: ({ scale = 1, rotation = 0 }: { scale?: number; rotation?: number } = {}) => {
|
|
26
|
+
const swap = rotation % 180 !== 0
|
|
27
|
+
return new Viewport((swap ? p.height : p.width) * scale, (swap ? p.width : p.height) * scale, scale, rotation)
|
|
28
|
+
},
|
|
29
|
+
render: () => ({ promise: Promise.resolve(), cancel: () => {} }),
|
|
30
|
+
getTextContent: async () => ({ items: p.text.map((t) => ({ str: t.str, hasEOL: !!t.hasEOL, transform: [1, 0, 0, 1, 0, 0] })) }),
|
|
31
|
+
getAnnotations: async () => (p.links ?? []).map((l) => ({ subtype: 'Link', ...l })),
|
|
32
|
+
cleanup: () => {},
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
export const getDocument = vi.fn((_params: Record<string, unknown>) => {
|
|
37
|
+
const task: Record<string, unknown> = { destroy: vi.fn() }
|
|
38
|
+
task.promise = (async () => {
|
|
39
|
+
await Promise.resolve()
|
|
40
|
+
if (fakeDoc.password && _params.password !== fakeDoc.password) {
|
|
41
|
+
// Ask until the right one comes
|
|
42
|
+
let tried = _params.password as string | undefined
|
|
43
|
+
while (tried !== fakeDoc.password) {
|
|
44
|
+
tried = await new Promise<string>((resolve) => (task.onPassword as (cb: (p: string) => void, reason: number) => void)(resolve, tried === undefined ? 1 : 2))
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return {
|
|
48
|
+
numPages: fakeDoc.pages.length,
|
|
49
|
+
getPage: async (n: number) => page(n - 1),
|
|
50
|
+
getOutline: async () => fakeDoc.outline,
|
|
51
|
+
getMetadata: async () => ({ info: { Title: fakeDoc.title }, metadata: null }),
|
|
52
|
+
getDestination: async (name: string) => (name === 'ch2' ? [{ num: 1 }, { name: 'XYZ' }, 0, 700, null] : null),
|
|
53
|
+
getPageIndex: async (ref: { num: number }) => ref.num,
|
|
54
|
+
getData: async () => new Uint8Array([37, 80, 68, 70]),
|
|
55
|
+
destroy: vi.fn(),
|
|
56
|
+
}
|
|
57
|
+
})()
|
|
58
|
+
return task
|
|
59
|
+
})
|
|
60
|
+
|
|
61
|
+
export class TextLayer {
|
|
62
|
+
textDivs: HTMLElement[] = []
|
|
63
|
+
textContentItemsStr: string[] = []
|
|
64
|
+
constructor(private o: { textContentSource: { items: { str: string }[] }; container: HTMLElement }) {}
|
|
65
|
+
async render() {
|
|
66
|
+
for (const item of this.o.textContentSource.items) {
|
|
67
|
+
const span = document.createElement('span')
|
|
68
|
+
span.textContent = item.str
|
|
69
|
+
this.o.container.appendChild(span)
|
|
70
|
+
this.textDivs.push(span)
|
|
71
|
+
this.textContentItemsStr.push(item.str)
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
cancel() {}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
export class TouchManager {
|
|
78
|
+
constructor(_o: unknown) {}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export const GlobalWorkerOptions = { workerSrc: 'fake-worker.js' }
|
|
82
|
+
export const PasswordResponses = { NEED_PASSWORD: 1, INCORRECT_PASSWORD: 2 }
|
|
83
|
+
export const PDFDateString = { toDateObject: () => null }
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
import { describe, it, expect, beforeEach, afterEach } from 'vitest'
|
|
2
|
-
import { createApp, defineComponent, h } from 'vue'
|
|
2
|
+
import { createApp, defineComponent, h, nextTick } from 'vue'
|
|
3
3
|
import { createM3UI } from '../plugin'
|
|
4
4
|
import { useColorPalette, palettes } from '../composables/useColorPalette'
|
|
5
|
+
import { useTheme } from '../composables/useTheme'
|
|
6
|
+
import { recentColors } from '../composables/_colorUtils'
|
|
7
|
+
import { setStoragePrefix } from '../composables/_storageKeys'
|
|
5
8
|
|
|
6
9
|
describe('createM3UI', () => {
|
|
7
10
|
beforeEach(() => {
|
|
@@ -79,4 +82,56 @@ describe('createM3UI', () => {
|
|
|
79
82
|
expect(document.documentElement.getAttribute('data-palette')).toBe('brand')
|
|
80
83
|
})
|
|
81
84
|
})
|
|
85
|
+
|
|
86
|
+
describe('storagePrefix', () => {
|
|
87
|
+
afterEach(() => {
|
|
88
|
+
setStoragePrefix('')
|
|
89
|
+
localStorage.clear()
|
|
90
|
+
document.documentElement.classList.remove('dark')
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
function mountWith(options: Parameters<typeof createM3UI>[0]) {
|
|
94
|
+
let result!: { theme: ReturnType<typeof useTheme>; palette: ReturnType<typeof useColorPalette> }
|
|
95
|
+
const app = createApp(defineComponent({
|
|
96
|
+
setup() {
|
|
97
|
+
result = { theme: useTheme(), palette: useColorPalette() }
|
|
98
|
+
return () => h('div')
|
|
99
|
+
},
|
|
100
|
+
}))
|
|
101
|
+
app.use(createM3UI(options))
|
|
102
|
+
app.mount(document.createElement('div'))
|
|
103
|
+
return { app, ...result }
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
it("ignores another app's choices on the same origin and stores its own under the prefix", async () => {
|
|
107
|
+
localStorage.setItem('m3-theme', 'dark')
|
|
108
|
+
localStorage.setItem('m3-palette', 'teal')
|
|
109
|
+
localStorage.setItem('m3-color-recent', '["#ff0000"]')
|
|
110
|
+
const { app, theme, palette } = mountWith({ storagePrefix: 'shop', palette: 'red' })
|
|
111
|
+
expect(theme.theme.value).toBe('system')
|
|
112
|
+
expect(document.documentElement.classList.contains('dark')).toBe(false)
|
|
113
|
+
expect(palette.palette.value).toBe('red')
|
|
114
|
+
expect(document.documentElement.getAttribute('data-palette')).toBe('red')
|
|
115
|
+
expect(recentColors.value).toEqual([])
|
|
116
|
+
theme.theme.value = 'light'
|
|
117
|
+
await nextTick()
|
|
118
|
+
expect(localStorage.getItem('shop-m3-theme')).toBe('light')
|
|
119
|
+
expect(localStorage.getItem('shop-m3-palette')).toBe('red')
|
|
120
|
+
expect(localStorage.getItem('m3-theme')).toBe('dark')
|
|
121
|
+
expect(localStorage.getItem('m3-palette')).toBe('teal')
|
|
122
|
+
app.unmount()
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it("applies the app's own stored choices", () => {
|
|
126
|
+
localStorage.setItem('m3-palette', 'teal')
|
|
127
|
+
localStorage.setItem('admin-m3-theme', 'dark')
|
|
128
|
+
localStorage.setItem('admin-m3-palette', 'green')
|
|
129
|
+
const { app, theme, palette } = mountWith({ storagePrefix: 'admin', palette: 'red' })
|
|
130
|
+
expect(theme.theme.value).toBe('dark')
|
|
131
|
+
expect(document.documentElement.classList.contains('dark')).toBe(true)
|
|
132
|
+
expect(palette.palette.value).toBe('green')
|
|
133
|
+
expect(document.documentElement.getAttribute('data-palette')).toBe('green')
|
|
134
|
+
app.unmount()
|
|
135
|
+
})
|
|
136
|
+
})
|
|
82
137
|
})
|
|
@@ -179,7 +179,7 @@ describe('SSR', () => {
|
|
|
179
179
|
withDirectives(h('p', { id: 'skeleton' }, 'Ada'), [[d('skeleton'), true]]),
|
|
180
180
|
withDirectives(h('p', { id: 'ring' }), [[d('focus-ring'), { within: true }]]),
|
|
181
181
|
withDirectives(h('p', { id: 'layer' }), [[d('state-layer'), { dragged: true }]]),
|
|
182
|
-
withDirectives(h('p'), [[d('ripple')], [d('reveal')], [d('tooltip'), 'Hi'], [d('scroll-elevation')], [d('long-press'), () => {}], [d('swipe'), () => {}], [d('click-outside'), () => {}]]),
|
|
182
|
+
withDirectives(h('p'), [[d('ripple')], [d('reveal')], [d('tooltip'), 'Hi'], [d('scroll-elevation')], [d('long-press'), () => {}], [d('swipe'), () => {}], [d('click-outside'), () => {}], [d('sparkle')]]),
|
|
183
183
|
]),
|
|
184
184
|
})
|
|
185
185
|
app.use(lib.createM3UI())
|
|
@@ -50,6 +50,16 @@ describe('themeInitScript', () => {
|
|
|
50
50
|
expect(html.getAttribute('data-palette')).toBe('blue')
|
|
51
51
|
})
|
|
52
52
|
|
|
53
|
+
it('reads the keys under storagePrefix, not another app\'s', () => {
|
|
54
|
+
matchMedia({})
|
|
55
|
+
localStorage.setItem('m3-theme', 'dark')
|
|
56
|
+
localStorage.setItem('m3-palette', 'blue')
|
|
57
|
+
localStorage.setItem('shop-m3-palette', 'green')
|
|
58
|
+
run({ storagePrefix: 'shop' })
|
|
59
|
+
expect(html.classList.contains('dark')).toBe(false)
|
|
60
|
+
expect(html.getAttribute('data-palette')).toBe('green')
|
|
61
|
+
})
|
|
62
|
+
|
|
53
63
|
it('forces the palette with persistPalette: false', () => {
|
|
54
64
|
localStorage.setItem('m3-palette', 'blue')
|
|
55
65
|
run({ palette: 'red', persistPalette: false })
|
|
@@ -3,8 +3,12 @@ import { __resetConfetti, fireConfetti, useConfetti } from '../composables/useCo
|
|
|
3
3
|
|
|
4
4
|
// happy-dom has no 2D canvas: a fake one that records what's drawn
|
|
5
5
|
let fills: string[] = []
|
|
6
|
+
// Pieces drawn in the last full frame (each frame clears the canvas, then draws every piece)
|
|
7
|
+
let onScreen = 0
|
|
8
|
+
let drawing = 0
|
|
6
9
|
beforeEach(() => {
|
|
7
10
|
fills = []
|
|
11
|
+
onScreen = drawing = 0
|
|
8
12
|
vi.stubGlobal('Path2D', class { constructor(public d: string) {} })
|
|
9
13
|
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(function () {
|
|
10
14
|
const ctx = {
|
|
@@ -12,8 +16,8 @@ beforeEach(() => {
|
|
|
12
16
|
globalAlpha: 1,
|
|
13
17
|
setTransform: () => {},
|
|
14
18
|
translate: () => {},
|
|
15
|
-
clearRect: () => {},
|
|
16
|
-
fill: () => fills.push(ctx.fillStyle),
|
|
19
|
+
clearRect: () => { onScreen = drawing; drawing = 0 },
|
|
20
|
+
fill: () => { drawing++; fills.push(ctx.fillStyle) },
|
|
17
21
|
}
|
|
18
22
|
return ctx as unknown as CanvasRenderingContext2D
|
|
19
23
|
})
|
|
@@ -78,4 +82,31 @@ describe('useConfetti', () => {
|
|
|
78
82
|
expect(new Set(fills)).toEqual(new Set(['blue', 'green']))
|
|
79
83
|
button.remove()
|
|
80
84
|
})
|
|
85
|
+
|
|
86
|
+
it('with a duration keeps adding pieces (count per second) until it ends, then lets them fall', async () => {
|
|
87
|
+
fireConfetti(undefined, { from: 'top', count: 100, duration: 500, colors: ['red'] })
|
|
88
|
+
await frames(150)
|
|
89
|
+
const early = onScreen
|
|
90
|
+
await frames(250)
|
|
91
|
+
// Still raining: more pieces than before, about count per second so far
|
|
92
|
+
expect(onScreen).toBeGreaterThan(early)
|
|
93
|
+
expect(onScreen).toBeGreaterThan(20)
|
|
94
|
+
expect(onScreen).toBeLessThan(60)
|
|
95
|
+
await frames(300)
|
|
96
|
+
// Over: no new pieces (at most the 50 a second's half gives), the canvas stays while they fall
|
|
97
|
+
expect(onScreen).toBeLessThanOrEqual(51)
|
|
98
|
+
expect(canvas()).not.toBeNull()
|
|
99
|
+
})
|
|
100
|
+
|
|
101
|
+
it('stop() ends a rain early (and Infinity runs until then)', async () => {
|
|
102
|
+
const stop = fireConfetti(undefined, { from: 'top', count: 200, duration: Infinity, colors: ['red'] })
|
|
103
|
+
await frames(200)
|
|
104
|
+
stop()
|
|
105
|
+
await frames(30)
|
|
106
|
+
const atStop = onScreen
|
|
107
|
+
expect(atStop).toBeGreaterThan(0)
|
|
108
|
+
await frames(300)
|
|
109
|
+
expect(onScreen).toBeLessThanOrEqual(atStop)
|
|
110
|
+
expect(onScreen).toBeGreaterThan(0)
|
|
111
|
+
})
|
|
81
112
|
})
|
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, beforeEach, vi } from 'vitest'
|
|
2
|
+
import { __resetFireworks, launchFireworks, shapeOutline, useFireworks } from '../composables/useFireworks'
|
|
3
|
+
|
|
4
|
+
// happy-dom has no 2D canvas: a fake one that records the strokes
|
|
5
|
+
let strokes: string[] = []
|
|
6
|
+
beforeEach(() => {
|
|
7
|
+
strokes = []
|
|
8
|
+
vi.spyOn(HTMLCanvasElement.prototype, 'getContext').mockImplementation(function () {
|
|
9
|
+
const ctx = {
|
|
10
|
+
fillStyle: '',
|
|
11
|
+
strokeStyle: '',
|
|
12
|
+
globalAlpha: 1,
|
|
13
|
+
lineWidth: 1,
|
|
14
|
+
lineCap: 'butt',
|
|
15
|
+
setTransform: () => {},
|
|
16
|
+
clearRect: () => {},
|
|
17
|
+
beginPath: () => {},
|
|
18
|
+
moveTo: () => {},
|
|
19
|
+
lineTo: () => {},
|
|
20
|
+
arc: () => {},
|
|
21
|
+
fill: () => {},
|
|
22
|
+
stroke: () => strokes.push(ctx.strokeStyle),
|
|
23
|
+
}
|
|
24
|
+
return ctx as unknown as CanvasRenderingContext2D
|
|
25
|
+
})
|
|
26
|
+
})
|
|
27
|
+
afterEach(() => {
|
|
28
|
+
__resetFireworks()
|
|
29
|
+
vi.restoreAllMocks()
|
|
30
|
+
vi.unstubAllGlobals()
|
|
31
|
+
document.querySelectorAll('canvas').forEach((c) => c.remove())
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
const canvas = () => document.querySelector('canvas')
|
|
35
|
+
const wait = (ms: number) => new Promise((r) => setTimeout(r, ms))
|
|
36
|
+
|
|
37
|
+
describe('useFireworks', () => {
|
|
38
|
+
it('lays a burst out along the shape: n points, the farthest at 1', () => {
|
|
39
|
+
for (const shape of ['Circle', 'Heart', 'Flower'] as const) {
|
|
40
|
+
const out = shapeOutline(shape, 48)
|
|
41
|
+
expect(out).toHaveLength(96)
|
|
42
|
+
let max = 0
|
|
43
|
+
for (let i = 0; i < out.length; i += 2) max = Math.max(max, Math.hypot(out[i]!, out[i + 1]!))
|
|
44
|
+
expect(max).toBeCloseTo(1, 5)
|
|
45
|
+
}
|
|
46
|
+
// A circle's points all sit on it
|
|
47
|
+
const ring = shapeOutline('Circle', 32)
|
|
48
|
+
for (let i = 0; i < ring.length; i += 2) expect(Math.hypot(ring[i]!, ring[i + 1]!)).toBeGreaterThan(0.97)
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
it('launches rockets on the shared canvas in the given colors, and removes it after the last burst', async () => {
|
|
52
|
+
launchFireworks({ count: 2, colors: ['red'] })
|
|
53
|
+
expect(canvas()!.getAttribute('aria-hidden')).toBe('true')
|
|
54
|
+
await wait(200)
|
|
55
|
+
expect(strokes.length).toBeGreaterThan(0)
|
|
56
|
+
expect(new Set(strokes)).toEqual(new Set(['red']))
|
|
57
|
+
// Up to 0.5s of stagger, 1.3s of flight and 2.6s of burst
|
|
58
|
+
await wait(4600)
|
|
59
|
+
expect(canvas()).toBeNull()
|
|
60
|
+
}, 8000)
|
|
61
|
+
|
|
62
|
+
it("a classic burst throws streaks out in every direction that fall as they fade", async () => {
|
|
63
|
+
const segments: [number, number, number, number][] = []
|
|
64
|
+
let from: [number, number] = [0, 0]
|
|
65
|
+
vi.mocked(HTMLCanvasElement.prototype.getContext).mockImplementation(function () {
|
|
66
|
+
const ctx = {
|
|
67
|
+
fillStyle: '', strokeStyle: '', globalAlpha: 1, lineWidth: 1, lineCap: 'butt',
|
|
68
|
+
setTransform: () => {}, clearRect: () => {}, beginPath: () => {}, arc: () => {}, fill: () => {},
|
|
69
|
+
moveTo: (x: number, y: number) => { from = [x, y] },
|
|
70
|
+
lineTo: (x: number, y: number) => { segments.push([...from, x, y]) },
|
|
71
|
+
stroke: () => strokes.push(ctx.strokeStyle),
|
|
72
|
+
}
|
|
73
|
+
return ctx as unknown as CanvasRenderingContext2D
|
|
74
|
+
})
|
|
75
|
+
launchFireworks({ count: 1, shapes: ['classic'], colors: ['gold'], size: 100 })
|
|
76
|
+
// The rocket flies about a second, then the burst
|
|
77
|
+
await wait(1700)
|
|
78
|
+
const recent = segments.slice(-400)
|
|
79
|
+
const dx = recent.map((s) => s[2] - s[0])
|
|
80
|
+
const dy = recent.map((s) => s[3] - s[1])
|
|
81
|
+
// Streaks going left and right, and (with gravity winning) more of them going down than up by now
|
|
82
|
+
expect(dx.some((d) => d > 0) && dx.some((d) => d < 0)).toBe(true)
|
|
83
|
+
expect(dy.filter((d) => d > 0).length).toBeGreaterThan(dy.filter((d) => d < 0).length)
|
|
84
|
+
expect(new Set(strokes)).toEqual(new Set(['gold']))
|
|
85
|
+
await wait(3200)
|
|
86
|
+
expect(canvas()).toBeNull()
|
|
87
|
+
}, 8000)
|
|
88
|
+
|
|
89
|
+
it('keeps launching with a duration until stop()', async () => {
|
|
90
|
+
const stop = useFireworks().launch({ count: 20, duration: Infinity, colors: ['blue'] })
|
|
91
|
+
await wait(400)
|
|
92
|
+
stop()
|
|
93
|
+
// What's in the air finishes, then the canvas goes
|
|
94
|
+
expect(canvas()).not.toBeNull()
|
|
95
|
+
await wait(4200)
|
|
96
|
+
expect(canvas()).toBeNull()
|
|
97
|
+
}, 8000)
|
|
98
|
+
|
|
99
|
+
it('does nothing under reduced motion', () => {
|
|
100
|
+
vi.stubGlobal('matchMedia', (q: string) => ({ matches: q.includes('reduce') }))
|
|
101
|
+
launchFireworks()
|
|
102
|
+
expect(canvas()).toBeNull()
|
|
103
|
+
})
|
|
104
|
+
})
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
import { describe, it, expect, afterEach, vi } from 'vitest'
|
|
2
|
+
import { effectScope, nextTick, ref } from 'vue'
|
|
3
|
+
import { useMediaPlayer, type MediaPlayerOptions } from '../composables/useMediaPlayer'
|
|
4
|
+
|
|
5
|
+
afterEach(() => {
|
|
6
|
+
vi.useRealTimers()
|
|
7
|
+
vi.restoreAllMocks()
|
|
8
|
+
Reflect.deleteProperty(navigator, 'mediaSession')
|
|
9
|
+
})
|
|
10
|
+
|
|
11
|
+
function setup(options: MediaPlayerOptions) {
|
|
12
|
+
const scope = effectScope()
|
|
13
|
+
const player = scope.run(() => useMediaPlayer(options))!
|
|
14
|
+
return { player, stop: () => scope.stop() }
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// A media element whose play() resolves (or rejects) and fires the events a browser would
|
|
18
|
+
function fakeMedia(opts: { block?: boolean } = {}) {
|
|
19
|
+
const el = document.createElement('video')
|
|
20
|
+
let paused = true
|
|
21
|
+
Object.defineProperty(el, 'paused', { get: () => paused })
|
|
22
|
+
Object.defineProperty(el, 'duration', { value: 120, configurable: true })
|
|
23
|
+
el.play = vi.fn(async () => {
|
|
24
|
+
if (opts.block) throw new DOMException('blocked', 'NotAllowedError')
|
|
25
|
+
paused = false
|
|
26
|
+
el.dispatchEvent(new Event('play'))
|
|
27
|
+
el.dispatchEvent(new Event('playing'))
|
|
28
|
+
})
|
|
29
|
+
el.pause = vi.fn(() => {
|
|
30
|
+
paused = true
|
|
31
|
+
el.dispatchEvent(new Event('pause'))
|
|
32
|
+
})
|
|
33
|
+
return el
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
describe('useMediaPlayer — clock', () => {
|
|
37
|
+
it('advances while playing, at the rate, and stops at the end', async () => {
|
|
38
|
+
vi.useFakeTimers()
|
|
39
|
+
const { player } = setup({ duration: 10 })
|
|
40
|
+
expect(player.duration.value).toBe(10)
|
|
41
|
+
await player.play()
|
|
42
|
+
vi.advanceTimersByTime(1000)
|
|
43
|
+
expect(player.currentTime.value).toBeGreaterThan(0.8)
|
|
44
|
+
expect(player.currentTime.value).toBeLessThan(1.2)
|
|
45
|
+
|
|
46
|
+
player.rate.value = 2
|
|
47
|
+
const before = player.currentTime.value
|
|
48
|
+
vi.advanceTimersByTime(1000)
|
|
49
|
+
expect(player.currentTime.value - before).toBeGreaterThan(1.7)
|
|
50
|
+
|
|
51
|
+
vi.advanceTimersByTime(10000)
|
|
52
|
+
expect(player.currentTime.value).toBe(10)
|
|
53
|
+
expect(player.playing.value).toBe(false)
|
|
54
|
+
expect(player.ended.value).toBe(true)
|
|
55
|
+
|
|
56
|
+
// Playing again starts over
|
|
57
|
+
await player.play()
|
|
58
|
+
expect(player.ended.value).toBe(false)
|
|
59
|
+
expect(player.currentTime.value).toBeLessThan(1)
|
|
60
|
+
})
|
|
61
|
+
|
|
62
|
+
it("doesn't advance while waiting, and loops with loop", async () => {
|
|
63
|
+
vi.useFakeTimers()
|
|
64
|
+
const { player } = setup({ duration: 4, loop: true })
|
|
65
|
+
await player.play()
|
|
66
|
+
player.waiting.value = true
|
|
67
|
+
vi.advanceTimersByTime(1000)
|
|
68
|
+
expect(player.currentTime.value).toBe(0)
|
|
69
|
+
player.waiting.value = false
|
|
70
|
+
vi.advanceTimersByTime(5000)
|
|
71
|
+
expect(player.playing.value).toBe(true)
|
|
72
|
+
expect(player.currentTime.value).toBeLessThan(4)
|
|
73
|
+
})
|
|
74
|
+
|
|
75
|
+
it('seek and skip clamp to the media', () => {
|
|
76
|
+
const { player } = setup({ duration: 30 })
|
|
77
|
+
player.skip()
|
|
78
|
+
expect(player.currentTime.value).toBe(10)
|
|
79
|
+
player.skip(-25)
|
|
80
|
+
expect(player.currentTime.value).toBe(0)
|
|
81
|
+
player.currentTime.value = 99
|
|
82
|
+
expect(player.currentTime.value).toBe(30)
|
|
83
|
+
})
|
|
84
|
+
})
|
|
85
|
+
|
|
86
|
+
describe('useMediaPlayer — media element', () => {
|
|
87
|
+
it('mirrors the element and acts on it', async () => {
|
|
88
|
+
const el = fakeMedia()
|
|
89
|
+
const media = ref<HTMLMediaElement | null>(el)
|
|
90
|
+
const { player } = setup({ media })
|
|
91
|
+
expect(player.duration.value).toBe(120)
|
|
92
|
+
|
|
93
|
+
await player.play()
|
|
94
|
+
expect(el.play).toHaveBeenCalled()
|
|
95
|
+
expect(player.playing.value).toBe(true)
|
|
96
|
+
|
|
97
|
+
player.currentTime.value = 30
|
|
98
|
+
expect(el.currentTime).toBe(30)
|
|
99
|
+
player.rate.value = 1.5
|
|
100
|
+
expect(el.playbackRate).toBe(1.5)
|
|
101
|
+
player.muted.value = true
|
|
102
|
+
expect(el.muted).toBe(true)
|
|
103
|
+
|
|
104
|
+
el.dispatchEvent(new Event('waiting'))
|
|
105
|
+
expect(player.waiting.value).toBe(true)
|
|
106
|
+
el.dispatchEvent(new Event('playing'))
|
|
107
|
+
expect(player.waiting.value).toBe(false)
|
|
108
|
+
|
|
109
|
+
player.playing.value = false
|
|
110
|
+
expect(el.pause).toHaveBeenCalled()
|
|
111
|
+
expect(player.playing.value).toBe(false)
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
it('a position set before the element exists is where it starts', async () => {
|
|
115
|
+
const media = ref<HTMLMediaElement | null>(null)
|
|
116
|
+
const { player } = setup({ media })
|
|
117
|
+
player.seek(105)
|
|
118
|
+
const el = fakeMedia()
|
|
119
|
+
media.value = el
|
|
120
|
+
await nextTick()
|
|
121
|
+
expect(el.currentTime).toBe(105)
|
|
122
|
+
expect(player.currentTime.value).toBe(105)
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
it('reports a blocked autoplay', async () => {
|
|
126
|
+
const { player } = setup({ media: fakeMedia({ block: true }) })
|
|
127
|
+
await player.play()
|
|
128
|
+
expect(player.blocked.value).toBe(true)
|
|
129
|
+
expect(player.playing.value).toBe(false)
|
|
130
|
+
})
|
|
131
|
+
|
|
132
|
+
it('carries rate and volume over to an attached element', async () => {
|
|
133
|
+
const { player } = setup({ duration: 60 })
|
|
134
|
+
player.rate.value = 2
|
|
135
|
+
player.volume.value = 0.3
|
|
136
|
+
const el = fakeMedia()
|
|
137
|
+
player.attach(el)
|
|
138
|
+
await nextTick()
|
|
139
|
+
expect(player.media.value).toBe(el)
|
|
140
|
+
expect(el.playbackRate).toBe(2)
|
|
141
|
+
expect(el.volume).toBeCloseTo(0.3)
|
|
142
|
+
expect(player.duration.value).toBe(120)
|
|
143
|
+
player.attach(undefined)
|
|
144
|
+
await nextTick()
|
|
145
|
+
expect(player.media.value).toBe(null)
|
|
146
|
+
expect(player.duration.value).toBe(60)
|
|
147
|
+
})
|
|
148
|
+
})
|
|
149
|
+
|
|
150
|
+
describe('useMediaPlayer — Media Session', () => {
|
|
151
|
+
it('takes the session while playing: metadata, handlers and position', async () => {
|
|
152
|
+
const handlers: Record<string, ((d: MediaSessionActionDetails) => void) | null> = {}
|
|
153
|
+
const session = {
|
|
154
|
+
metadata: null as unknown,
|
|
155
|
+
playbackState: 'none',
|
|
156
|
+
setActionHandler: (a: string, fn: ((d: MediaSessionActionDetails) => void) | null) => { handlers[a] = fn },
|
|
157
|
+
setPositionState: vi.fn(),
|
|
158
|
+
}
|
|
159
|
+
Object.defineProperty(navigator, 'mediaSession', { value: session, configurable: true })
|
|
160
|
+
vi.stubGlobal('MediaMetadata', class { constructor(public init: MediaMetadataInit) {} })
|
|
161
|
+
const onNext = vi.fn()
|
|
162
|
+
const { player, stop } = setup({ duration: 100, metadata: { title: 'Song', artist: 'Band', artwork: ['/a.png'] }, onNext })
|
|
163
|
+
|
|
164
|
+
await player.play()
|
|
165
|
+
expect((session.metadata as { init: MediaMetadataInit }).init).toMatchObject({ title: 'Song', artist: 'Band', artwork: [{ src: '/a.png' }] })
|
|
166
|
+
expect(session.playbackState).toBe('playing')
|
|
167
|
+
expect(session.setPositionState).toHaveBeenCalledWith({ duration: 100, playbackRate: 1, position: 0 })
|
|
168
|
+
|
|
169
|
+
handlers.seekforward!({ action: 'seekforward' })
|
|
170
|
+
expect(player.currentTime.value).toBe(10)
|
|
171
|
+
handlers.seekto!({ action: 'seekto', seekTime: 42 })
|
|
172
|
+
expect(player.currentTime.value).toBe(42)
|
|
173
|
+
handlers.nexttrack!({ action: 'nexttrack' })
|
|
174
|
+
expect(onNext).toHaveBeenCalled()
|
|
175
|
+
expect(handlers.previoustrack).toBeNull()
|
|
176
|
+
|
|
177
|
+
stop()
|
|
178
|
+
expect(session.metadata).toBeNull()
|
|
179
|
+
expect(handlers.play).toBeNull()
|
|
180
|
+
vi.unstubAllGlobals()
|
|
181
|
+
})
|
|
182
|
+
})
|