-
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathattw.test.ts
More file actions
115 lines (98 loc) · 3.19 KB
/
attw.test.ts
File metadata and controls
115 lines (98 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import {describe, it, expect, beforeEach, afterEach} from 'vitest';
import {runAttw} from '../../analyze/attw.js';
import {TarballFileSystem} from '../../tarball-file-system.js';
import {LocalFileSystem} from '../../local-file-system.js';
import {createTempDir, cleanupTempDir, createTestPackage} from '../utils.js';
import {pack as packAsTarball} from '@publint/pack';
import fs from 'node:fs/promises';
import path from 'node:path';
describe('runAttw', () => {
let tempDir: string;
beforeEach(async () => {
tempDir = await createTempDir();
});
afterEach(async () => {
await cleanupTempDir(tempDir);
});
async function createTarballFromPackage(
packageData: any,
files: Record<string, string> = {}
): Promise<ArrayBuffer> {
await createTestPackage(tempDir, packageData);
for (const [fileName, content] of Object.entries(files)) {
const filePath = path.join(tempDir, fileName);
const dirPath = path.dirname(filePath);
await fs.mkdir(dirPath, {recursive: true});
await fs.writeFile(filePath, content);
}
const tarballPath = await packAsTarball(tempDir, {
packageManager: 'npm',
ignoreScripts: true,
destination: tempDir
});
const buffer = await fs.readFile(tarballPath);
return buffer.buffer.slice(
buffer.byteOffset,
buffer.byteOffset + buffer.byteLength
) as ArrayBuffer;
}
it('should return empty result for non-TarballFileSystem', async () => {
const localFileSystem = new LocalFileSystem(tempDir);
const result = await runAttw(localFileSystem);
expect(result).toEqual({
messages: []
});
});
it('should suggest when no type definitions found', async () => {
const tarball = await createTarballFromPackage(
{
name: 'test-package',
version: '1.0.0',
type: 'module',
exports: {
'.': './index.js'
}
},
{
'index.js': 'export const test = "hello";'
}
);
const fileSystem = new TarballFileSystem(tarball);
const result = await runAttw(fileSystem);
expect(result.messages).toHaveLength(1);
expect(result.messages[0]).toEqual({
severity: 'suggestion',
score: 0,
message: 'No type definitions found.'
});
});
it('should detect TypeScript issues in package with types', async () => {
const tarball = await createTarballFromPackage(
{
name: 'test-package',
version: '1.0.0',
type: 'module',
types: './index.d.ts',
exports: {
'.': {
types: './index.d.ts',
import: './index.js'
}
}
},
{
'index.js': 'export const test = "hello";',
'index.d.ts': 'export declare const different: string;'
}
);
const fileSystem = new TarballFileSystem(tarball);
const result = await runAttw(fileSystem);
expect(result.messages.length).toBeGreaterThanOrEqual(0);
for (const message of result.messages) {
expect(['error', 'warning', 'suggestion']).toContain(message.severity);
expect(message.score).toBe(0);
expect(typeof message.message).toBe('string');
expect(message.message.length).toBeGreaterThan(0);
}
});
});