mirror of
https://github.com/shumaiOne/shumai.git
synced 2026-11-30 21:04:59 +00:00
refactor: extract core service domain layer to @shumai/core
This commit is contained in:
@@ -0,0 +1,118 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import * as fs from 'fs'
|
||||
import * as path from 'path'
|
||||
import { LocalStorageService, S3StorageService } from './s3'
|
||||
|
||||
// Mock the AWS SDK
|
||||
const s3ClientConstructorSpy = vi.fn()
|
||||
vi.mock('@aws-sdk/client-s3', () => {
|
||||
return {
|
||||
S3Client: class {
|
||||
constructor(params: unknown) {
|
||||
s3ClientConstructorSpy(params)
|
||||
}
|
||||
send = vi.fn().mockResolvedValue({})
|
||||
},
|
||||
HeadObjectCommand: class {},
|
||||
PutObjectCommand: class {},
|
||||
GetObjectCommand: class {},
|
||||
ListObjectsV2Command: class {},
|
||||
}
|
||||
})
|
||||
|
||||
vi.mock('@aws-sdk/s3-request-presigner', () => {
|
||||
return {
|
||||
getSignedUrl: vi.fn().mockResolvedValue('http://presigned-url'),
|
||||
}
|
||||
})
|
||||
|
||||
describe('S3Service implementations', () => {
|
||||
describe('S3StorageService', () => {
|
||||
it('should correctly build standard endpoints', () => {
|
||||
const s3 = new S3StorageService('s3.example.com', 'key', 'secret', 'test-bucket', true)
|
||||
expect(s3).toBeDefined()
|
||||
expect(s3ClientConstructorSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ region: 'auto' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('should use provided region', () => {
|
||||
s3ClientConstructorSpy.mockClear()
|
||||
const s3 = new S3StorageService(
|
||||
's3.example.com',
|
||||
'key',
|
||||
'secret',
|
||||
'test-bucket',
|
||||
true,
|
||||
'ap-singapore',
|
||||
)
|
||||
expect(s3).toBeDefined()
|
||||
expect(s3ClientConstructorSpy).toHaveBeenCalledWith(
|
||||
expect.objectContaining({ region: 'ap-singapore' }),
|
||||
)
|
||||
})
|
||||
|
||||
it('should implement presign correctly', async () => {
|
||||
const s3 = new S3StorageService('localhost:9000', 'key', 'secret', 'test-bucket', false)
|
||||
const url = await s3.presign('bucket', 'key', 'GET')
|
||||
expect(url).toBe('http://presigned-url')
|
||||
})
|
||||
})
|
||||
|
||||
describe('LocalStorageService', () => {
|
||||
const TEST_BASE_PATH = path.join(process.cwd(), 'data-test', 's3', 'data')
|
||||
let localS3: LocalStorageService
|
||||
|
||||
beforeEach(() => {
|
||||
if (fs.existsSync(TEST_BASE_PATH)) {
|
||||
fs.rmSync(TEST_BASE_PATH, { recursive: true, force: true })
|
||||
}
|
||||
fs.mkdirSync(TEST_BASE_PATH, { recursive: true })
|
||||
localS3 = new LocalStorageService('http://localhost:3000', TEST_BASE_PATH)
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
if (fs.existsSync(TEST_BASE_PATH)) {
|
||||
fs.rmSync(TEST_BASE_PATH, { recursive: true, force: true })
|
||||
}
|
||||
})
|
||||
|
||||
it('should implement putObject and getObjectSize correctly', async () => {
|
||||
await localS3.putObject('test-bucket', 'test.txt', 'hello world', 11)
|
||||
const size = await localS3.getObjectSize('test-bucket', 'test.txt')
|
||||
expect(size).toBe(11)
|
||||
})
|
||||
|
||||
it('should list objects', async () => {
|
||||
await localS3.putObject('test-bucket', 'dir1/file1.txt', 'abc', 3)
|
||||
await localS3.putObject('test-bucket', 'dir1/file2.txt', 'def', 3)
|
||||
await localS3.putObject('test-bucket', 'dir2/file3.txt', 'ghi', 3)
|
||||
|
||||
const keys1 = await localS3.listObjects('test-bucket', 'dir1')
|
||||
expect(keys1.length).toBe(2)
|
||||
expect(keys1).toContain('dir1/file1.txt')
|
||||
expect(keys1).toContain('dir1/file2.txt')
|
||||
|
||||
const keysAll = await localS3.listObjects('test-bucket', '')
|
||||
expect(keysAll.length).toBe(3)
|
||||
})
|
||||
|
||||
it('should handle headObject correctly', async () => {
|
||||
await localS3.putObject('test-bucket', 'test.txt', 'hello world', 11)
|
||||
const head = await localS3.headObject('test-bucket', 'test.txt')
|
||||
|
||||
expect(head.key).toBe('test.txt')
|
||||
expect(head.size).toBe(11)
|
||||
expect(head.eTag).toContain('"')
|
||||
})
|
||||
|
||||
it('should generate a local presign URL', async () => {
|
||||
const url = await localS3.presign('my-bucket', 'dir/file.txt', 'GET')
|
||||
expect(url).toBe('http://localhost:3000/files/my-bucket/dir/file.txt')
|
||||
})
|
||||
|
||||
it('should throw an error for unsupported presign methods', async () => {
|
||||
await expect(localS3.presign('b', 'k', 'POST')).rejects.toThrow()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user