sunpeak 0.5.9 → 0.5.12

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.
@@ -0,0 +1,116 @@
1
+ import { render, screen, fireEvent } from '@testing-library/react';
2
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
3
+ import { CounterResource } from './counter-resource';
4
+
5
+ // Mock sunpeak hooks
6
+ const mockSetWidgetState = vi.fn();
7
+ let mockSafeArea = { insets: { top: 0, bottom: 0, left: 0, right: 0 } };
8
+ let mockMaxHeight = 600;
9
+ let mockUserAgent: {
10
+ device: { type: 'desktop' | 'mobile' | 'tablet' | 'unknown' };
11
+ capabilities: { hover: boolean; touch: boolean };
12
+ } = {
13
+ device: { type: 'desktop' },
14
+ capabilities: { hover: true, touch: false },
15
+ };
16
+
17
+ vi.mock('sunpeak', () => ({
18
+ useWidgetState: () => [{ count: 0 }, mockSetWidgetState],
19
+ useSafeArea: () => mockSafeArea,
20
+ useMaxHeight: () => mockMaxHeight,
21
+ useUserAgent: () => mockUserAgent,
22
+ }));
23
+
24
+ describe('CounterResource', () => {
25
+ beforeEach(() => {
26
+ vi.clearAllMocks();
27
+ mockSafeArea = { insets: { top: 0, bottom: 0, left: 0, right: 0 } };
28
+ mockMaxHeight = 600;
29
+ mockUserAgent = { device: { type: 'desktop' }, capabilities: { hover: true, touch: false } };
30
+ });
31
+
32
+ it('renders counter with initial count', () => {
33
+ render(<CounterResource />);
34
+
35
+ expect(screen.getByText('Welcome to Sunpeak!')).toBeInTheDocument();
36
+ expect(screen.getByText('0')).toBeInTheDocument();
37
+ });
38
+
39
+ it('increments counter when + button is clicked', () => {
40
+ render(<CounterResource />);
41
+
42
+ const incrementButton = screen.getByLabelText('Increment');
43
+ fireEvent.click(incrementButton);
44
+
45
+ expect(mockSetWidgetState).toHaveBeenCalledWith({ count: 1 });
46
+ });
47
+
48
+ it('decrements counter when - button is clicked', () => {
49
+ render(<CounterResource />);
50
+
51
+ const decrementButton = screen.getByLabelText('Decrement');
52
+ fireEvent.click(decrementButton);
53
+
54
+ expect(mockSetWidgetState).toHaveBeenCalledWith({ count: -1 });
55
+ });
56
+
57
+ it('resets counter when Reset button is clicked', () => {
58
+ render(<CounterResource />);
59
+
60
+ const resetButton = screen.getByText('Reset');
61
+ fireEvent.click(resetButton);
62
+
63
+ expect(mockSetWidgetState).toHaveBeenCalledWith({ count: 0 });
64
+ });
65
+
66
+ it('respects safe area insets', () => {
67
+ mockSafeArea = { insets: { top: 20, bottom: 30, left: 10, right: 15 } };
68
+
69
+ const { container } = render(<CounterResource />);
70
+ const mainDiv = container.firstChild as HTMLElement;
71
+
72
+ expect(mainDiv).toHaveStyle({
73
+ paddingTop: 'calc(2rem + 20px)',
74
+ paddingBottom: 'calc(2rem + 30px)',
75
+ paddingLeft: 'calc(2rem + 10px)',
76
+ paddingRight: 'calc(2rem + 15px)',
77
+ });
78
+ });
79
+
80
+ it('respects maxHeight constraint', () => {
81
+ mockMaxHeight = 400;
82
+
83
+ const { container } = render(<CounterResource />);
84
+ const mainDiv = container.firstChild as HTMLElement;
85
+
86
+ expect(mainDiv).toHaveStyle({
87
+ maxHeight: '400px',
88
+ });
89
+ });
90
+
91
+ it('renders larger buttons for touch devices', () => {
92
+ mockUserAgent = { device: { type: 'mobile' }, capabilities: { hover: false, touch: true } };
93
+
94
+ render(<CounterResource />);
95
+
96
+ const incrementButton = screen.getByLabelText('Increment');
97
+ const resetButton = screen.getByText('Reset');
98
+
99
+ // Buttons should have larger size for touch
100
+ expect(incrementButton).toBeInTheDocument();
101
+ expect(resetButton).toBeInTheDocument();
102
+ });
103
+
104
+ it('renders standard-sized buttons for non-touch devices', () => {
105
+ mockUserAgent = { device: { type: 'desktop' }, capabilities: { hover: true, touch: false } };
106
+
107
+ render(<CounterResource />);
108
+
109
+ const incrementButton = screen.getByLabelText('Increment');
110
+ const resetButton = screen.getByText('Reset');
111
+
112
+ // Buttons should have standard size for non-touch
113
+ expect(incrementButton).toBeInTheDocument();
114
+ expect(resetButton).toBeInTheDocument();
115
+ });
116
+ });
@@ -1,4 +1,4 @@
1
- import { useWidgetState } from 'sunpeak';
1
+ import { useWidgetState, useSafeArea, useMaxHeight, useUserAgent } from 'sunpeak';
2
2
  import { Button } from '@openai/apps-sdk-ui/components/Button';
3
3
 
4
4
  interface CounterState extends Record<string, unknown> {
@@ -15,8 +15,12 @@ export function CounterResource() {
15
15
  const [widgetState, setWidgetState] = useWidgetState<CounterState>(() => ({
16
16
  count: 0,
17
17
  }));
18
+ const safeArea = useSafeArea();
19
+ const maxHeight = useMaxHeight();
20
+ const userAgent = useUserAgent();
18
21
 
19
22
  const count = widgetState?.count ?? 0;
23
+ const hasTouch = userAgent?.capabilities.touch ?? false;
20
24
 
21
25
  const increment = () => {
22
26
  setWidgetState({ count: count + 1 });
@@ -31,7 +35,16 @@ export function CounterResource() {
31
35
  };
32
36
 
33
37
  return (
34
- <div className="flex flex-col items-center justify-center p-8 space-y-6">
38
+ <div
39
+ className="flex flex-col items-center justify-center p-8 space-y-6"
40
+ style={{
41
+ paddingTop: `calc(2rem + ${safeArea?.insets.top ?? 0}px)`,
42
+ paddingBottom: `calc(2rem + ${safeArea?.insets.bottom ?? 0}px)`,
43
+ paddingLeft: `calc(2rem + ${safeArea?.insets.left ?? 0}px)`,
44
+ paddingRight: `calc(2rem + ${safeArea?.insets.right ?? 0}px)`,
45
+ maxHeight: maxHeight ?? undefined,
46
+ }}
47
+ >
35
48
  <div className="text-center space-y-2">
36
49
  <h1 className="text-3xl font-bold text-primary">Welcome to Sunpeak!</h1>
37
50
  <p className="text-secondary">Build your MCP resource here</p>
@@ -41,15 +54,27 @@ export function CounterResource() {
41
54
  <div className="text-6xl font-bold text-primary">{count}</div>
42
55
 
43
56
  <div className="flex gap-2">
44
- <Button variant="soft" color="secondary" onClick={decrement} aria-label="Decrement">
57
+ <Button
58
+ variant="soft"
59
+ color="secondary"
60
+ onClick={decrement}
61
+ aria-label="Decrement"
62
+ size={hasTouch ? 'lg' : 'md'}
63
+ >
45
64
 
46
65
  </Button>
47
- <Button variant="solid" color="primary" onClick={increment} aria-label="Increment">
66
+ <Button
67
+ variant="solid"
68
+ color="primary"
69
+ onClick={increment}
70
+ aria-label="Increment"
71
+ size={hasTouch ? 'lg' : 'md'}
72
+ >
48
73
  +
49
74
  </Button>
50
75
  </div>
51
76
 
52
- <Button variant="outline" color="secondary" onClick={reset} size="sm">
77
+ <Button variant="outline" color="secondary" onClick={reset} size={hasTouch ? 'md' : 'sm'}>
53
78
  Reset
54
79
  </Button>
55
80
  </div>