| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import test from 'node:test'
- import assert from 'node:assert/strict'
- import fs from 'node:fs'
- import vm from 'node:vm'
- import { createRequire } from 'node:module'
- import { buildMinioAttachmentUrl, minioAttachmentPath } from '../src/utils/minioAttachment.js'
- const base = 'https://files.example.test/attachments/'
- const path = '/2026/10/09/workOrder/jpg/4649512327058833952.jpg'
- test('相对路径展示时补 MinIO 基址,存储时保持相对路径', () => {
- assert.equal(buildMinioAttachmentUrl(path, base), base + path.slice(1))
- assert.equal(buildMinioAttachmentUrl(path.slice(1), base), base + path.slice(1))
- assert.equal(minioAttachmentPath(base + path.slice(1), base), path)
- assert.equal(minioAttachmentPath(path, base), path)
- assert.equal(buildMinioAttachmentUrl('', base), '')
- assert.equal(buildMinioAttachmentUrl('https://legacy.test/a.jpg', base), 'https://legacy.test/a.jpg')
- })
- test('上传、回显、追加和删除不将展示 URL 回写到工单', () => {
- const source = fs.readFileSync(new URL('../src/components/ImageUpload/index.vue', import.meta.url), 'utf8')
- const script = source.match(/<script setup>([\s\S]*?)<\/script>/)[1]
- .replace(/^import .*;\r?\n/gm, '')
- .replaceAll('import.meta.env.VITE_APP_BASE_API', "'/api'")
- const props = { modelValue: path, relativePath: true, uploadUrl: '/api/minio/upload?dir=workOrder' }
- const emitted = []
- let refresh
- const context = vm.createContext({
- defineProps: () => props,
- defineEmits: () => (event, value) => emitted.push(value),
- getCurrentInstance: () => ({ proxy: { $modal: { closeLoading() {} } } }),
- getToken: () => 'test',
- ref: value => ({ value }),
- computed: getter => ({ get value() { return getter() } }),
- watch: (getter, callback) => { refresh = () => callback(getter()); refresh() },
- buildMinioAttachmentUrl: value => buildMinioAttachmentUrl(value, base),
- minioAttachmentPath: value => minioAttachmentPath(value, base)
- })
- vm.runInContext(script, context)
- assert.equal(vm.runInContext('fileList.value[0].url', context), base + path.slice(1))
- vm.runInContext('number.value = 1; handleUploadSuccess({ code: 200, relativePath: "/2026/10/09/workOrder/jpg/next.jpg" }, { uid: 2 })', context)
- assert.equal(emitted.at(-1), path + ',/2026/10/09/workOrder/jpg/next.jpg')
- props.modelValue = emitted.at(-1)
- refresh()
- vm.runInContext('handleDelete(fileList.value[0])', context)
- assert.equal(emitted.at(-1), '/2026/10/09/workOrder/jpg/next.jpg')
- props.relativePath = false
- props.modelValue = '/profile/normal.jpg'
- refresh()
- assert.equal(vm.runInContext('fileList.value[0].url', context), '/api/profile/normal.jpg')
- assert.equal(vm.runInContext('listToString(fileList.value)', context), '/profile/normal.jpg')
- })
- test('工单页面与上传组件可通过 Vue SFC 编译', () => {
- const require = createRequire(import.meta.url)
- const { parse, compileScript, compileTemplate } = require('@vue/compiler-sfc')
- for (const file of ['../src/components/ImageUpload/index.vue', '../src/views/subSystem/pipeNetwork/ops/workOrder/dispose.vue']) {
- const filename = new URL(file, import.meta.url).pathname
- const { descriptor, errors } = parse(fs.readFileSync(new URL(file, import.meta.url), 'utf8'), { filename })
- assert.deepEqual(errors, [])
- const compiled = compileScript(descriptor, { id: 'test' })
- const template = compileTemplate({ source: descriptor.template.content, filename, id: 'test', compilerOptions: { bindingMetadata: compiled.bindings } })
- assert.deepEqual(template.errors, [])
- }
- })
|