Forked from vadimfedenko/analyze-images
scripts / native-attachment-smoke.cjs
const assert = require('node:assert/strict');
const fs = require('node:fs/promises');
const path = require('node:path');
const os = require('node:os');
const {ChatMessage, Chat} = require('@lmstudio/sdk');
const {inlineImage} = require('../dist/inlineImage');
const {persistChatImage} = require('../dist/persistentImage');
const {migrateInlineImages} = require('../dist/inlineMigration');
(async () => {
const storage = await fs.mkdtemp(path.join(os.tmpdir(), 'aia-native-files-'));
const bytes = Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwMCAO+aWQAAAABJRU5ErkJggg==', 'base64');
const client = {files: {
getLocalFileAbsolutePath: async name => ({path: path.join(storage, name)}),
prepareImageBase64: () => {throw Error('Must not use expiring uploads');},
}};
const inline = inlineImage(client, 'aia-original.png', bytes);
const saved = await persistChatImage(client, inline);
assert(!saved.identifier.includes(':'));
assert.equal(path.basename(saved.identifier), saved.identifier);
assert.equal(saved.name, inline.name);
assert.deepEqual(await fs.readFile(await saved.getFilePath()), bytes);
// Serialize/reload with a fresh connection, then reproduce native branch copying.
const disk = JSON.stringify({messages: [{role: 'user', content: [
{type: 'text', text: 'Original question'},
{type: 'file', identifier: saved.identifier, name: saved.name, sizeBytes: saved.sizeBytes, fileType: saved.type},
]}]});
const freshClient = {files: {getLocalFileAbsolutePath: client.files.getLocalFileAbsolutePath}};
const reloaded = Chat.from(JSON.parse(disk)).getMessagesArray()[0].getFiles(freshClient)[0];
const destination = path.join(storage, 'branched.png');
await fs.copyFile(path.join(storage, reloaded.identifier), destination);
assert.deepEqual(await fs.readFile(destination), bytes);
assert.deepEqual(await fs.readFile(await reloaded.getFilePath()), bytes);
assert.equal(await persistChatImage(client, saved), saved);
const badPathClient = {files: {getLocalFileAbsolutePath: async () => ({path: path.join(storage, 'unrelated')})}};
await assert.rejects(persistChatImage(badPathClient, inline), /Unexpected/);
const malformed = ChatMessage.from({role:'user',content:[{type:'file',name:'bad.png',identifier:'base64:not a valid image',sizeBytes:1,fileType:'image'}]}).getFiles(client)[0];
await assert.rejects(persistChatImage(client, malformed), /Invalid/);
const raw = JSON.stringify({messages: [{versions: [{content: [{type:'text',text:'Keep all text'},
{type:'file',identifier:inline.identifier,name:inline.name,sizeBytes:inline.sizeBytes,fileType:'image'}]}]}],
workingDirectoryName:'unchanged',plugins:['keep'],draft:'keep draft'});
const migrated = await migrateInlineImages(raw, client);
assert.equal(migrated.changed, 1);
const data = JSON.parse(migrated.raw);
const ref = data.messages[0].versions[0].content[1];
await fs.copyFile(path.join(storage,ref.identifier), path.join(storage,'migrated-branch.png'));
assert.deepEqual(await fs.readFile(path.join(storage,'migrated-branch.png')), bytes);
ref.identifier = inline.identifier;
assert.deepEqual(data,JSON.parse(raw)); // Only the attachment reference changed.
assert.equal((await migrateInlineImages(migrated.raw,client)).changed,0);
assert.equal((await migrateInlineImages(migrated.raw,client)).raw,migrated.raw);
console.log('PASS: native saved attachment survives serialization/new client, native branch copy and replay, with exact bytes/name, no expiring uploads and path validation.');
})().catch(error => {console.error(error);process.exitCode=1;});