Files
privacy.sexy/tests/unit/infrastructure/Dialog/Browser/FileSaverDialog.spec.ts
undergroundwires c546a33eff Show native save dialogs in desktop app #50, #264
This commit introduces native operating system file dialogs in the
desktop application replacing the existing web-based dialogs.

It lays the foundation for future enhancements such as:

- Providing error messages when saving or executing files, addressing
  #264.
- Creating system restore points, addressing #50.

Documentation updates:

- Update `desktop-vs-web-features.md` with added functionality.
- Update `README.md` with security feature highlights.
- Update home page documentation to emphasize security features.

Other supporting changes include:

- Integrate IPC communication channels for secure Electron dialog API
  interactions.
- Refactor `IpcRegistration` for more type-safety and simplicity.
- Introduce a Vue hook to encapsulate dialog functionality.
- Improve errors during IPC registration for easier troubleshooting.
- Move `ClientLoggerFactory` for consistency in hooks organization and
  remove `LoggerFactory` interface for simplicity.
- Add tests for the save file dialog in the browser context.
- Add `Blob` polyfill in tests to compensate for the missing
  `blob.text()` function in `jsdom` (see jsdom/jsdom#2555).

Improve environment detection logic:

- Treat test environment as browser environments to correctly activate
  features based on the environment. This resolves issues where the
  environment is misidentified as desktop, but Electron preloader APIs
  are missing.
- Rename `isDesktop` environment identification variable to
  `isRunningAsDesktopApplication` for better clarity and to avoid
  confusion with desktop environments in web/browser/test environments.
- Simplify `BrowserRuntimeEnvironment` to consistently detect
  non-desktop application environments.
- Improve environment detection for Electron main process
  (electron/electron#2288).
2024-01-13 18:04:23 +01:00

127 lines
3.7 KiB
TypeScript

import { describe, it, expect } from 'vitest';
import { FileSaverDialog, SaveAsFunction, WindowOpenFunction } from '@/infrastructure/Dialog/Browser/FileSaverDialog';
import { FileType } from '@/presentation/common/Dialog';
import { expectExists } from '@tests/shared/Assertions/ExpectExists';
describe('FileSaverDialog', () => {
describe('saveFile', () => {
describe('saving file with correct mime type', () => {
const testCases: ReadonlyArray<{
readonly fileType: FileType;
readonly expectedMimeType: string;
}> = [
{ fileType: FileType.BatchFile, expectedMimeType: 'application/bat' },
{ fileType: FileType.ShellScript, expectedMimeType: 'text/x-shellscript' },
];
testCases.forEach(({ fileType, expectedMimeType }) => {
it(`correct mimeType for ${FileType[fileType]}`, () => {
// arrange
let actualMimeType: string | undefined;
const saveAsSpy: SaveAsFunction = (blob) => {
actualMimeType = blob.type;
};
// act
new SaveFileTestSetup()
.withFileType(fileType)
.withSaveAs(saveAsSpy)
.saveFile();
// assert
expect(actualMimeType).to.equal(expectedMimeType);
});
});
});
it('blob contains correct file contents', async () => {
// arrange
const expectedFileContents = 'expected file contents';
let actualBlob: Blob | undefined;
const saveAsSpy: SaveAsFunction = (blob) => {
actualBlob = blob;
};
// act
new SaveFileTestSetup()
.withSaveAs(saveAsSpy)
.withFileContents(expectedFileContents)
.saveFile();
// assert
expectExists(actualBlob);
const actualFileContents = await actualBlob.text();
expect(actualFileContents).to.equal(expectedFileContents);
});
it('opens new window on save failure', () => {
// arrange
const fileContents = 'test file contents';
const failingSaveAs: SaveAsFunction = () => {
throw new Error('injected fail');
};
let calledArgs: Parameters<WindowOpenFunction> | undefined;
const windowOpenSpy: WindowOpenFunction = (...args) => {
calledArgs = args;
};
// act
new SaveFileTestSetup()
.withSaveAs(failingSaveAs)
.withFileType(FileType.BatchFile)
.withFileContents(fileContents)
.withWindowOpen(windowOpenSpy)
.saveFile();
// assert
expectExists(calledArgs);
const [url, target, features] = calledArgs;
const mimeType = 'application/bat';
expect(url).to.equal(`data:${mimeType},${encodeURIComponent(fileContents)}`);
expect(target).to.equal('_blank');
expect(features).to.equal('');
});
});
});
class SaveFileTestSetup {
private saveAs: SaveAsFunction = () => {};
private windowOpen: WindowOpenFunction = () => {};
private fileContents: string = `${SaveFileTestSetup.name} file contents`;
private fileName: string = `${SaveFileTestSetup.name} file name`;
private fileType: FileType = FileType.BatchFile;
public withSaveAs(saveAs: SaveAsFunction): this {
this.saveAs = saveAs;
return this;
}
public withFileContents(fileContents: string): this {
this.fileContents = fileContents;
return this;
}
public withFileType(fileType: FileType): this {
this.fileType = fileType;
return this;
}
public withWindowOpen(windowOpen: WindowOpenFunction): this {
this.windowOpen = windowOpen;
return this;
}
public saveFile() {
const dialog = new FileSaverDialog(
this.saveAs,
this.windowOpen,
);
return dialog.saveFile(
this.fileContents,
this.fileName,
this.fileType,
);
}
}