minioAttachment.test.mjs 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. import test from 'node:test'
  2. import assert from 'node:assert/strict'
  3. import fs from 'node:fs'
  4. import vm from 'node:vm'
  5. import { createRequire } from 'node:module'
  6. import { buildMinioAttachmentUrl, minioAttachmentPath } from '../src/utils/minioAttachment.js'
  7. const base = 'https://files.example.test/attachments/'
  8. const path = '/2026/10/09/workOrder/jpg/4649512327058833952.jpg'
  9. test('相对路径展示时补 MinIO 基址,存储时保持相对路径', () => {
  10. assert.equal(buildMinioAttachmentUrl(path, base), base + path.slice(1))
  11. assert.equal(buildMinioAttachmentUrl(path.slice(1), base), base + path.slice(1))
  12. assert.equal(minioAttachmentPath(base + path.slice(1), base), path)
  13. assert.equal(minioAttachmentPath(path, base), path)
  14. assert.equal(buildMinioAttachmentUrl('', base), '')
  15. assert.equal(buildMinioAttachmentUrl('https://legacy.test/a.jpg', base), 'https://legacy.test/a.jpg')
  16. })
  17. test('上传、回显、追加和删除不将展示 URL 回写到工单', () => {
  18. const source = fs.readFileSync(new URL('../src/components/ImageUpload/index.vue', import.meta.url), 'utf8')
  19. const script = source.match(/<script setup>([\s\S]*?)<\/script>/)[1]
  20. .replace(/^import .*;\r?\n/gm, '')
  21. .replaceAll('import.meta.env.VITE_APP_BASE_API', "'/api'")
  22. const props = { modelValue: path, relativePath: true, uploadUrl: '/api/minio/upload?dir=workOrder' }
  23. const emitted = []
  24. let refresh
  25. const context = vm.createContext({
  26. defineProps: () => props,
  27. defineEmits: () => (event, value) => emitted.push(value),
  28. getCurrentInstance: () => ({ proxy: { $modal: { closeLoading() {} } } }),
  29. getToken: () => 'test',
  30. ref: value => ({ value }),
  31. computed: getter => ({ get value() { return getter() } }),
  32. watch: (getter, callback) => { refresh = () => callback(getter()); refresh() },
  33. buildMinioAttachmentUrl: value => buildMinioAttachmentUrl(value, base),
  34. minioAttachmentPath: value => minioAttachmentPath(value, base)
  35. })
  36. vm.runInContext(script, context)
  37. assert.equal(vm.runInContext('fileList.value[0].url', context), base + path.slice(1))
  38. vm.runInContext('number.value = 1; handleUploadSuccess({ code: 200, relativePath: "/2026/10/09/workOrder/jpg/next.jpg" }, { uid: 2 })', context)
  39. assert.equal(emitted.at(-1), path + ',/2026/10/09/workOrder/jpg/next.jpg')
  40. props.modelValue = emitted.at(-1)
  41. refresh()
  42. vm.runInContext('handleDelete(fileList.value[0])', context)
  43. assert.equal(emitted.at(-1), '/2026/10/09/workOrder/jpg/next.jpg')
  44. props.relativePath = false
  45. props.modelValue = '/profile/normal.jpg'
  46. refresh()
  47. assert.equal(vm.runInContext('fileList.value[0].url', context), '/api/profile/normal.jpg')
  48. assert.equal(vm.runInContext('listToString(fileList.value)', context), '/profile/normal.jpg')
  49. })
  50. test('工单页面与上传组件可通过 Vue SFC 编译', () => {
  51. const require = createRequire(import.meta.url)
  52. const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc')
  53. for (const file of ['../src/components/ImageUpload/index.vue', '../src/views/subSystem/pipeNetwork/ops/workOrder/dispose.vue']) {
  54. const filename = new URL(file, import.meta.url).pathname
  55. const { descriptor, errors } = parse(fs.readFileSync(new URL(file, import.meta.url), 'utf8'), { filename })
  56. assert.deepEqual(errors, [])
  57. const compiled = compileScript(descriptor, { id: 'test' })
  58. const template = compileTemplate({ source: descriptor.template.content, filename, id: 'test', compilerOptions: { bindingMetadata: compiled.bindings } })
  59. assert.deepEqual(template.errors, [])
  60. }
  61. })