Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

More lib tests (#35574)

Terry Sutton73f38a40da5a
diff --git a/apps/studio/lib/mime.test.ts b/apps/studio/lib/mime.test.ts
new file mode 100644
index 00000000000..56ec6a560d2
--- /dev/null
+++ b/apps/studio/lib/mime.test.ts
@@ -0,0 +1,21 @@
+import { describe, it, expect } from 'vitest'
+import { lookupMime } from './mime'
+
+describe('lookupMime', () => {
+ it('returns the correct mime type for a known extension', () => {
+ // 'html' is a common extension in mime-db
+ expect(lookupMime('html')).toBe('text/html')
+ })
+
+ it('returns undefined for an unknown extension', () => {
+ expect(lookupMime('notarealext')).toBeUndefined()
+ })
+
+ it('returns undefined for undefined input', () => {
+ expect(lookupMime(undefined)).toBeUndefined()
+ })
+
+ it('is case insensitive', () => {
+ expect(lookupMime('JPG')).toBe(lookupMime('jpg'))
+ })
+})
diff --git a/apps/studio/lib/password-strength.test.ts b/apps/studio/lib/password-strength.test.ts
new file mode 100644
index 00000000000..41f09655ad1
--- /dev/null
+++ b/apps/studio/lib/password-strength.test.ts
@@ -0,0 +1,80 @@
+import { describe, it, expect, beforeEach, vi } from 'vitest'
+import passwordStrength from './password-strength'
+import { toast } from 'sonner'
+
+// Hoist the post_ mock so it's available before the module is loaded
+const postMock = vi.hoisted(() => vi.fn())
+
+vi.mock('data/fetchers', () => ({
+ post: postMock,
+}))
+
+vi.mock('sonner', () => ({
+ toast: { error: vi.fn() },
+}))
+
+describe('passwordStrength', () => {
+ beforeEach(() => {
+ vi.clearAllMocks()
+ })
+
+ it('returns empty values for message, warning and strength for empty input', async () => {
+ const result = await passwordStrength('')
+ expect(result).toEqual({ message: '', warning: '', strength: 0 })
+ expect(postMock).not.toHaveBeenCalled()
+ })
+
+ it('returns max length message, warning, and strength 0 for password longer than 99 characters', async () => {
+ const longPassword = 'a'.repeat(100)
+ const result = await passwordStrength(longPassword)
+ expect(result.message).toMatch(/maximum length/i)
+ expect(result.warning).toMatch(/less than 100 characters/i)
+ expect(result.strength).toBe(0)
+ expect(postMock).not.toHaveBeenCalled()
+ })
+
+ it('returns strong score, suggestion, and empty warning for strong password', async () => {
+ postMock.mockResolvedValue({
+ data: {
+ result: {
+ score: 4,
+ feedback: { suggestions: ['Successfully updated database password'] },
+ },
+ },
+ error: null,
+ })
+ const result = await passwordStrength('StrongPassword123!')
+ expect(result.message).toMatch(/strong/i)
+ expect(result.message).toContain('Successfully updated database password')
+ expect(result.warning).toBe('')
+ expect(result.strength).toBe(4)
+ })
+
+ it('returns weak score, suggestion, and warning for weak password', async () => {
+ postMock.mockResolvedValue({
+ data: {
+ result: {
+ score: 2,
+ feedback: {
+ suggestions: ['Try a longer password'],
+ warning: 'Too short',
+ },
+ },
+ },
+ error: null,
+ })
+ const result = await passwordStrength('weak')
+ expect(result.message).toMatch(/not secure/i)
+ expect(result.message).toContain('Try a longer password')
+ expect(result.warning).toMatch(/too short/i)
+ expect(result.warning).toMatch(/you need a stronger password/i)
+ expect(result.strength).toBe(2)
+ })
+
+ it('returns empty values and shows toast error on server error', async () => {
+ postMock.mockResolvedValue({ data: null, error: { message: 'Server error' } })
+ const result = await passwordStrength('any')
+ expect(result).toEqual({ message: '', warning: '', strength: 0 })
+ expect(toast.error).toHaveBeenCalledTimes(1)
+ })
+})
diff --git a/apps/studio/lib/pg-format.test.ts b/apps/studio/lib/pg-format.test.ts
new file mode 100644
index 00000000000..02cf406a355
--- /dev/null
+++ b/apps/studio/lib/pg-format.test.ts
@@ -0,0 +1,56 @@
+import { describe, it, expect } from 'vitest'
+import { quoteLiteral } from './pg-format'
+
+describe('quoteLiteral', () => {
+ it('returns NULL for null and undefined', () => {
+ expect(quoteLiteral(null)).toBe('NULL')
+ expect(quoteLiteral(undefined)).toBe('NULL')
+ })
+
+ it('returns correct literals for booleans', () => {
+ expect(quoteLiteral(true)).toBe("'t'")
+ expect(quoteLiteral(false)).toBe("'f'")
+ })
+
+ it('returns quoted string for numbers', () => {
+ expect(quoteLiteral(123)).toBe("'123'")
+ expect(quoteLiteral(-45.67)).toBe("'-45.67'")
+ })
+
+ it('escapes single quotes and backslashes in strings', () => {
+ expect(quoteLiteral("O'Reilly")).toBe("'O''Reilly'")
+ expect(quoteLiteral('back\\slash')).toBe("E'back\\\\slash'")
+ expect(quoteLiteral("quote'and\\backslash")).toBe("E'quote''and\\\\backslash'")
+ })
+
+ it('formats Date objects as UTC strings', () => {
+ const date = new Date('2023-01-01T12:34:56Z')
+ expect(quoteLiteral(date)).toBe("'2023-01-01 12:34:56.000+00'")
+ })
+
+ it('handles Buffer objects', () => {
+ expect(quoteLiteral(Buffer.from('abc'))).toBe("E'\\\\x616263'")
+ })
+
+ it('formats arrays of primitives', () => {
+ expect(quoteLiteral([1, 2, 3])).toBe("'1','2','3'")
+ expect(quoteLiteral(['a', "b'c", 'd'])).toBe("'a','b''c','d'")
+ })
+
+ it('formats nested arrays', () => {
+ expect(
+ quoteLiteral([
+ [1, 2],
+ [3, 4],
+ ])
+ ).toBe("('1', '2'), ('3', '4')")
+ })
+
+ it('formats objects as jsonb', () => {
+ expect(quoteLiteral({ foo: 'bar', n: 1 })).toBe('\'{"foo":"bar","n":1}\'::jsonb')
+ })
+
+ it('handles empty string', () => {
+ expect(quoteLiteral('')).toBe("''")
+ })
+})
diff --git a/apps/studio/lib/pingPostgrest.test.ts b/apps/studio/lib/pingPostgrest.test.ts
new file mode 100644
index 00000000000..79a2e216ade
--- /dev/null
+++ b/apps/studio/lib/pingPostgrest.test.ts
@@ -0,0 +1,38 @@
+import { describe, it, expect, vi, beforeEach } from 'vitest'
+import pingPostgrest from './pingPostgrest'
+import * as fetchModule from './common/fetch'
+
+vi.mock('./constants', () => ({ API_URL: 'https://api.example.com' }))
+
+describe('pingPostgrest', () => {
+ beforeEach(() => {
+ vi.restoreAllMocks()
+ })
+
+ it('returns true if headWithTimeout returns no error', async () => {
+ vi.spyOn(fetchModule, 'headWithTimeout').mockResolvedValue({ error: undefined })
+ const result = await pingPostgrest('my-project')
+ expect(result).toBe(true)
+ })
+
+ it('returns false if headWithTimeout returns an error', async () => {
+ vi.spyOn(fetchModule, 'headWithTimeout').mockResolvedValue({ error: { message: 'fail' } })
+ const result = await pingPostgrest('my-project')
+ expect(result).toBe(false)
+ })
+
+ it('returns false if projectRef is undefined', async () => {
+ const result = await pingPostgrest(undefined as any)
+ expect(result).toBe(false)
+ })
+
+ it('passes timeout option to headWithTimeout', async () => {
+ const spy = vi.spyOn(fetchModule, 'headWithTimeout').mockResolvedValue({ error: undefined })
+ await pingPostgrest('my-project', { timeout: 1234 })
+ expect(spy).toHaveBeenCalledWith(
+ 'https://api.example.com/projects/my-project/api/rest',
+ [],
+ expect.objectContaining({ timeout: 1234 })
+ )
+ })
+})