Three S3 configurations had grown separately — S3_* for documents, GENERATED_IMAGES_S3_* for images, and AUDIO_BACKUPS_S3_* after them — with different key names and their own client construction. That is why moving storage meant hunting through several files. src/utils/objectStorage.js now resolves settings for any purpose: its own variables first, then the shared S3_* ones, with a per-purpose bucket name (S3_BUCKET_AUDIO_BACKUPS). One endpoint plus three bucket names is enough for the whole app, and a purpose that needs its own account still overrides everything. Audio backups and documents use it; generated images keeps its own tested storage module, whose variable names the resolver already understands. Nothing existing has to change: S3_ACCESS_KEY_ID, S3_SECRET_ACCESS_KEY and the AWS_* fallbacks still resolve, and path-style addressing keeps each purpose's previous default — off for documents, so a Backblaze endpoint behaves as before, on where a custom endpoint implies MinIO. A _FILE credential now always beats an inline one, so a mounted secret cannot be shadowed by an inherited environment variable. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Dv6sqaY6Vq3ChZHMem3cnU
208 lines
12 KiB
JavaScript
208 lines
12 KiB
JavaScript
const { test } = require('node:test');
|
|
const assert = require('node:assert/strict');
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
|
|
const root = path.join(__dirname, '..');
|
|
|
|
function read(relativePath) {
|
|
return fs.readFileSync(path.join(root, relativePath), 'utf8');
|
|
}
|
|
|
|
test('AI memory context is limited to saved template categories', () => {
|
|
const route = read('src/routes/memories.js');
|
|
assert.match(route, /var AI_CONTEXT_CATEGORIES = \[/);
|
|
assert.match(route, /'physical_exam'/);
|
|
assert.match(route, /'template_ed'/);
|
|
assert.match(route, /rows = rows\.filter\(function\(r\) \{ return AI_CONTEXT_CATEGORIES\.indexOf\(r\.category\) !== -1; \}\)/);
|
|
assert.doesNotMatch(route.match(/var AI_CONTEXT_CATEGORIES = \[[\s\S]*?\];/)[0], /'custom'/);
|
|
});
|
|
|
|
test('template settings do not offer new custom AI memories', () => {
|
|
const settings = read('public/components/settings.html');
|
|
const memories = read('public/js/memories.js');
|
|
assert.doesNotMatch(settings, /<option value="custom">/);
|
|
assert.match(settings, /Only template categories are sent to AI/);
|
|
assert.match(memories, /custom: 'Custom \(not used by AI\)'/);
|
|
assert.match(memories, /window\.getUserMemoryContext = function\(\)/);
|
|
});
|
|
|
|
test('note refine corrections still call AI without storing learning memories', () => {
|
|
const app = read('public/js/app.js');
|
|
const refine = read('src/routes/refine.js');
|
|
assert.match(app, /function refineDocument\(outputElementId, inputElementId\)/);
|
|
assert.match(app, /fetch\('\/api\/refine'/);
|
|
assert.match(app, /currentDocument: docText, instructions: instructions/);
|
|
assert.match(refine, /router\.post\('\/refine'/);
|
|
assert.match(refine, /PROMPTS\.refine \+ INJECTION_GUARD/);
|
|
assert.match(refine, /sourceContext/);
|
|
assert.doesNotMatch(refine, /INSERT INTO user_memories|correction_/);
|
|
});
|
|
|
|
test('browser Whisper is removed from public runtime and user settings', () => {
|
|
assert.equal(fs.existsSync(path.join(root, 'public/js/browserWhisper.js')), false);
|
|
assert.equal(fs.existsSync(path.join(root, 'public/js/whisperWorker.js')), false);
|
|
assert.equal(fs.existsSync(path.join(root, 'public/js/whisperWorkerV2.js')), false);
|
|
assert.equal(fs.existsSync(path.join(root, 'docs/browser-whisper-setup.md')), false);
|
|
assert.equal(fs.existsSync(path.join(root, 'docs/browser-whisper-troubleshooting.md')), false);
|
|
assert.equal(fs.existsSync(path.join(root, 'scripts/download-whisper-models.sh')), false);
|
|
const publicRuntimeFiles = [
|
|
'public/index.html',
|
|
'public/js/app.js',
|
|
'public/js/transcriptionSettings.js',
|
|
'public/components/settings.html',
|
|
'public/components/faq.html'
|
|
];
|
|
publicRuntimeFiles.forEach((file) => {
|
|
assert.doesNotMatch(read(file), /BrowserWhisper|browser-whisper|Browser Whisper|Xenova\/whisper|transformers\.min\.js/, file);
|
|
});
|
|
const dockerfile = read('Dockerfile');
|
|
assert.doesNotMatch(dockerfile, /Xenova\/whisper|transformers\.min\.js|Browser Whisper/);
|
|
const server = read('server.js');
|
|
assert.doesNotMatch(server, /wasm-unsafe-eval|unsafe-eval|huggingface\.co|cdn-lfs|transformers/);
|
|
});
|
|
|
|
test('browser speech recognition is gated by explicit user setting', () => {
|
|
const app = read('public/js/app.js');
|
|
const speechFactory = app.match(/function createSpeechRecognition\(\) \{[\s\S]*?\n\}/)[0];
|
|
assert.match(speechFactory, /window\.WebSpeechRecognition && !window\.WebSpeechRecognition\.isEnabled\(\)/);
|
|
});
|
|
|
|
test('audio backup settings render without dynamic HTML templates', () => {
|
|
const audioBackup = read('public/js/audioBackup.js');
|
|
const renderer = audioBackup.match(/window\.renderAudioBackups = function\(\) \{[\s\S]*?\n \};/)[0];
|
|
assert.doesNotMatch(renderer, /innerHTML/);
|
|
assert.match(renderer, /document\.createElement\('button'\)/);
|
|
assert.match(renderer, /textContent =/);
|
|
});
|
|
|
|
// The Settings picker offered six hardcoded ids. On this gateway none of them
|
|
// resolve, and /api/transcribe prefers the user's choice over the admin
|
|
// default — so choosing one broke every recording with "Invalid model name".
|
|
test('the STT picker offers what the gateway has, not a hardcoded list', () => {
|
|
const stt = read('src/utils/sttProvider.js');
|
|
const prefs = read('src/routes/userPreferences.js');
|
|
|
|
assert.match(stt, /async function discoverSTTModels\(options\)/);
|
|
assert.match(stt, /getLiteLLMSTTModels\(resp\.data && resp\.data\.data\)/, 'filtered by audio_transcription mode');
|
|
assert.match(stt, /STT_DISCOVERY_TTL_MS = 5 \* 60 \* 1000;/, 'cached, so a user-facing page does not hit the gateway every load');
|
|
assert.match(stt, /module\.exports = \{[\s\S]{0,80}discoverSTTModels,/);
|
|
|
|
assert.match(prefs, /var sttIds = await discoverSTTModels\(\);/);
|
|
assert.match(prefs, /if \(!sttIds\.length\) sttIds = getSTTModelLists\(\)\.litellm\.slice\(\);/,
|
|
'the built-in list survives only as a fallback');
|
|
assert.match(prefs, /model === adminSttModel \? ' \(default\)' : ''/, 'the admin default is marked');
|
|
});
|
|
|
|
// A recording is significant clinical material: it must be possible to take a
|
|
// copy out of the app, and a recording that has silently stopped must say so.
|
|
test('recordings can be exported, and a dead recorder is reported', () => {
|
|
const backup = read('public/js/audioBackup.js');
|
|
assert.match(backup, /window\.downloadAudioBackup = function\(id, stamp\)/);
|
|
assert.match(backup, /'\/api\/audio-backups\/' \+ id\.replace\('server_', ''\) \+ '\/audio'/, 'server-side copies');
|
|
assert.match(backup, /objectStore\(STORE_NAME\)\.get\(localId\)/, 'and local ones');
|
|
// A local record belongs to one account; another must not be able to pull it.
|
|
assert.match(backup, /!boundary\.valid\(owner\) \|\| record\.owner !== owner/);
|
|
assert.match(backup, /audio-backup-download/, 'the list offers it');
|
|
assert.match(backup, /indexOf\('mp4'\) !== -1 \? 'm4a'/, 'the extension matches what was recorded');
|
|
|
|
const app = read('public/js/app.js');
|
|
assert.match(app, /self\.mediaRecorder\.onerror = function\(event\)/);
|
|
assert.match(app, /track\.addEventListener\('ended'/, 'the microphone being taken away is a failure too');
|
|
assert.match(app, /AudioRecorder\.prototype\.notifyFailure/);
|
|
assert.match(app, /if \(this\.notified\) return;/, 'reported once, not per chunk');
|
|
assert.match(app, /audio-recorder-failed/, 'callers can react');
|
|
});
|
|
|
|
test('a running recording holds the screen awake and survives a glance away', () => {
|
|
const app = read('public/js/app.js');
|
|
// The screen sleeping suspends the recording, and the browser drops a wake
|
|
// lock whenever the page is hidden — so it has to be taken again on return,
|
|
// or one glance away ends it for the rest of the session.
|
|
assert.match(app, /navigator\.wakeLock\.request\('screen'\)/);
|
|
assert.match(app, /document\.addEventListener\('visibilitychange', function\(\) \{\s*\n\s*if \(document\.visibilityState === 'visible'\) _acquireWakeLock\(\);/);
|
|
assert.match(app, /if \(document\.visibilityState !== 'visible'\) return Promise\.resolve\(null\);/,
|
|
'requesting while hidden would just be rejected');
|
|
// Counted, so two recorders do not release each other's lock.
|
|
assert.match(app, /_wakeLockHolders = Math\.max\(0, _wakeLockHolders - 1\);/);
|
|
assert.match(app, /if \(_wakeLockHolders > 0 \|\| !_wakeLock\) return;/);
|
|
// Signing out must not leave the screen pinned awake.
|
|
assert.match(app, /window\.addEventListener\('account-boundary', function\(\) \{\s*\n\s*_wakeLockHolders = 0;/);
|
|
// Denied or unsupported must not stop the recording.
|
|
assert.match(app, /\.catch\(function\(\) \{ return null; \}\);/);
|
|
});
|
|
|
|
test('starting an already-running recorder does not throw away what it has', () => {
|
|
const app = read('public/js/app.js');
|
|
assert.match(app, /if \(self\.mediaRecorder && self\.mediaRecorder\.state === 'recording'\) return Promise\.resolve\(\);/);
|
|
assert.match(app, /if \(self\.heldWakeLock\) \{ self\.heldWakeLock = false; releaseWakeLock\(\); \}/, 'and stopping releases the lock');
|
|
});
|
|
|
|
test('a recording that ends by itself is still transcribed, and logging out stops it', () => {
|
|
const live = read('public/js/liveEncounter.js');
|
|
// Same path as pressing Stop, so the audio is transcribed and stored rather
|
|
// than left in a tab that still claims to be recording.
|
|
assert.match(live, /document\.addEventListener\('audio-recorder-failed', function\(\) \{[\s\S]{0,160}recordBtn\.click\(\);/);
|
|
assert.match(live, /window\.addEventListener\('account-boundary', function\(\) \{[\s\S]{0,200}recorder\.stop\(\)/,
|
|
'signing out mid-recording stops it');
|
|
});
|
|
|
|
test('every recording is kept for 24 hours, not only the failures', () => {
|
|
const transcribe = read('src/routes/transcribe.js');
|
|
const store = read('src/utils/audioBackupStore.js');
|
|
const db = read('src/db/database.js');
|
|
|
|
// The audio is already on the server for transcription, so keeping it costs
|
|
// no second upload.
|
|
assert.match(transcribe, /require\('\.\.\/utils\/audioBackupStore'\)\.save\(req\.user\.id, req\.body\.module \|\| 'recording', req\.file\.buffer, mimeType\)/);
|
|
assert.match(transcribe, /console\.warn\('\[Transcribe\] backup failed \(transcription continues\)/,
|
|
'a storage failure must not lose the transcription someone is waiting for');
|
|
|
|
// Object storage when configured, the encrypted database column otherwise.
|
|
// Which one, and with what credentials, is resolved centrally now.
|
|
assert.match(store, /_client = objectStorage\.storeFor\('audio-backups', env\);/);
|
|
assert.match(store, /objectStorage\.isConfigured\('audio-backups', env\)/);
|
|
assert.match(store, /cryptoUtil\.encryptBuffer\(compressed\)/, 'compressed and encrypted either way');
|
|
assert.match(store, /'recordings\/' \+ userId \+ '\/'/, 'keys are scoped to their owner');
|
|
assert.match(store, /WHERE id = \$1 AND user_id = \$2 AND expires_at > NOW\(\)/, 'ownership and expiry are in the query');
|
|
assert.match(store, /cryptoUtil\.isEncryptedBuffer\(stored\) \? cryptoUtil\.decryptBuffer\(stored\) : stored/,
|
|
'rows written before encryption still read back');
|
|
|
|
// An expired row must take its object with it.
|
|
assert.match(db, /DELETE FROM audio_backups WHERE expires_at < NOW\(\) RETURNING storage_key/);
|
|
assert.match(db, /await store\.removeObject\(audio\.rows\[i\]\.storage_key\)/);
|
|
assert.match(db, /ALTER TABLE audio_backups ADD COLUMN IF NOT EXISTS storage_key TEXT;/,
|
|
'existing installations get the column too');
|
|
});
|
|
|
|
test('signing out mid-recording warns, and keeps the audio with its encounter', () => {
|
|
const app = read('public/js/app.js');
|
|
const auth = read('public/js/auth.js');
|
|
// Every running recorder is registered, so anything about to end the session
|
|
// can find one instead of discarding minutes of a consultation.
|
|
assert.match(app, /var _activeRecorders = new Set\(\);/);
|
|
assert.match(app, /_activeRecorders\.add\(self\);/);
|
|
assert.match(app, /_activeRecorders\.delete\(self\);/);
|
|
assert.match(app, /window\.rescueActiveRecordings = function\(\)/);
|
|
// Tagged with the module that produced it, which is what makes it findable
|
|
// afterwards: 'encounter', 'soap', 'dictation'.
|
|
assert.match(app, /var module = recorder\._module \|\| 'recording';/);
|
|
assert.match(app, /return saveAudioBackup\(blob, module\);/);
|
|
// The caller is on its way out of the app, so this must never reject.
|
|
assert.match(app, /\.catch\(function\(\) \{ return \{ module: module, id: null \}; \}\);/);
|
|
|
|
assert.match(auth, /activeRecordingCount\(\) > 0/);
|
|
assert.match(auth, /showConfirm\('A recording is still running\./);
|
|
assert.match(auth, /rescueActiveRecordings\(\)\.then/);
|
|
assert.match(auth, /\.finally\(function\(\) \{\s*\n\s*exitApp\(\);/, 'sign-out completes whether or not the save worked');
|
|
});
|
|
|
|
test('switching to the Assistant does not reload the page out from under a recording', () => {
|
|
const app = read('public/js/app.js');
|
|
const handler = app.slice(app.indexOf('if (!onAssistant) {'), app.indexOf('// Inside the assistant, Workspace opens the launcher'));
|
|
// window.location reloads the document, which ends any running recording.
|
|
assert.match(handler, /if \(typeof window\.activateTab === 'function'\) window\.activateTab\('assistant'\);/);
|
|
assert.match(handler, /else window\.location\.href = '\/assistant';/, 'the reload stays as a fallback');
|
|
// activateTab already rewrites the URL, so the address still reads /assistant.
|
|
assert.match(app, /var target = tabName === 'assistant' \? '\/assistant' : '\/';/);
|
|
});
|