@mytmpvpn/mytmpvpn-client
Version:
MyTmpVpn Client Library
181 lines (180 loc) • 8.64 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
const appconfig = __importStar(require("../src/appconfig"));
const common_1 = require("./common");
const auth_1 = require("../src/auth");
describe('Testing referral APIs', () => {
let client;
const appConfig = appconfig.loadDefaultAppConfig();
const userProfile = (0, auth_1.createTestUserProfile)();
beforeAll(async () => {
client = await (0, common_1.createAuthUser)(appConfig, userProfile);
});
afterAll(async () => {
await (0, common_1.deleteCleanUpAllAndDeleteAuthUser)(client, appConfig, userProfile);
});
describe('getReferralCode', () => {
it('should generate a referral code', async () => {
const referralCodeResponse = await client.getReferralCode();
expect(referralCodeResponse).toBeDefined();
expect(typeof referralCodeResponse.referralCode).toBe('string');
expect(referralCodeResponse.referralCode.length).toBe(8);
expect(referralCodeResponse.referralCode).toMatch(/^[A-Z0-9]{8}$/);
});
it('should return the same code on subsequent calls', async () => {
const response1 = await client.getReferralCode();
const response2 = await client.getReferralCode();
expect(response1.referralCode).toBe(response2.referralCode);
});
});
describe('validateReferralCode', () => {
it('should reject invalid referral code format', async () => {
const invalidCode = 'SHORT';
const response = await client.validateReferralCode(invalidCode);
expect(response).toBeDefined();
expect(response.isValid).toBe(false);
expect(response.error).toBeDefined();
expect(typeof response.error).toBe('string');
});
it('should reject referral code with invalid characters', async () => {
const invalidCode = 'INVALID@';
const response = await client.validateReferralCode(invalidCode);
expect(response).toBeDefined();
expect(response.isValid).toBe(false);
expect(response.error).toBeDefined();
});
it('should reject own referral code', async () => {
const ownCodeResponse = await client.getReferralCode();
const validationResponse = await client.validateReferralCode(ownCodeResponse.referralCode);
expect(validationResponse).toBeDefined();
expect(validationResponse.isValid).toBe(false);
expect(validationResponse.error).toBeDefined();
expect(validationResponse.error).toContain('own referral code');
});
it('should handle empty referral code', async () => {
const response = await client.validateReferralCode('');
expect(response).toBeDefined();
expect(response.isValid).toBe(false);
expect(response.error).toBeDefined();
});
});
describe('getReferralStats', () => {
it('should return referral statistics', async () => {
const stats = await client.getReferralStats();
expect(stats).toBeDefined();
expect(typeof stats.totalReferrals).toBe('number');
expect(typeof stats.totalRewardsEarned).toBe('number');
expect(stats.totalReferrals).toBeGreaterThanOrEqual(0);
expect(stats.totalRewardsEarned).toBeGreaterThanOrEqual(0);
});
});
describe('getReferralHistoryPaginated', () => {
it('should return paginated referral history with default options', async () => {
const response = await client.getReferralHistory();
expect(response).toBeDefined();
expect(response.history).toBeDefined();
expect(response.pagination).toBeDefined();
expect(Array.isArray(response.history)).toBe(true);
expect(typeof response.pagination.hasMore).toBe('boolean');
// Validate history items
response.history.forEach(item => {
expect(item.relationshipId).toBeDefined();
expect(typeof item.relationshipId).toBe('string');
expect(item.referralCode).toBeDefined();
expect(typeof item.referralCode).toBe('string');
expect(item.createdAt).toBeDefined();
expect(typeof item.createdAt).toBe('string');
expect(typeof item.referrerReward).toBe('number');
expect(typeof item.refereeReward).toBe('number');
});
});
it('should return paginated referral history with custom limit', async () => {
const limit = 1;
const response = await client.getReferralHistory({ limit });
expect(response).toBeDefined();
expect(response.history.length).toBeLessThanOrEqual(limit);
});
it('should handle pagination with nextToken', async () => {
const firstPage = await client.getReferralHistory({ limit: 1 });
if (firstPage.pagination.nextToken) {
const secondPage = await client.getReferralHistory({
limit: 1,
nextToken: firstPage.pagination.nextToken
});
expect(secondPage).toBeDefined();
expect(secondPage.history).toBeDefined();
expect(Array.isArray(secondPage.history)).toBe(true);
}
});
});
describe('error handling', () => {
it('should handle network errors gracefully', async () => {
// This test would require mocking network failures
// For now, we'll test that the methods don't throw unexpected errors
try {
await client.getReferralCode();
await client.getReferralStats();
await client.getReferralHistory();
}
catch (error) {
// If errors occur, they should be proper MyTmpVpnError instances
expect(error).toBeInstanceOf(Error);
}
});
it('should validate input parameters', async () => {
// Test null/undefined handling
try {
await client.validateReferralCode(null);
}
catch (error) {
expect(error).toBeInstanceOf(Error);
}
});
});
describe('integration with existing functionality', () => {
it('should work alongside other client methods', async () => {
// Test that referral methods don't interfere with existing functionality
const peanutsBalance = await client.getPeanutsBalance();
const referralStats = await client.getReferralStats();
const locations = await client.listLocations();
expect(peanutsBalance).toBeDefined();
expect(referralStats).toBeDefined();
expect(locations).toBeDefined();
expect(Array.isArray(locations)).toBe(true);
});
it('should maintain authentication state', async () => {
// Ensure referral methods use proper authentication
const session = client.getSession();
expect(session).toBeDefined();
const stats = await client.getReferralStats();
expect(stats).toBeDefined();
// Session should still be valid after referral API calls
const sessionAfter = client.getSession();
expect(sessionAfter).toBeDefined();
expect(sessionAfter).toBe(session);
});
});
});