Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Add service name extraction unit tests (#798)
Leendert de Borst1d5c2885141e
diff --git a/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.en.test.ts b/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.en.test.tsnew file mode 100644index 000000000..5b553eabf--- /dev/null+++ b/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.en.test.ts@@ -0,0 +1,77 @@+import { describe, it, expect } from 'vitest';+import { createTestDocument } from './TestUtils';+import { FormDetector } from '../FormDetector';++describe('FormDetector.getSuggestedServiceName (English)', () => {+ it('should extract service name from title with divider', () => {+ const { document, location } = createTestDocument(+ 'Welcome to MyBank - Online Banking Platform For You',+ 'https://www.mybank.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('MyBank');+ });++ it('should extract service name from title without divider', () => {+ const { document, location } = createTestDocument(+ 'GitHub: Let\'s build from here',+ 'https://github.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('GitHub');+ });++ it('should handle titles with multiple meaningful words', () => {+ const { document, location } = createTestDocument(+ 'Amazon Shopping Cart',+ 'https://www.amazon.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('Amazon Shopping');+ });++ it('should fall back to domain name when title has no meaningful words', () => {+ const { document, location } = createTestDocument(+ 'Home | Welcome',+ 'https://www.example.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('example.com');+ });++ it('should handle titles with special characters', () => {+ const { document, location } = createTestDocument(+ 'Netflix - Watch TV Shows Online, Watch Movies Online',+ 'https://www.netflix.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('Netflix');+ });++ it('should handle titles with multiple dividers', () => {+ const { document, location } = createTestDocument(+ 'Twitter / X - Social Media Platform',+ 'https://twitter.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('Twitter');+ });++ it('should handle empty titles', () => {+ const { document, location } = createTestDocument(+ '',+ 'https://www.example.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('example.com');+ });++ it('should handle titles with only stop words', () => {+ const { document, location } = createTestDocument(+ 'The and or but',+ 'https://www.example.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('example.com');+ });+});\ No newline at end of filediff --git a/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.nl.test.ts b/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.nl.test.tsnew file mode 100644index 000000000..a2151d8bb--- /dev/null+++ b/browser-extension/src/utils/formDetector/__tests__/FormDetector.serviceName.nl.test.ts@@ -0,0 +1,77 @@+import { describe, it, expect } from 'vitest';+import { createTestDocument } from './TestUtils';+import { FormDetector } from '../FormDetector';++describe('FormDetector.getSuggestedServiceName (Dutch)', () => {+ it('should extract service name from title with divider', () => {+ const { document, location } = createTestDocument(+ 'ING - Online Bankieren',+ 'https://www.ing.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('ING');+ });++ it('should extract service name from title without divider', () => {+ const { document, location } = createTestDocument(+ 'Bol.com | De winkel van ons allemaal',+ 'https://www.bol.com'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('Bol.com');+ });++ it('should handle titles with multiple meaningful words', () => {+ const { document, location } = createTestDocument(+ 'Albert Heijn Online Boodschappen',+ 'https://www.ah.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('Albert Heijn Online Boodschappen');+ });++ it('should fall back to domain name when title has no meaningful words', () => {+ const { document, location } = createTestDocument(+ 'Home | Welkom',+ 'https://www.voorbeeld.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('voorbeeld.nl');+ });++ it('should handle titles with special characters', () => {+ const { document, location } = createTestDocument(+ 'NS - Nederlandse Spoorwegen',+ 'https://www.ns.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('NS - Nederlandse Spoorwegen');+ });++ it('should handle titles with multiple dividers', () => {+ const { document, location } = createTestDocument(+ 'KPN / Internet & TV',+ 'https://www.kpn.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('KPN');+ });++ it('should handle empty titles', () => {+ const { document, location } = createTestDocument(+ '',+ 'https://www.voorbeeld.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('voorbeeld.nl');+ });++ it('should handle titles with only Dutch stop words', () => {+ const { document, location } = createTestDocument(+ 'Je in op de',+ 'https://www.voorbeeld.nl'+ );+ const serviceName = FormDetector.getSuggestedServiceName(document, location);+ expect(serviceName).toBe('voorbeeld.nl');+ });+});\ No newline at end of filediff --git a/browser-extension/src/utils/formDetector/__tests__/TestUtils.ts b/browser-extension/src/utils/formDetector/__tests__/TestUtils.tsindex 74b8e4322..f02da24c2 100644--- a/browser-extension/src/utils/formDetector/__tests__/TestUtils.ts+++ b/browser-extension/src/utils/formDetector/__tests__/TestUtils.ts@@ -40,6 +40,34 @@ export const createTestDom = (htmlFile: string) : JSDOM => {});};+/**+ * Creates a test document with the specified title and URL.+ * This is used for testing service name extraction.+ */+export const createTestDocument = (title: string, url: string) : { document: Document, location: Location } => {+ const dom = createTestDom('empty.html');+ const document = dom.window.document;++ // Set the title+ document.title = title;++ // Create a proper Location object+ const location = {+ href: url,+ origin: new URL(url).origin,+ protocol: new URL(url).protocol,+ host: new URL(url).host,+ hostname: new URL(url).hostname,+ port: new URL(url).port,+ pathname: new URL(url).pathname,+ search: new URL(url).search,+ hash: new URL(url).hash,+ ancestorOrigins: {} as DOMStringList,+ } as Location;++ return { document, location };+};+/*** Helper function to test field detection*/diff --git a/browser-extension/src/utils/formDetector/__tests__/test-forms/empty.html b/browser-extension/src/utils/formDetector/__tests__/test-forms/empty.htmlnew file mode 100644index 000000000..e69de29bb