Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
push:
branches: [ master ]


permissions: {}

env:
Expand Down
30 changes: 30 additions & 0 deletions src/plugins/api-server/backend/routes/control.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,29 @@ const routes = {
description: 'Success',
},
},
}),
playPlaylist: createRoute({
method: 'post',
path: `/api/${API_VERSION}/playPlaylist`,
summary: 'play playlist',
description: 'Plays a playlist given its ID and optionally a video ID',
request: {
body: {
content: {
'application/json': {
schema: z.object({
playlistId: z.string().describe('Playlist ID'),
videoId: z.string().optional().describe('Video ID (optional)'),
}),
Comment on lines +69 to +81

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🟡 Minor

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
# Read the specific lines for the schema definition
echo "=== Schema Definition (lines 69-81) ==="
sed -n '69,90p' src/plugins/api-server/backend/routes/control.ts

# Read the controller implementation for playPlaylist
echo "=== playPlaylist Controller Implementation ==="
rg -A 20 "async playPlaylist" src/providers/song-controls.ts

# Check if there are any existing validation tests
echo "=== Look for validation tests ==="
find . -type f -name "*.test.ts" -o -name "*.spec.ts" | head -5

Repository: pear-devs/pear-desktop

Length of output: 1236


🏁 Script executed:

#!/bin/bash
# Search for playPlaylist function in song-controls.ts more broadly
echo "=== Searching for playPlaylist in song-controls.ts ==="
grep -n "playPlaylist" src/providers/song-controls.ts

# If found, show the function definition
if grep -q "playPlaylist" src/providers/song-controls.ts; then
    echo "=== playPlaylist Function Context ==="
    grep -B 5 -A 30 "playPlaylist" src/providers/song-controls.ts
fi

# Verify Zod behavior with a quick script
echo "=== Verifying Zod behavior with empty strings ==="
node - <<'JS'
const { z } = require('zod');

const schema = z.object({
  playlistId: z.string().describe('Playlist ID'),
  videoId: z.string().optional().describe('Video ID (optional)'),
});

const testCases = [
  { playlistId: '', videoId: undefined },
  { playlistId: '   ', videoId: 'vid123' },
  { playlistId: 'valid-id', videoId: '' },
  { playlistId: 'valid-id', videoId: '   ' }
];

testCases.forEach((data, i) => {
  try {
    const result = schema.parse(data);
    console.log(`Test ${i+1} PASSED:`, JSON.stringify(data), '->', JSON.stringify(result));
  } catch (e) {
    console.log(`Test ${i+1} FAILED:`, JSON.stringify(data), '->', e.errors.map(e => e.message).join(', '));
  }
});
JS

Repository: pear-devs/pear-desktop

Length of output: 1354


Reject blank IDs in the request schema.

z.string() accepts empty strings '' and whitespace-only values. The controller logic silently skips execution if a blank playlistId is provided (due to falsy check) while the endpoint still returns 204 Success. This masks client errors.

Update the schema to enforce non-empty, trimmed strings:

Suggested change
     request: {
       body: {
         content: {
           'application/json': {
             schema: z.object({
-              playlistId: z.string().describe('Playlist ID'),
-              videoId: z.string().optional().describe('Video ID (optional)'),
+              playlistId: z.string().trim().min(1).describe('Playlist ID'),
+              videoId: z.string().trim().min(1).optional().describe('Video ID (optional)'),
             }),
           },
         },
       },
     },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
playPlaylist: createRoute({
method: 'post',
path: `/api/${API_VERSION}/playPlaylist`,
summary: 'play playlist',
description: 'Plays a playlist given its ID and optionally a video ID',
request: {
body: {
content: {
'application/json': {
schema: z.object({
playlistId: z.string().describe('Playlist ID'),
videoId: z.string().optional().describe('Video ID (optional)'),
}),
playPlaylist: createRoute({
method: 'post',
path: `/api/${API_VERSION}/playPlaylist`,
summary: 'play playlist',
description: 'Plays a playlist given its ID and optionally a video ID',
request: {
body: {
content: {
'application/json': {
schema: z.object({
playlistId: z.string().trim().min(1).describe('Playlist ID'),
videoId: z.string().trim().min(1).optional().describe('Video ID (optional)'),
}),
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/plugins/api-server/backend/routes/control.ts` around lines 69 - 81, The
playPlaylist request schema in control.ts currently uses z.string() for
playlistId, which allows blank or whitespace-only IDs and can lead to a false
204 success; update the createRoute body schema to validate a trimmed, non-empty
playlistId (and keep videoId aligned if needed) so invalid requests are rejected
before reaching the controller logic.

},
},
},
},
responses: {
204: {
description: 'Success',
},
},
}),
pause: createRoute({
method: 'post',
Expand Down Expand Up @@ -597,6 +620,13 @@ export const register = (
ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.playPlaylist, (ctx) => {
const { playlistId, videoId } = ctx.req.valid('json');
controller.playPlaylist(playlistId, videoId);

ctx.status(204);
return ctx.body(null);
});
app.openapi(routes.pause, (ctx) => {
controller.pause();

Expand Down
11 changes: 11 additions & 0 deletions src/providers/song-controls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,5 +146,16 @@ export const getSongControls = (win: BrowserWindow) => {
});
win.webContents.send('peard:search', query, params, continuation);
}),
playPlaylist: (playlistId: ArgsType<string>, videoId?: ArgsType<string>) => {
const pid = parseStringFromArgsType(playlistId);
const vid = videoId ? parseStringFromArgsType(videoId) : null;
if (pid) {
let url = `https://music.youtube.com/playlist?list=${pid}`;
if (vid) {
url += `&v=${vid}`;
}
win.webContents.loadURL(url);
Comment on lines +149 to +157

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🩺 Stability & Availability | 🟠 Major

Handle loadURL() promise to ensure navigation completion and error reporting.

win.webContents.loadURL(url) returns a Promise<void> that rejects on navigation failure. The current playPlaylist implementation drops this promise, and the route handler in src/plugins/api-server/backend/routes/control.ts (lines 623–629) is synchronous, returning HTTP 204 before navigation completes. This results in unhandled navigation errors and premature success responses.

Update playPlaylist to return the promise and make the route handler asynchronous to await it:

Suggested change
-    playPlaylist: (playlistId: ArgsType<string>, videoId?: ArgsType<string>) => {
+    playPlaylist: async (playlistId: ArgsType<string>, videoId?: ArgsType<string>) => {
       const pid = parseStringFromArgsType(playlistId);
       const vid = videoId ? parseStringFromArgsType(videoId) : null;
       if (pid) {
         let url = `https://music.youtube.com/playlist?list=${pid}`;
         if (vid) {
           url += `&v=${vid}`;
         }
-        win.webContents.loadURL(url);
+        return win.webContents.loadURL(url);
       }
     },
-  app.openapi(routes.playPlaylist, (ctx) => {
+  app.openapi(routes.playPlaylist, async (ctx) => {
     const { playlistId, videoId } = ctx.req.valid('json');
-    controller.playPlaylist(playlistId, videoId);
+    await controller.playPlaylist(playlistId, videoId);

     ctx.status(204);
     return ctx.body(null);
   });
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/song-controls.ts` around lines 149 - 157, `playPlaylist` in
`song-controls.ts` currently ignores the `win.webContents.loadURL(url)` promise,
so navigation failures are unhandled and the API route can respond too early.
Update `playPlaylist` to return the `loadURL` promise (or otherwise await it),
and make the control route in `control.ts` asynchronous so it awaits
`playPlaylist` before sending the 204 response. Use the existing `playPlaylist`
method and the route handler that calls it to keep error reporting and
completion behavior aligned.

}
},
};
};