chore: land in-progress workspace changes before 1.16.2
The tree carried uncommitted work from several areas (web push, mobile screens, admin charts, test-hygiene cleanup, CI and map docs). It is landed as one commit so the release pipeline can start from a clean tree; the desktop transcript-correction behaviour shipped in the previous commit and is unchanged here.
This commit is contained in:
parent
9c2b6a9782
commit
7fe49c437e
144 changed files with 3760 additions and 4523 deletions
76
apps/web/tests/billing-catalog.test.ts
Normal file
76
apps/web/tests/billing-catalog.test.ts
Normal file
|
|
@ -0,0 +1,76 @@
|
|||
import { describe, expect, test } from 'vitest'
|
||||
import {
|
||||
formatBillingPrice,
|
||||
formatPlanCatalogPrice,
|
||||
parseBillingCatalog,
|
||||
} from '../src/lib/billing-catalog'
|
||||
|
||||
const catalogPayload = {
|
||||
schema_version: '1',
|
||||
plans: [
|
||||
{
|
||||
tier: 'pro',
|
||||
prices: [
|
||||
{ provider: 'payple', unit_amount: 9900, currency: 'KRW', interval: 'month', interval_count: 1 },
|
||||
],
|
||||
},
|
||||
{
|
||||
tier: 'pro_plus',
|
||||
prices: [
|
||||
{ provider: 'payple', unit_amount: 29900, currency: 'KRW', interval: 'month', interval_count: 1 },
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
||||
describe('billing catalog strict client contract', () => {
|
||||
test('parses provider prices and formats minor currency units', () => {
|
||||
const catalog = parseBillingCatalog(catalogPayload)
|
||||
expect(catalog).not.toBeNull()
|
||||
expect(formatBillingPrice(catalog!.plans.pro[0])).toContain('9,900')
|
||||
expect(formatPlanCatalogPrice('pro', catalog)).toContain('9,900')
|
||||
expect(formatPlanCatalogPrice('pro_plus', catalog)).toContain('29,900')
|
||||
expect(formatPlanCatalogPrice('free', catalog)).toBe('무료')
|
||||
})
|
||||
|
||||
test('rejects duplicate tiers/providers, malformed amounts, and unknown values', () => {
|
||||
const invalid = [
|
||||
null,
|
||||
{},
|
||||
{ ...catalogPayload, schema_version: '2' },
|
||||
{ ...catalogPayload, plans: [catalogPayload.plans[0], catalogPayload.plans[0]] },
|
||||
{
|
||||
...catalogPayload,
|
||||
plans: [{
|
||||
tier: 'pro',
|
||||
prices: [catalogPayload.plans[0].prices[0], catalogPayload.plans[0].prices[0]],
|
||||
}, catalogPayload.plans[1]],
|
||||
},
|
||||
{
|
||||
...catalogPayload,
|
||||
plans: [{ tier: 'pro', prices: [{ ...catalogPayload.plans[0].prices[0], unit_amount: 0 }] }, catalogPayload.plans[1]],
|
||||
},
|
||||
{
|
||||
...catalogPayload,
|
||||
plans: [{ tier: 'pro', prices: [{ ...catalogPayload.plans[0].prices[0], currency: 'KRW<script>' }] }, catalogPayload.plans[1]],
|
||||
},
|
||||
{
|
||||
...catalogPayload,
|
||||
plans: [{ tier: 'pro', prices: [{ ...catalogPayload.plans[0].prices[0], provider: 'google_play' }] }, catalogPayload.plans[1]],
|
||||
},
|
||||
]
|
||||
for (const value of invalid) expect(parseBillingCatalog(value)).toBeNull()
|
||||
})
|
||||
|
||||
test('keeps an unavailable provider absent instead of inventing a price', () => {
|
||||
const catalog = parseBillingCatalog({
|
||||
schema_version: '1',
|
||||
plans: [
|
||||
{ tier: 'pro', prices: [] },
|
||||
{ tier: 'pro_plus', prices: [] },
|
||||
],
|
||||
})
|
||||
expect(catalog).not.toBeNull()
|
||||
expect(formatPlanCatalogPrice('pro', catalog)).toBeNull()
|
||||
})
|
||||
})
|
||||
504
apps/web/tests/web-push.test.ts
Normal file
504
apps/web/tests/web-push.test.ts
Normal file
|
|
@ -0,0 +1,504 @@
|
|||
import { describe, expect, test } from 'vitest'
|
||||
import type { getSupabaseBrowserClient } from '../src/lib/supabase-browser'
|
||||
import {
|
||||
describeWebBrowserDevice,
|
||||
ensureCurrentWebDevice,
|
||||
getOrCreateWebInstallationId,
|
||||
getStoredWebInstallationId,
|
||||
} from '../src/lib/web-device'
|
||||
import {
|
||||
requestWebPushPermission,
|
||||
subscribeWebPush,
|
||||
unsubscribeWebPush,
|
||||
} from '../src/lib/web-push'
|
||||
import {
|
||||
disableCurrentWebPush,
|
||||
getCurrentWebPushConnectionStatus,
|
||||
reconcileFailedWebPushEnable,
|
||||
signOutWithoutWaitingForWebPushCleanup,
|
||||
} from '../src/lib/web-push-session'
|
||||
|
||||
type WebClient = ReturnType<typeof getSupabaseBrowserClient>
|
||||
|
||||
const USER_ID = '00000000-0000-4000-8000-000000000001'
|
||||
const DEVICE_ID = '00000000-0000-4000-8000-000000000002'
|
||||
const VAPID_KEY = 'AQIDBAUGBwgJCgsMDQ4PEA'
|
||||
|
||||
function installPushBrowser(
|
||||
permission: NotificationPermission,
|
||||
requestPermission: () => Promise<NotificationPermission>,
|
||||
): () => void {
|
||||
const keys = ['window', 'navigator', 'Notification'] as const
|
||||
const originals = new Map(keys.map((key) => [key, Object.getOwnPropertyDescriptor(globalThis, key)]))
|
||||
|
||||
Object.defineProperty(globalThis, 'window', {
|
||||
configurable: true,
|
||||
value: { PushManager: class TestPushManager {} },
|
||||
})
|
||||
Object.defineProperty(globalThis, 'navigator', {
|
||||
configurable: true,
|
||||
value: { serviceWorker: {} },
|
||||
})
|
||||
Object.defineProperty(globalThis, 'Notification', {
|
||||
configurable: true,
|
||||
value: { permission, requestPermission },
|
||||
})
|
||||
|
||||
return () => {
|
||||
for (const key of keys) {
|
||||
const descriptor = originals.get(key)
|
||||
if (descriptor) Object.defineProperty(globalThis, key, descriptor)
|
||||
else Reflect.deleteProperty(globalThis, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function memoryStorage(initial?: Record<string, string>): Storage {
|
||||
const values = new Map(Object.entries(initial ?? {}))
|
||||
return {
|
||||
get length() {
|
||||
return values.size
|
||||
},
|
||||
clear: () => values.clear(),
|
||||
getItem: (key) => values.get(key) ?? null,
|
||||
key: (index) => [...values.keys()][index] ?? null,
|
||||
removeItem: (key) => {
|
||||
values.delete(key)
|
||||
},
|
||||
setItem: (key, value) => {
|
||||
values.set(key, value)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function pushSubscription(
|
||||
onUnsubscribe?: () => void,
|
||||
unsubscribeError?: Error,
|
||||
): PushSubscription {
|
||||
return {
|
||||
endpoint: 'https://push.example/subscription/1',
|
||||
expirationTime: null,
|
||||
options: {
|
||||
applicationServerKey: null,
|
||||
userVisibleOnly: true,
|
||||
},
|
||||
getKey: () => null,
|
||||
toJSON: () => ({
|
||||
endpoint: 'https://push.example/subscription/1',
|
||||
keys: { auth: 'auth-key', p256dh: 'p256dh-key' },
|
||||
}),
|
||||
unsubscribe: async () => {
|
||||
onUnsubscribe?.()
|
||||
if (unsubscribeError) throw unsubscribeError
|
||||
return true
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
function activeDeviceClient(
|
||||
rpc: (name: string, args: unknown) => Promise<{ data: unknown; error: { message: string } | null }>,
|
||||
): WebClient {
|
||||
const chain = {
|
||||
select: () => chain,
|
||||
eq: () => chain,
|
||||
maybeSingle: async () => ({
|
||||
data: { id: DEVICE_ID, revoked_at: null },
|
||||
error: null,
|
||||
}),
|
||||
}
|
||||
return {
|
||||
from: () => chain,
|
||||
rpc,
|
||||
} as unknown as WebClient
|
||||
}
|
||||
|
||||
function registration(
|
||||
subscription: PushSubscription | null,
|
||||
onSubscribe?: () => void,
|
||||
): ServiceWorkerRegistration {
|
||||
return {
|
||||
pushManager: {
|
||||
getSubscription: async () => subscription,
|
||||
permissionState: async () => 'granted',
|
||||
subscribe: async () => {
|
||||
onSubscribe?.()
|
||||
if (!subscription) throw new Error('test registration requires a subscription')
|
||||
return subscription
|
||||
},
|
||||
},
|
||||
} as ServiceWorkerRegistration
|
||||
}
|
||||
|
||||
describe('web push client contract', () => {
|
||||
test('requests permission only after confirming the public VAPID key exists', async () => {
|
||||
let permissionRequests = 0
|
||||
const restore = installPushBrowser('default', async () => {
|
||||
permissionRequests += 1
|
||||
return 'granted'
|
||||
})
|
||||
try {
|
||||
await expect(requestWebPushPermission('')).rejects.toMatchObject({
|
||||
code: 'vapid-key-missing',
|
||||
})
|
||||
expect(permissionRequests).toBe(0)
|
||||
|
||||
await expect(requestWebPushPermission(VAPID_KEY)).resolves.toBeUndefined()
|
||||
expect(permissionRequests).toBe(1)
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test('keeps one valid browser installation ID and replaces invalid stored data', () => {
|
||||
const storage = memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': 'invalid',
|
||||
})
|
||||
expect(getStoredWebInstallationId(storage)).toBeNull()
|
||||
|
||||
const installationId = getOrCreateWebInstallationId(storage, () => DEVICE_ID)
|
||||
expect(installationId).toBe(DEVICE_ID)
|
||||
expect(getOrCreateWebInstallationId(storage, () => USER_ID)).toBe(DEVICE_ID)
|
||||
})
|
||||
|
||||
test('derives bounded browser device metadata without persisting the full user agent', () => {
|
||||
expect(describeWebBrowserDevice(
|
||||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) Chrome/130.0 Safari/537.36',
|
||||
'Win32',
|
||||
)).toEqual({ deviceName: 'Chrome · Windows', osVersion: 'Win32' })
|
||||
})
|
||||
|
||||
test('creates a web device with the stable installation ID before push registration', async () => {
|
||||
const inserts: unknown[] = []
|
||||
const readChain = {
|
||||
select: () => readChain,
|
||||
eq: () => readChain,
|
||||
maybeSingle: async () => ({ data: null, error: null }),
|
||||
}
|
||||
const client = {
|
||||
from: () => ({
|
||||
...readChain,
|
||||
insert: (value: unknown) => {
|
||||
inserts.push(value)
|
||||
return {
|
||||
select: () => ({
|
||||
single: async () => ({
|
||||
data: { id: DEVICE_ID, revoked_at: null },
|
||||
error: null,
|
||||
}),
|
||||
}),
|
||||
}
|
||||
},
|
||||
}),
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(ensureCurrentWebDevice(USER_ID, '1.16.1', {
|
||||
client,
|
||||
storage: memoryStorage(),
|
||||
uuidFactory: () => DEVICE_ID,
|
||||
now: () => new Date('2026-11-04T00:00:00.000Z'),
|
||||
deviceName: 'Chrome · Windows',
|
||||
osVersion: 'Win32',
|
||||
})).resolves.toEqual({
|
||||
id: DEVICE_ID,
|
||||
installationId: DEVICE_ID,
|
||||
revokedAt: null,
|
||||
})
|
||||
expect(inserts).toEqual([{
|
||||
user_id: USER_ID,
|
||||
installation_id: DEVICE_ID,
|
||||
platform: 'web',
|
||||
device_name: 'Chrome · Windows',
|
||||
app_version: '1.16.1',
|
||||
os_version: 'Win32',
|
||||
last_seen_at: '2026-11-04T00:00:00.000Z',
|
||||
}])
|
||||
})
|
||||
|
||||
test('never resurrects a revoked browser device', async () => {
|
||||
const chain = {
|
||||
select: () => chain,
|
||||
eq: () => chain,
|
||||
maybeSingle: async () => ({
|
||||
data: { id: DEVICE_ID, revoked_at: '2026-11-03T00:00:00.000Z' },
|
||||
error: null,
|
||||
}),
|
||||
}
|
||||
const client = {
|
||||
from: () => chain,
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(ensureCurrentWebDevice(USER_ID, '1.16.1', {
|
||||
client,
|
||||
storage: memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
}),
|
||||
deviceName: 'Chrome · Windows',
|
||||
})).rejects.toMatchObject({ code: 'device-revoked' })
|
||||
})
|
||||
|
||||
test('subscribes only once and persists through the device-bound RPC', async () => {
|
||||
const calls: Array<{ name: string; args: unknown }> = []
|
||||
const subscription = pushSubscription()
|
||||
const client = {
|
||||
rpc: async (name: string, args: unknown) => {
|
||||
calls.push({ name, args })
|
||||
return {
|
||||
data: {
|
||||
device_id: DEVICE_ID,
|
||||
provider: 'webpush',
|
||||
registered_at: '2026-11-04T00:00:00.000Z',
|
||||
},
|
||||
error: null,
|
||||
}
|
||||
},
|
||||
} as unknown as WebClient
|
||||
|
||||
const result = await subscribeWebPush({
|
||||
userId: USER_ID,
|
||||
deviceId: DEVICE_ID,
|
||||
vapidPublicKey: VAPID_KEY,
|
||||
client,
|
||||
registration: registration(subscription),
|
||||
})
|
||||
|
||||
expect(calls).toEqual([{
|
||||
name: 'register_push_registration',
|
||||
args: {
|
||||
push_device_id: DEVICE_ID,
|
||||
push_provider: 'webpush',
|
||||
registration_id: JSON.stringify(subscription.toJSON()),
|
||||
},
|
||||
}])
|
||||
expect(result).toMatchObject({
|
||||
provider: 'webpush',
|
||||
endpoint: subscription.endpoint,
|
||||
registeredAt: '2026-11-04T00:00:00.000Z',
|
||||
})
|
||||
})
|
||||
|
||||
test('rolls back the browser subscription when persistence fails', async () => {
|
||||
let unsubscribeCalls = 0
|
||||
const subscription = pushSubscription(() => {
|
||||
unsubscribeCalls += 1
|
||||
})
|
||||
const client = {
|
||||
rpc: async () => ({ data: null, error: { message: 'database unavailable' } }),
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(subscribeWebPush({
|
||||
userId: USER_ID,
|
||||
deviceId: DEVICE_ID,
|
||||
vapidPublicKey: VAPID_KEY,
|
||||
client,
|
||||
registration: registration(subscription),
|
||||
})).rejects.toMatchObject({ code: 'persistence-failed' })
|
||||
expect(unsubscribeCalls).toBe(1)
|
||||
})
|
||||
|
||||
test('removes the server registration and browser subscription together', async () => {
|
||||
let unsubscribeCalls = 0
|
||||
const client = {
|
||||
rpc: async () => ({
|
||||
data: { device_id: DEVICE_ID, removed: true },
|
||||
error: null,
|
||||
}),
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(unsubscribeWebPush({
|
||||
deviceId: DEVICE_ID,
|
||||
client,
|
||||
registration: registration(pushSubscription(() => {
|
||||
unsubscribeCalls += 1
|
||||
})),
|
||||
})).resolves.toEqual({
|
||||
browserSubscriptionRemoved: true,
|
||||
serverRegistrationRemoved: true,
|
||||
})
|
||||
expect(unsubscribeCalls).toBe(1)
|
||||
})
|
||||
|
||||
test('still removes the browser subscription when server cleanup fails', async () => {
|
||||
let unsubscribeCalls = 0
|
||||
const client = {
|
||||
rpc: async () => ({ data: null, error: { message: 'offline' } }),
|
||||
} as unknown as WebClient
|
||||
|
||||
const operation = unsubscribeWebPush({
|
||||
deviceId: DEVICE_ID,
|
||||
client,
|
||||
registration: registration(pushSubscription(() => {
|
||||
unsubscribeCalls += 1
|
||||
})),
|
||||
})
|
||||
await expect(operation).rejects.toMatchObject({
|
||||
name: 'WebPushError',
|
||||
code: 'persistence-failed',
|
||||
})
|
||||
expect(unsubscribeCalls).toBe(1)
|
||||
})
|
||||
|
||||
test('removes the local subscription and bounds a hung unregister RPC', async () => {
|
||||
let unsubscribeCalls = 0
|
||||
const client = {
|
||||
rpc: () => new Promise<never>(() => undefined),
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(unsubscribeWebPush({
|
||||
deviceId: DEVICE_ID,
|
||||
client,
|
||||
registration: registration(pushSubscription(() => {
|
||||
unsubscribeCalls += 1
|
||||
})),
|
||||
remoteTimeoutMs: 25,
|
||||
})).rejects.toMatchObject({ code: 'persistence-failed' })
|
||||
expect(unsubscribeCalls).toBe(1)
|
||||
})
|
||||
|
||||
test('keeps disabled intent when server cleanup succeeds but local unsubscribe fails', async () => {
|
||||
const storage = memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
})
|
||||
const client = activeDeviceClient(async () => ({
|
||||
data: { device_id: DEVICE_ID, removed: true },
|
||||
error: null,
|
||||
}))
|
||||
|
||||
await expect(disableCurrentWebPush(USER_ID, {
|
||||
client,
|
||||
storage,
|
||||
registration: registration(pushSubscription(
|
||||
undefined,
|
||||
new Error('browser unsubscribe failed'),
|
||||
)),
|
||||
})).rejects.toMatchObject({ code: 'unsubscribe-failed' })
|
||||
|
||||
expect(storage.getItem(`d3ro.voice.web-push.disabled.v1.${USER_ID}`)).toBe('1')
|
||||
})
|
||||
|
||||
test('starts local cleanup even when the device lookup never resolves', async () => {
|
||||
let unsubscribeCalls = 0
|
||||
const stalledChain = {
|
||||
select: () => stalledChain,
|
||||
eq: () => stalledChain,
|
||||
maybeSingle: () => new Promise<never>(() => undefined),
|
||||
}
|
||||
const client = {
|
||||
from: () => stalledChain,
|
||||
} as unknown as WebClient
|
||||
|
||||
await expect(disableCurrentWebPush(USER_ID, {
|
||||
client,
|
||||
storage: memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
}),
|
||||
registration: registration(pushSubscription(() => {
|
||||
unsubscribeCalls += 1
|
||||
})),
|
||||
remoteCleanupTimeoutMs: 25,
|
||||
})).rejects.toMatchObject({ code: 'persistence-failed' })
|
||||
expect(unsubscribeCalls).toBe(1)
|
||||
})
|
||||
|
||||
test('does not report a stale local subscription as connected after disable intent', async () => {
|
||||
let rpcCalls = 0
|
||||
const restore = installPushBrowser('granted', async () => 'granted')
|
||||
try {
|
||||
const storage = memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
[`d3ro.voice.web-push.disabled.v1.${USER_ID}`]: '1',
|
||||
})
|
||||
const client = activeDeviceClient(async () => {
|
||||
rpcCalls += 1
|
||||
return { data: null, error: null }
|
||||
})
|
||||
|
||||
await expect(getCurrentWebPushConnectionStatus(USER_ID, {
|
||||
client,
|
||||
storage,
|
||||
vapidPublicKey: VAPID_KEY,
|
||||
registration: registration(pushSubscription()),
|
||||
})).resolves.toBe('orphaned')
|
||||
expect(rpcCalls).toBe(0)
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test('re-registers an existing browser subscription before reporting it connected', async () => {
|
||||
const calls: Array<{ name: string; args: unknown }> = []
|
||||
const restore = installPushBrowser('granted', async () => 'granted')
|
||||
try {
|
||||
const subscription = pushSubscription()
|
||||
const client = activeDeviceClient(async (name, args) => {
|
||||
calls.push({ name, args })
|
||||
return {
|
||||
data: { registered_at: '2026-11-04T00:00:00.000Z' },
|
||||
error: null,
|
||||
}
|
||||
})
|
||||
|
||||
await expect(getCurrentWebPushConnectionStatus(USER_ID, {
|
||||
client,
|
||||
storage: memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
}),
|
||||
vapidPublicKey: VAPID_KEY,
|
||||
registration: registration(subscription),
|
||||
})).resolves.toBe('subscribed')
|
||||
expect(calls).toEqual([{
|
||||
name: 'register_push_registration',
|
||||
args: {
|
||||
push_device_id: DEVICE_ID,
|
||||
push_provider: 'webpush',
|
||||
registration_id: JSON.stringify(subscription.toJSON()),
|
||||
},
|
||||
}])
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test('fails closed when an existing browser subscription cannot be re-registered', async () => {
|
||||
const restore = installPushBrowser('granted', async () => 'granted')
|
||||
try {
|
||||
const client = activeDeviceClient(async () => ({
|
||||
data: null,
|
||||
error: { message: 'database unavailable' },
|
||||
}))
|
||||
await expect(getCurrentWebPushConnectionStatus(USER_ID, {
|
||||
client,
|
||||
storage: memoryStorage({
|
||||
'd3ro.voice.web.installation-id.v1': DEVICE_ID,
|
||||
}),
|
||||
vapidPublicKey: VAPID_KEY,
|
||||
registration: registration(pushSubscription()),
|
||||
})).rejects.toMatchObject({ code: 'persistence-failed' })
|
||||
} finally {
|
||||
restore()
|
||||
}
|
||||
})
|
||||
|
||||
test('keeps the switch checked when failed enable reconciliation finds a live registration', () => {
|
||||
expect(reconcileFailedWebPushEnable('subscribed')).toEqual({
|
||||
enabled: true,
|
||||
status: 'error',
|
||||
})
|
||||
expect(reconcileFailedWebPushEnable('unsubscribed')).toEqual({
|
||||
enabled: false,
|
||||
status: 'unsubscribed',
|
||||
})
|
||||
})
|
||||
|
||||
test('does not make sign-out wait for stalled Web Push cleanup', async () => {
|
||||
let signOutCalls = 0
|
||||
const stalledCleanup = new Promise<void>(() => undefined)
|
||||
|
||||
await expect(signOutWithoutWaitingForWebPushCleanup(
|
||||
() => stalledCleanup,
|
||||
async () => {
|
||||
signOutCalls += 1
|
||||
},
|
||||
)).resolves.toBeUndefined()
|
||||
expect(signOutCalls).toBe(1)
|
||||
})
|
||||
})
|
||||
84
apps/web/tests/web-stt-client.test.ts
Normal file
84
apps/web/tests/web-stt-client.test.ts
Normal file
|
|
@ -0,0 +1,84 @@
|
|||
import { describe, expect, test } from 'vitest'
|
||||
import { transcribeWebAudio, WebSttError } from '../src/lib/web-stt-client'
|
||||
|
||||
const audio = new Blob([new Uint8Array([0x1a, 0x45, 0xdf, 0xa3])], { type: 'audio/webm;codecs=opus' })
|
||||
|
||||
describe('web STT fail-closed client', () => {
|
||||
test('sends one authenticated request and accepts only a strict real transcript', async () => {
|
||||
let calls = 0
|
||||
const result = await transcribeWebAudio({
|
||||
audio,
|
||||
accessToken: 'access-token',
|
||||
supabaseUrl: 'https://project.supabase.co',
|
||||
fetchImpl: async (input, init) => {
|
||||
calls += 1
|
||||
expect(String(input)).toBe('https://project.supabase.co/functions/v1/stt-proxy')
|
||||
expect(init?.headers).toEqual({ Authorization: 'Bearer access-token' })
|
||||
expect(init?.body).toBeInstanceOf(FormData)
|
||||
return new Response(JSON.stringify({
|
||||
transcript: '실제 전사 결과',
|
||||
confidence: 0.97,
|
||||
language_code: 'ko',
|
||||
duration_seconds: 1.25,
|
||||
provider: 'whisper.cpp-local',
|
||||
}), { status: 200, headers: { 'Content-Type': 'application/json' } })
|
||||
},
|
||||
})
|
||||
expect(calls).toBe(1)
|
||||
expect(result.transcript).toBe('실제 전사 결과')
|
||||
})
|
||||
|
||||
test('maps auth, quota, provider and upstream failures without a fallback request', async () => {
|
||||
for (const [status, error, code] of [
|
||||
[401, 'invalid_token', 'auth_required'],
|
||||
[429, 'quota_exceeded', 'quota_exceeded'],
|
||||
[503, 'stt_provider_unavailable', 'provider_unavailable'],
|
||||
[502, 'stt_upstream_failed', 'upstream_failed'],
|
||||
] as const) {
|
||||
let calls = 0
|
||||
const rejection = transcribeWebAudio({
|
||||
audio,
|
||||
accessToken: 'token',
|
||||
supabaseUrl: 'https://project.supabase.co',
|
||||
fetchImpl: async () => {
|
||||
calls += 1
|
||||
return new Response(JSON.stringify({ error }), { status })
|
||||
},
|
||||
})
|
||||
await expect(rejection).rejects.toMatchObject({ code })
|
||||
expect(calls).toBe(1)
|
||||
}
|
||||
})
|
||||
|
||||
test('rejects empty, oversized, wrong MIME, malformed success and insecure endpoints', async () => {
|
||||
const noFetch = async (): Promise<Response> => { throw new Error('must not fetch') }
|
||||
const invalidCalls = [
|
||||
transcribeWebAudio({ audio: new Blob([], { type: 'audio/webm' }), accessToken: 't', supabaseUrl: 'https://p.supabase.co', fetchImpl: noFetch }),
|
||||
transcribeWebAudio({ audio: new Blob([new Uint8Array(25 * 1024 * 1024 + 1)], { type: 'audio/webm' }), accessToken: 't', supabaseUrl: 'https://p.supabase.co', fetchImpl: noFetch }),
|
||||
transcribeWebAudio({ audio: new Blob(['x'], { type: 'audio/mpeg' }), accessToken: 't', supabaseUrl: 'https://p.supabase.co', fetchImpl: noFetch }),
|
||||
transcribeWebAudio({ audio, accessToken: 't', supabaseUrl: 'http://remote.example', fetchImpl: noFetch }),
|
||||
transcribeWebAudio({
|
||||
audio,
|
||||
accessToken: 't',
|
||||
supabaseUrl: 'https://p.supabase.co',
|
||||
fetchImpl: async () => new Response(JSON.stringify({ transcript: 'placeholder' }), { status: 200 }),
|
||||
}),
|
||||
]
|
||||
for (const call of invalidCalls) await expect(call).rejects.toBeInstanceOf(WebSttError)
|
||||
})
|
||||
|
||||
test('aborts the only provider request as cancelled', async () => {
|
||||
const controller = new AbortController()
|
||||
const promise = transcribeWebAudio({
|
||||
audio,
|
||||
accessToken: 'token',
|
||||
supabaseUrl: 'https://project.supabase.co',
|
||||
signal: controller.signal,
|
||||
fetchImpl: async (_input, init) => new Promise((_resolve, reject) => {
|
||||
init?.signal?.addEventListener('abort', () => reject(new DOMException('aborted', 'AbortError')), { once: true })
|
||||
}),
|
||||
})
|
||||
controller.abort()
|
||||
await expect(promise).rejects.toMatchObject({ code: 'cancelled' })
|
||||
})
|
||||
})
|
||||
Loading…
Add table
Add a link
Reference in a new issue