const test = require('node:test')
const assert = require('node:assert')
const fs = require('fs')
const path = require('path')
const os = require('os')
const { resolveLocalImagePath } = require('../src/lib/media')

test('resolveLocalImagePath matches file when basename uses U+202F before AM/PM but user typed a space', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-media-'))
  const id = Date.now()
  const realName = `x-${id}-9.53.14\u202fPM.png`
  const realPath = path.join(dir, realName)
  fs.writeFileSync(realPath, 'x')

  const userPath = path.join(dir, `x-${id}-9.53.14 PM.png`)
  assert.strictEqual(resolveLocalImagePath(userPath), realPath)
})

test('resolveLocalImagePath strips wrapping quotes', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-media-'))
  const id = Date.now()
  const realPath = path.join(dir, `z-${id}.png`)
  fs.writeFileSync(realPath, 'x')
  assert.strictEqual(resolveLocalImagePath(`"${realPath}"`), realPath)
})

test('resolveLocalImagePath strips backslashes before spaces (shell-style)', () => {
  const dir = fs.mkdtempSync(path.join(os.tmpdir(), 'bild-media-'))
  const id = Date.now()
  const realPath = path.join(dir, `a-${id} two words.png`)
  fs.writeFileSync(realPath, 'x')
  const escaped = path.join(dir, `a-${id}\\ two\\ words.png`)
  assert.strictEqual(resolveLocalImagePath(escaped), realPath)
})
