WhatapClient.integration.test.tsβ’4.44 kB
/**
* WhatapClient Integration Tests
*
* Tests with real WhaTap API
* Requires test credentials in .env.test
*/
import * as fs from 'fs/promises';
import * as path from 'path';
import * as os from 'os';
import { WhatapClient } from '../../../src/core/client/WhatapClient';
import { AuthManager } from '../../../src/core/auth/AuthManager';
import { SessionStore } from '../../../src/core/auth/SessionStore';
// Check if integration tests should run
const runIntegrationTests =
process.env.RUN_INTEGRATION_TESTS === 'true' &&
process.env.WHATAP_TEST_EMAIL &&
process.env.WHATAP_TEST_PASSWORD;
const describeIf = runIntegrationTests ? describe : describe.skip;
describeIf('WhatapClient (Integration Tests)', () => {
let testDir: string;
let sessionStore: SessionStore;
let authManager: AuthManager;
let whatapClient: WhatapClient;
const credentials = {
email: process.env.WHATAP_TEST_EMAIL!,
password: process.env.WHATAP_TEST_PASSWORD!,
serviceUrl: process.env.WHATAP_SERVICE_URL || 'https://service.whatap.io',
};
beforeAll(async () => {
// Create temporary test directory
testDir = path.join(os.tmpdir(), `whatap-test-${Date.now()}`);
await fs.mkdir(testDir, { recursive: true });
sessionStore = new SessionStore(testDir);
authManager = new AuthManager(sessionStore);
// Login
console.log('π Logging in to WhaTap...');
await authManager.login(credentials);
console.log('β
Login successful');
// Create WhatapClient
whatapClient = new WhatapClient(authManager);
}, 60000); // 60 second timeout for login
afterAll(async () => {
// Clean up
try {
await fs.rm(testDir, { recursive: true, force: true });
} catch (error) {
// Ignore cleanup errors
}
});
test('should get list of projects', async () => {
const projects = await whatapClient.getProjects();
expect(projects).toBeDefined();
expect(Array.isArray(projects)).toBe(true);
if (projects.length > 0) {
const project = projects[0];
expect(project).toHaveProperty('pcode');
expect(project).toHaveProperty('name');
expect(project).toHaveProperty('type');
console.log('β
Found projects:', projects.length);
console.log(` First project: ${project.name} (pcode: ${project.pcode})`);
}
}, 30000);
test('should execute MXQL query if projects exist', async () => {
const projects = await whatapClient.getProjects();
if (projects.length === 0) {
console.log('β οΈ No projects found, skipping MXQL test');
return;
}
const pcode = projects[0].pcode;
const now = Date.now();
const oneHourAgo = now - 3600000;
const result = await whatapClient.executeMxql({
pcode,
mql: 'CATEGORY app_counter',
stime: oneHourAgo,
etime: now,
limit: 10,
});
expect(result).toBeDefined();
expect(result).toHaveProperty('ok');
expect(result).toHaveProperty('code');
expect(result).toHaveProperty('data');
console.log('β
MXQL query executed successfully');
console.log(` Response code: ${result.code}`);
console.log(` OK: ${result.ok}`);
console.log(` Data type: ${typeof result.data}`);
}, 30000);
test('should get specific project details', async () => {
const projects = await whatapClient.getProjects();
if (projects.length === 0) {
console.log('β οΈ No projects found, skipping project detail test');
return;
}
const pcode = projects[0].pcode;
const project = await whatapClient.getProject(pcode);
expect(project).toBeDefined();
expect(project.pcode).toBe(pcode);
expect(project).toHaveProperty('name');
expect(project).toHaveProperty('type');
console.log('β
Project details retrieved');
console.log(` Name: ${project.name}`);
console.log(` Type: ${project.type}`);
}, 30000);
});
// If integration tests are skipped, log a message
if (!runIntegrationTests) {
describe('WhatapClient (Integration Tests - SKIPPED)', () => {
test('integration tests skipped', () => {
console.log('β οΈ Integration tests skipped');
console.log(' To run integration tests:');
console.log(' 1. Copy .env.example to .env.test');
console.log(' 2. Set RUN_INTEGRATION_TESTS=true');
console.log(' 3. Set WHATAP_TEST_EMAIL and WHATAP_TEST_PASSWORD');
expect(true).toBe(true);
});
});
}