Skip to content

Commit

Permalink
total cleanup of tests
Browse files Browse the repository at this point in the history
  • Loading branch information
austenstone committed Apr 26, 2024
1 parent 374c934 commit 7cb9b2a
Show file tree
Hide file tree
Showing 13 changed files with 1,310 additions and 232 deletions.
185 changes: 33 additions & 152 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,163 +1,44 @@
import * as process from 'process';
import * as path from 'path';
import { test } from '@jest/globals';
import dotenv from 'dotenv'
dotenv.config({ override: true })

interface _SummaryPrivate {
_buffer: string,
_filePath?: string;
}

// import run from "../src/run";
import { writeFileSync } from 'fs';
import { createJobSummaryUsage } from '../src/job-summary';
// existsSync, readFileSync, unlinkSync,
const addInput = (key, value) => process.env[`INPUT_${key.replace(/ /g, '-').toUpperCase()}`] = value || ''
// const removeInput = (key) => delete process.env[`INPUT_${key.replace(/ /g, '-').toUpperCase()}`]

const input: any = {
'github-token': process.env.GITHUB_TOKEN,
'job-summary': 'false',
'csv': 'false',
'xml': 'false',
'ACTIONS_RUNTIME_TOKEN': 'token',
import { createJobSummarySeatAssignments, createJobSummarySeatInfo, createJobSummaryUsage } from '../src/job-summary';
import { summary } from '@actions/core/lib/summary';
import { exampleResponseEnterprise, exampleResponseOrg, exampleResponseTeam, exampleSeatAssignmentResponse, exampleSeatInfoResponse } from './mock/mock-data';
import { readFileSync } from 'fs';

const getSummaryBuffer = (_summary: typeof summary): string => {
return (_summary as unknown as {
_buffer: string,
_filePath?: string;
})._buffer
}

const organization = process.env.GITHUB_ORG || process.env.GITHUB_REPOSITORY?.split('/')[0];
if (!organization) {
writeFileSync('.env', 'GITHUB_TOKEN=ghp_123\nGITHUB_ORG=org');
throw new Error('GITHUB_ORG or GITHUB_REPOSITORY is required');
}

beforeEach(() => {
Object.keys(process.env).forEach(key => {
if (key.startsWith('INPUT_')) delete process.env[key];
});
Object.entries(input).forEach(([key, value]) => addInput(key, value));
process.env['GITHUB_REPOSITORY'] = `austenstone/${path.basename(process.cwd())}`;
const fileName = 'copilot-usage.md';
writeFileSync(fileName, '');
process.env['GITHUB_STEP_SUMMARY'] = fileName;
test('createJobSummaryUsage(enterpriseUsage)', async () => {
const summary = await createJobSummaryUsage(exampleResponseEnterprise);
expect(summary).toBeDefined();
expect(getSummaryBuffer(summary)).toEqual(readFileSync('./__tests__/mock/enterprise-usage-summary.md', 'utf-8'));
});

test('run with github organization', async () => {
addInput('organization', organization);
const summary = await createJobSummaryUsage([
{
day: '01-01-2024',
total_suggestions_count: 123,
total_acceptances_count: 123,
total_lines_suggested: 132,
total_lines_accepted: 132,
total_active_users: 123,
total_chat_acceptances: 123,
total_chat_turns: 123,
total_active_chat_users: 123,
breakdown: [
{
language: 'python',
editor: 'vscode',
suggestions_count: 1,
acceptances_count: 1,
lines_suggested: 1,
lines_accepted: 1,
active_users: 1,
}
]
}
]);
console.log((summary as unknown as _SummaryPrivate)._buffer)
test('createJobSummaryUsage(orgUsage)', async () => {
const summary = await createJobSummaryUsage(exampleResponseOrg);
expect(summary).toBeDefined();
expect(getSummaryBuffer(summary)).toEqual(readFileSync('./__tests__/mock/org-usage-summary.md', 'utf-8'));
});

// test('run with github team', async () => {
// addInput('organization', organization);
// addInput('team', 'corporate-solutions-eng');
// await run()
// });

// test('run with github enterprise', async () => {
// addInput('enterprise', organization); // same name as organization
// await run()
// });

// test('run job summary', async () => {
// const fileName = 'copilot-usage.md';
// writeFileSync(fileName, '');
// process.env['GITHUB_STEP_SUMMARY'] = fileName;
// addInput('organization', organization);
// addInput('job-summary', 'true');
// await run();
// expect(existsSync(fileName)).toBe(true);
// expect(readFileSync(fileName).toString()).toContain('Copilot Usage');
// unlinkSync(fileName);
// });

// test('run csv', async () => {
// const fileName = "copilot-usage.csv";
// const numDays = 20;
// addInput('organization', organization);
// addInput('csv', 'true');
// addInput('days', numDays.toString());
// await run();
// expect(existsSync(fileName)).toBe(true);
// const csv = readFileSync(fileName).toString();
// expect(csv).toContain('day,total_suggestions_count,total_acceptances_count,total_lines_suggested,total_lines_accepted,total_active_users,total_chat_acceptances,total_chat_turns,total_active_chat_users,breakdown');
// expect(csv.split('\n').length).toEqual(numDays);
// unlinkSync(fileName);
// });

// test('run with xml', async () => {
// addInput('organization', organization);
// addInput('xml', 'true');
// await run();
// expect(existsSync('copilot-usage.xml')).toBe(true);
// unlinkSync('copilot-usage.xml');
// });

// test('run with no org, team, or enterprise', async () => {
// await expect(run()).rejects.toThrow('organization, enterprise or team is required');
// });

// test('run with no token', async () => {
// removeInput('github-token');
// await expect(run()).rejects.toThrow('github-token is required');
// });

// test('get only 7 days of data', async () => {
// const fileName = "copilot-usage.csv";
// const numDays = 7;
// addInput('organization', organization);
// addInput('csv', 'true');
// addInput('days', numDays.toString());
// await run();
// expect(existsSync(fileName)).toBe(true);
// const csv = readFileSync(fileName).toString();
// expect(csv.split('\n').length - 1).toEqual(numDays);
// });
test('createJobSummaryUsage(teamUsage)', async () => {
const summary = await createJobSummaryUsage(exampleResponseTeam);
expect(summary).toBeDefined();
expect(getSummaryBuffer(summary)).toEqual(readFileSync('./__tests__/mock/team-usage-summary.md', 'utf-8'));
});

// test('get data since a specific date', async () => {
// const fileName = "copilot-usage.csv";
// const since = new Date(new Date().setDate(new Date().getDate() - 7)).toISOString().split('T')[0];
// addInput('organization', organization);
// addInput('csv', 'true');
// addInput('since', since);
// await run();
// expect(existsSync(fileName)).toBe(true);
// const csv = readFileSync(fileName).toString();
// expect(csv.split('\n').length - 1).toEqual(7);
// });
test('createJobSummarySeatInfo(orgSeatInfo)', async () => {
const summary = await createJobSummarySeatInfo(exampleSeatInfoResponse);
expect(summary).toBeDefined();
expect(getSummaryBuffer(summary)).toEqual(readFileSync('./__tests__/mock/org-seat-info-summary.md', 'utf-8'));
});

// test('get data since and until a specific date', async () => {
// const fileName = "copilot-usage.csv";
// const since = new Date(new Date().setDate(new Date().getDate() - 7)).toISOString().split('T')[0];
// const until = new Date(new Date().setDate(new Date().getDate() - 1)).toISOString().split('T')[0];
// addInput('organization', organization);
// addInput('csv', 'true');
// addInput('since', since);
// addInput('until', until);
// await run();
// expect(existsSync(fileName)).toBe(true);
// const csv = readFileSync(fileName).toString();
// expect(csv.split('\n').length - 1).toEqual(6);
// });
test('createJobSummarySeatAssignments(orgSeatAssignments)', async () => {
const summary = await createJobSummarySeatAssignments(exampleSeatAssignmentResponse);
expect(summary).toBeDefined();
expect(getSummaryBuffer(summary)).toEqual(readFileSync('./__tests__/mock/org-seat-assignments-summary.md', 'utf-8'));
});
70 changes: 70 additions & 0 deletions __tests__/mock/enterprise-usage-summary.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<h1>Copilot Usage<br>10/14/2023 - 10/15/2023</h1>
<h3>Suggestions: 10,200</h3>
<h3>Acceptances: 8,100</h3>
<h3>Acceptance Rate: 79.41%</h3>
<h3>Lines of Code Accepted: 8,500</h3>

```mermaid
---
config:
xyChart:
width: 90
height: 500
xAxis:
labelPadding: 20
themeVariables:
xyChart:
backgroundColor: "transparent"
---
xychart-beta
title "Accepts & Acceptance Rate"
x-axis ["10/14", "10/15"]
y-axis "Acceptances" 0 --> 5110
bar [3000, 5100]
line [3066, 5011.730769230769]
```

```mermaid
---
config:
xyChart:
width: 90
height: 500
xAxis:
labelPadding: 20
themeVariables:
xyChart:
backgroundColor: "transparent"
---
xychart-beta
title "Daily Active Users"
x-axis ["10/14", "10/15"]
y-axis "Active Users" 0 --> 25
line [15, 15]
```
<h1>Language Usage</h1>

```mermaid
pie showData
title Language Usage
"undefined" : 10200
```
<table><tr><th>Language</th><th>Suggestions</th><th>Acceptances</th><th>Acceptance Rate</th><th>Lines Suggested</th><th>Lines Accepted</th><th>Active Users</th></tr><tr><td>undefined</td><td>10,200</td><td>7,900</td><td>77.45%</td><td>12,500</td><td>8,700</td><td>30</td></tr></table>
<h1>Editor Usage</h1>

```mermaid
pie showData
title Editor Usage
"undefined" : 10200
```
<table><tr><th>Editor</th><th>Suggestions</th><th>Acceptances</th><th>Acceptance Rate</th><th>Lines Suggested</th><th>Lines Accepted</th><th>Active Users</th></tr><tr><td>undefined</td><td>10,200</td><td>7,900</td><td>77.45%</td><td>12,500</td><td>8,700</td><td>30</td></tr></table>
<h1>Daily Usage</h1>
<h3>The most active day was 10/15/2023 with 15 active users.</h3>
<h3>The day with the highest acceptance rate was 10/15/2023 with an acceptance rate of 98.08%.</h3>

```mermaid
pie showData
title Suggestions by Day of the Week
"undefined" : 10200
```
<table><tr><th>Day</th><th>Suggestions</th><th>Acceptances</th><th>Acceptance Rate</th><th>Lines Suggested</th><th>Lines Accepted</th><th>Active Users</th><th>Chat Acceptances</th><th>Chat Turns</th><th>Active Chat Users</th></tr><tr><td>10/14/2023</td><td>5,000</td><td>3,000</td><td>60.00%</td><td>7,000</td><td>3,500</td><td>15</td><td>45</td><td>350</td><td>8</td></tr><tr><td>10/15/2023</td><td>5,200</td><td>5,100</td><td>98.08%</td><td>5,300</td><td>5,000</td><td>15</td><td>57</td><td>455</td><td>12</td></tr></table>
Loading

0 comments on commit 7cb9b2a

Please sign in to comment.