feat(epg): accept local XMLTV files as EPG sources (#1600)

* feat(epg): accept local XMLTV files as EPG sources

Settings → EPG and the playlist dialog accepted `file://` in their form
pattern, but the main process rejected everything except http(s), so a local
XMLTV entry saved fine and then failed on import. Both surfaces now take a
remote link, a `file:` URL, an absolute POSIX path or a Windows drive/UNC
path (`classifyEpgSourceReference` in shared/interfaces), and the settings
section spells out the accepted formats with examples.

The EPG worker opens every source through `openEpgSourceStream`: remote
links keep the validated-redirect client and trust policy, local files are
read from disk behind the signature-sniffing optional gunzip stage, so
.xml, .xml.gz and extension-less gzip all parse. Only hand-typed sources
may be local: `extractM3uEpgUrls` harvests http(s) links only from M3U
headers, since the local branch bypasses `validateRemoteUrl`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* feat(epg): pick local XMLTV files with a native file dialog

A folder button beside each EPG source row (Settings → EPG and the playlist
dialog) opens the native open-file dialog and writes the chosen absolute
path into the row. New `EPG_OPEN_FILE_DIALOG` IPC behind
`ElectronBridgeApi.openEpgFileDialog`, gated in the renderer by
`RuntimeCapabilitiesService.supportsEpgFilePicker`.

The row's refresh/remove buttons carry `data-test-id`s now, and the EPG
e2e suites address them by id instead of index, since the folder button
became the first button in a row.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(epg): authorize local XMLTV files in the main process

Review follow-up (Greptile P1, Codex P1). The renderer hands source strings
to FETCH_EPG/EPG_FORCE_FETCH unchanged, so the form validator alone could
not enforce the provenance rule: a compromised renderer, or a legacy
`file://` entry an older version stored from an M3U header, could name any
file on disk.

`EpgWorkerService.startFetch` now asks a main-process
`EpgLocalSourceAuthorizer` before a local path reaches the worker: a path
the native picker returned is trusted at once, a hand-typed path is
confirmed once in a native message box the renderer cannot fake, and a
refusal is reported in the progress panel. Allowed paths persist under
TRUSTED_LOCAL_EPG_SOURCES in the main-process config. The worker opens its
local branch only when main set `allowLocalFile`; the service defaults to
deny-all until epg.events installs the persisted authorizer.
`resolvePlaylistEpgSourceState` and `filterPlaylistEpgUrlsForFetch` drop a
stored non-remote entry unless it is also in `manualEpgUrls`.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(epg): fail a refused local EPG fetch instead of resolving it

Review follow-up (Codex P2). A denied native confirmation now rejects the
fetch after reporting the error row, so handleFetchEpg and the renderer's
fetch result cannot claim the file was read. Also restores the unrelated
CLAUDE.md paragraph an earlier formatter pass had reflowed into a list.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* fix(epg): cancel a local source retired during its authorization prompt

Review follow-up (Codex P2). startFetch keeps the request generation
captured before awaiting the native confirmation and rechecks it
afterwards: a source retired meanwhile ends as cancelled instead of
starting an import that a pending clear would then have to await.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

* test(epg): leave the local XMLTV e2e with a pristine settings form

The local-file test ended with the EPG source field still dirty, which
arms the main-process close guard: the app then waited for the unsaved
changes dialog instead of closing, the close timeout killed it, and on
Windows the killed process kept iptvnator.db busy (EBUSY on the data-dir
cleanup) and hung the Playwright worker teardown. Discarding the form
before the app closes takes the test from 15 s to 4 s locally.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>

---------

Co-authored-by: 4gray <fourgray@proton.me>
Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
authored and GitHub committed 2026-09-13 21:04:41 +02:00
1 parent d9c8ba4d0b
commit d438f5c655
65 files changed
+1621 -149

No files matched your search

+110 -32
View File
@@ -1,3 +1,4 @@
import { writeFileSync } from 'node:fs';
import { readFile } from 'node:fs/promises';
import { createServer } from 'node:http';
import { join } from 'node:path';
@@ -233,10 +234,7 @@ test.describe('Electron EPG', () => {
removedUrls: [],
}));
});
await app.mainWindow
.locator('.epg-source-row button')
.nth(1)
.click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
await expect(
app.mainWindow.getByTestId('settings-unsaved-bar')
@@ -296,6 +294,106 @@ test.describe('Electron EPG', () => {
}
});
test('@epg @electron imports a local gzipped XMLTV file given as an absolute path', async ({
dataDir,
}) => {
const localGuide = join(dataDir, 'local guide.xml.gz');
writeFileSync(localGuide, gzipSync(epgFixtureXml));
const app = await launchElectronApp(dataDir);
try {
await openSettings(app.mainWindow);
await openSettingsSection(app.mainWindow, 'epg');
await expect(
app.mainWindow.getByTestId('epg-source-formats')
).toContainText('file://');
await app.mainWindow
.getByRole('button', { name: 'Add EPG source' })
.click();
const field = app.mainWindow
.locator('.epg-source-row input')
.first();
await field.fill('guide.xml');
// Material reveals errors once the control is touched.
await field.blur();
await expect(
app.mainWindow.locator('.epg-source-row mat-error')
).toBeVisible();
await field.fill(localGuide);
await expect(
app.mainWindow.locator('.epg-source-row mat-error')
).toHaveCount(0);
// Native dialogs cannot be driven from Playwright: stub them in
// the main process. A hand-typed path is confirmed once through
// the message box; a refused path is reported, never read.
await app.electronApp.evaluate(({ dialog }) => {
dialog.showMessageBox = (async () => ({
response: 1,
checkboxChecked: false,
})) as typeof dialog.showMessageBox;
});
await app.mainWindow.getByTestId('epg-source-refresh').click();
await expect(
app.mainWindow.locator(
'.epg-progress-panel .import-item.status-error'
)
).toContainText('was not allowed');
expect(await getEpgChannelCount(app.mainWindow)).toBe(0);
await app.electronApp.evaluate(({ dialog }) => {
dialog.showMessageBox = (async () => ({
response: 0,
checkboxChecked: false,
})) as typeof dialog.showMessageBox;
});
await app.mainWindow
.locator('.epg-progress-panel .retry-btn')
.click();
await expect
.poll(() => getEpgChannelCount(app.mainWindow), {
timeout: 30000,
})
.toBeGreaterThan(0);
await expect(
app.mainWindow.locator(
'.epg-progress-panel .import-item.status-complete'
)
).toHaveCount(1);
// A file chosen in the native picker is trusted without a prompt.
const browsedGuide = join(dataDir, 'browsed guide.xml.gz');
writeFileSync(browsedGuide, gzipSync(epgFixtureXml));
await app.electronApp.evaluate(({ dialog }, filePath) => {
dialog.showOpenDialog = (async () => ({
canceled: false,
filePaths: [filePath],
})) as typeof dialog.showOpenDialog;
dialog.showMessageBox = (async () => {
throw new Error('picked files must not prompt');
}) as typeof dialog.showMessageBox;
}, browsedGuide);
await app.mainWindow.getByTestId('epg-source-browse').click();
await expect(field).toHaveValue(browsedGuide);
await app.mainWindow.getByTestId('epg-source-refresh').click();
await expect(
app.mainWindow.locator(
'.epg-progress-panel .import-item.status-complete'
)
).toHaveCount(2);
// Leave with a pristine form: a dirty settings form arms the
// main-process close guard, and the app would then wait for a
// confirmation dialog instead of closing.
await app.mainWindow.getByTestId('discard-settings').click();
await expect(
app.mainWindow.getByTestId('settings-unsaved-bar')
).toHaveCount(0);
} finally {
await closeElectronApp(app);
}
});
test('@epg @electron adds an EPG source, fetches guide data, removes its stored EPG data on save', async ({
dataDir,
}) => {
@@ -316,10 +414,7 @@ test.describe('Electron EPG', () => {
.first()
.fill(epgServer.resourceUrl);
await app.mainWindow
.locator('.epg-source-row button')
.first()
.click();
await app.mainWindow.getByTestId('epg-source-refresh').click();
await expect(
app.mainWindow.locator('.epg-progress-panel')
).toBeVisible();
@@ -355,10 +450,7 @@ test.describe('Electron EPG', () => {
await saveSettings(app.mainWindow);
await app.mainWindow
.locator('.epg-source-row button')
.nth(1)
.click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await expect(app.mainWindow.locator('.epg-source-row')).toHaveCount(
0
);
@@ -436,10 +528,7 @@ test.describe('Electron EPG', () => {
'.epg-progress-panel .import-item.status-error'
)
).toHaveCount(1);
await app.mainWindow
.locator('.epg-source-row button')
.nth(1)
.click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
await expect(
app.mainWindow.locator('.epg-progress-panel .import-item')
@@ -502,8 +591,7 @@ test.describe('Electron EPG', () => {
await app.mainWindow
.locator('.epg-source-row')
.first()
.locator('button')
.nth(1)
.getByTestId('epg-source-remove')
.click();
// A staged removal must not delete data before Save.
expect(await programs()).toContain('Removed Bulletin');
@@ -523,10 +611,7 @@ test.describe('Electron EPG', () => {
await expect.poll(programs).toEqual(['Retained Bulletin']);
await openSettings(app.mainWindow);
await openSettingsSection(app.mainWindow, 'epg');
await app.mainWindow
.locator('.epg-source-row button')
.nth(1)
.click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
await expect.poll(programs).toEqual([]);
await expect.poll(() => getEpgChannelCount(app.mainWindow)).toBe(0);
@@ -592,8 +677,7 @@ test.describe('Electron EPG', () => {
await app.mainWindow
.locator('.epg-source-row')
.nth(1)
.locator('button')
.nth(1)
.getByTestId('epg-source-remove')
.click();
await saveSettings(app.mainWindow);
const firstMetadata = {
@@ -701,10 +785,7 @@ test.describe('Electron EPG', () => {
.locator('.epg-source-row input')
.fill(epgServer.resourceUrl);
await saveSettings(app.mainWindow);
await app.mainWindow
.locator('.epg-source-row button')
.nth(1)
.click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
// The same URL still belongs to the saved M3U playlist.
const retained = await app.mainWindow.evaluate(async () =>
@@ -771,10 +852,7 @@ test.describe('Electron EPG', () => {
.locator('.epg-source-row input')
.first()
.fill(epgServer.resourceUrl);
await app.mainWindow
.locator('.epg-source-row button')
.first()
.click();
await app.mainWindow.getByTestId('epg-source-refresh').click();
await expect
.poll(() => getEpgChannelCount(app.mainWindow), {
@@ -96,7 +96,7 @@ test('@epg @xtream @electron removes uploaded guide data and restores provider E
.toContain('Temporary XMLTV Bulletin');
await openSettings(app.mainWindow);
await openSettingsSection(app.mainWindow, 'epg');
await app.mainWindow.locator('.epg-source-row button').nth(1).click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
await openWorkspaceSection(app.mainWindow, 'Live TV');
await clickCategoryByNameExact(app.mainWindow, fixture.categoryName);
@@ -190,7 +190,7 @@ test('@epg @stalker @electron invalidates a loaded manual XMLTV mapping after so
.toContain('Retired Stalker Bulletin');
await openSettings(app.mainWindow);
await openSettingsSection(app.mainWindow, 'epg');
await app.mainWindow.locator('.epg-source-row button').nth(1).click();
await app.mainWindow.getByTestId('epg-source-remove').click();
await saveSettings(app.mainWindow);
await openWorkspaceSection(app.mainWindow, 'Live TV');
await clickCategoryByNameExact(app.mainWindow, fixture.categoryName);