Skip to content

Commit

Permalink
Factor out local forms functions
Browse files Browse the repository at this point in the history
Add test to make sure pdfs are actually generated
  • Loading branch information
zakpatterson committed Dec 19, 2021
1 parent 914cd3a commit e228e6d
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 136 deletions.
6 changes: 4 additions & 2 deletions src/core/pdfFiller/pdfHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ import { PDFDocument } from 'pdf-lib'
import Fill from './Fill'
import { fillPDF } from './fillPdf'

export interface PDFDownloader {
(url: string): Promise<PDFDocument>
export interface FileDownloader<T> {
(url: string): Promise<T>
}

export type PDFDownloader = FileDownloader<PDFDocument>

export const downloadPDF: PDFDownloader = async (url) => {
const download = await fetch(url)
const buffer = await download.arrayBuffer()
Expand Down
36 changes: 36 additions & 0 deletions src/core/tests/LocalForms.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { PDFDocument } from 'pdf-lib'
import {
FileDownloader,
PDFDownloader
} from 'ustaxes/core/pdfFiller/pdfHandler'
import fs from 'fs/promises'
import path from 'path'
import { TaxYear } from 'ustaxes/data'

const localPath = (url: string) => path.join(__dirname, '../../../public', url)

export const localFiles: FileDownloader<Uint8Array> = (
url: string
): Promise<Uint8Array> => fs.readFile(localPath(url))

const compiledPDFs: { [key: string]: PDFDocument } = {}

export const localPDFs =
(y: TaxYear): PDFDownloader =>
async (url: string): Promise<PDFDocument> => {
const fileUrl = `/forms/${y}/${url}`

const lookedUpAt = localPath(fileUrl)

if (lookedUpAt in compiledPDFs) {
return compiledPDFs[lookedUpAt]
}

const bytes = await localFiles(fileUrl)

const pdf = await PDFDocument.load(bytes.buffer)

compiledPDFs[lookedUpAt] = pdf

return pdf
}
30 changes: 0 additions & 30 deletions src/core/tests/common/LocalForms.ts

This file was deleted.

10 changes: 5 additions & 5 deletions src/forms/Y2019/tests/common/F1040.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Form from '../../irsForms/Form'
import { create1040 } from '../../irsForms/Main'
import { isRight } from 'ustaxes/core/util'
import * as arbitraries from 'ustaxes/core/tests/arbitraries'
import { localPDFs } from './LocalForms'
import { localPDFs } from 'ustaxes/core/tests/LocalForms'
import fs from 'fs/promises'
import path from 'path'
import { PDFDocument } from 'pdf-lib'
Expand All @@ -20,9 +20,9 @@ export const log1040Err = async (
err: any
): Promise<void> => {
try {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2019'))
const saveDirName = `Errors ${new Date().getUTCHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`
const saveRoot = path.resolve(__dirname, '../../../logs/errors')
const saveRoot = path.resolve(__dirname, '../../../../../logs/errors')
const saveDir = path.resolve(saveRoot, saveDirName)
await fs.mkdir(saveDir, { recursive: true })
await Promise.all(
Expand Down Expand Up @@ -65,7 +65,7 @@ export const with1040Pdfs = async (
): Promise<void> =>
await with1040Assert(
async ([, forms], info) => {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2019'))
f(pdfs, info)
},
logErr,
Expand All @@ -74,7 +74,7 @@ export const with1040Pdfs = async (

export const with1040Property = (
f: (f1040: [F1040, Form[]], info: Information) => Promise<void>,
year = 2020
year = 2019
): fc.IAsyncPropertyWithHooks<[Information]> =>
fc.asyncProperty(
new arbitraries.Arbitraries(year).information(),
Expand Down
22 changes: 0 additions & 22 deletions src/forms/Y2019/tests/common/LocalForms.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/forms/Y2019/tests/f1040.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { with1040Assert } from './common/F1040'
import { yearFormBuilder } from 'ustaxes/forms/YearForms'
import { isRight } from 'ustaxes/core/util'
import { localPDFs } from 'ustaxes/core/tests/LocalForms'

jest.setTimeout(40000)

beforeAll(() => {
jest.spyOn(console, 'warn').mockImplementation((x: string) => {
if (!x.includes('Removing XFA form data as pdf-lib')) {
console.warn(x)
}
})
})

describe('f1040', () => {
it('should be created in', async () => {
Expand Down Expand Up @@ -35,4 +48,17 @@ describe('f1040', () => {
}
})
})

it('should create a PDF without failing', async () => {
await with1040Assert(async ([f1040]) => {
const forms = await yearFormBuilder('Y2019', f1040.info)
.setDownloader(localPDFs('Y2019'))
.build()
.f1040Pdfs()
expect(isRight(forms)).toEqual(true)
if (isRight(forms)) {
expect(forms.right).not.toHaveLength(0)
}
})
})
})
8 changes: 4 additions & 4 deletions src/forms/Y2020/tests/common/F1040.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Form from '../../irsForms/Form'
import { create1040 } from '../../irsForms/Main'
import { isRight } from 'ustaxes/core/util'
import * as arbitraries from 'ustaxes/core/tests/arbitraries'
import { localPDFs } from './LocalForms'
import { localPDFs } from 'ustaxes/core/tests/LocalForms'
import fs from 'fs/promises'
import path from 'path'
import { PDFDocument } from 'pdf-lib'
Expand All @@ -20,9 +20,9 @@ export const log1040Err = async (
err: any
): Promise<void> => {
try {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2020'))
const saveDirName = `Errors ${new Date().getUTCHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`
const saveRoot = path.resolve(__dirname, '../../../logs/errors')
const saveRoot = path.resolve(__dirname, '../../../../logs/errors')
const saveDir = path.resolve(saveRoot, saveDirName)
await fs.mkdir(saveDir, { recursive: true })
await Promise.all(
Expand Down Expand Up @@ -65,7 +65,7 @@ export const with1040Pdfs = async (
): Promise<void> =>
await with1040Assert(
async ([, forms], info) => {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2020'))
f(pdfs, info)
},
logErr,
Expand Down
22 changes: 0 additions & 22 deletions src/forms/Y2020/tests/common/LocalForms.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/forms/Y2020/tests/f1040.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { localPDFs } from 'ustaxes/core/tests/LocalForms'
import { isRight } from 'ustaxes/core/util'
import { yearFormBuilder } from 'ustaxes/forms/YearForms'
import { with1040Assert } from './common/F1040'

jest.setTimeout(40000)

beforeAll(() => {
jest.spyOn(console, 'warn').mockImplementation((x: string) => {
if (!x.includes('Removing XFA form data as pdf-lib')) {
console.warn(x)
}
})
})

describe('f1040', () => {
it('should be created in', async () => {
await with1040Assert(async ([f1040, forms]) => {
Expand Down Expand Up @@ -35,4 +48,17 @@ describe('f1040', () => {
}
})
})

it('should create a PDF without failing', async () => {
await with1040Assert(async ([f1040]) => {
const forms = await yearFormBuilder('Y2020', f1040.info)
.setDownloader(localPDFs('Y2020'))
.build()
.f1040Pdfs()
expect(isRight(forms)).toEqual(true)
if (isRight(forms)) {
expect(forms.right).not.toHaveLength(0)
}
})
})
})
8 changes: 4 additions & 4 deletions src/forms/Y2021/tests/common/F1040.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Form from '../../irsForms/Form'
import { create1040 } from '../../irsForms/Main'
import { isRight } from 'ustaxes/core/util'
import * as arbitraries from 'ustaxes/core/tests/arbitraries'
import { localPDFs } from './LocalForms'
import { localPDFs } from 'ustaxes/core/tests/LocalForms'
import fs from 'fs/promises'
import path from 'path'
import { PDFDocument } from 'pdf-lib'
Expand All @@ -20,9 +20,9 @@ export const log1040Err = async (
err: any
): Promise<void> => {
try {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2021'))
const saveDirName = `Errors ${new Date().getUTCHours()}:${new Date().getMinutes()}:${new Date().getSeconds()}`
const saveRoot = path.resolve(__dirname, '../../../logs/errors')
const saveRoot = path.resolve(__dirname, '../../../../logs/errors')
const saveDir = path.resolve(saveRoot, saveDirName)
await fs.mkdir(saveDir, { recursive: true })
await Promise.all(
Expand Down Expand Up @@ -65,7 +65,7 @@ export const with1040Pdfs = async (
): Promise<void> =>
await with1040Assert(
async ([, forms], info) => {
const pdfs = await insertFormDataToPdfs(forms, localPDFs)
const pdfs = await insertFormDataToPdfs(forms, localPDFs('Y2021'))
f(pdfs, info)
},
logErr,
Expand Down
22 changes: 0 additions & 22 deletions src/forms/Y2021/tests/common/LocalForms.ts

This file was deleted.

26 changes: 26 additions & 0 deletions src/forms/Y2021/tests/f1040.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { localPDFs } from 'ustaxes/core/tests/LocalForms'
import { isRight } from 'ustaxes/core/util'
import { yearFormBuilder } from 'ustaxes/forms/YearForms'
import { with1040Assert } from './common/F1040'

jest.setTimeout(40000)

beforeAll(() => {
jest.spyOn(console, 'warn').mockImplementation((x: string) => {
if (!x.includes('Removing XFA form data as pdf-lib')) {
console.warn(x)
}
})
})

describe('f1040', () => {
it('should be created in', async () => {
await with1040Assert(async ([f1040, forms]) => {
Expand Down Expand Up @@ -35,4 +48,17 @@ describe('f1040', () => {
}
})
})

it('should create a PDF without failing', async () => {
await with1040Assert(async ([f1040]) => {
const forms = await yearFormBuilder('Y2021', f1040.info)
.setDownloader(localPDFs('Y2021'))
.build()
.f1040Pdfs()
expect(isRight(forms)).toEqual(true)
if (isRight(forms)) {
expect(forms.right).not.toHaveLength(0)
}
})
})
})
Loading

0 comments on commit e228e6d

Please sign in to comment.