-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add generate election package script (#5721)
* add generate-election-packages script with test when it needs regen * make default latest metadata * add version of electionPrimaryPrecinctSplits without ballot strings * all new fixtures generated files * fixtures build:resources * export newly added fixtures for use in package generator test * address pr comments * fix refactor
- Loading branch information
1 parent
20456bc
commit d9f5d9a
Showing
32 changed files
with
5,756 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
60 changes: 60 additions & 0 deletions
60
libs/backend/src/language_and_audio/election_package_string.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { electionPrimaryPrecinctSplitsFixtures } from '@votingworks/fixtures'; | ||
import { LanguageCode, BallotLanguageConfigs } from '@votingworks/types'; | ||
import { assert } from '@votingworks/basics'; | ||
import { getAllStringsForElectionPackage } from './election_package_strings'; | ||
import { GoogleCloudTranslator } from './translator'; | ||
import { MockGoogleCloudTranslationClient } from './test_utils'; | ||
|
||
const allBallotLanguages: BallotLanguageConfigs = [ | ||
{ | ||
languages: [ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
], | ||
}, | ||
]; | ||
|
||
describe('getAllStringsForElectionPackage', () => { | ||
it('should extract and translate election strings correctly for english only', async () => { | ||
const translationClient = new MockGoogleCloudTranslationClient(); | ||
const mockTranslator = new GoogleCloudTranslator({ translationClient }); | ||
const [appStrings, hmpbStrings, electionStrings] = | ||
await getAllStringsForElectionPackage( | ||
electionPrimaryPrecinctSplitsFixtures.election, | ||
mockTranslator, | ||
allBallotLanguages | ||
); | ||
|
||
expect(appStrings).toBeDefined(); | ||
expect(Object.keys(appStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(appStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(appStrings[LanguageCode.ENGLISH])).toHaveLength(427); | ||
|
||
expect(hmpbStrings).toBeDefined(); | ||
expect(Object.keys(hmpbStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(hmpbStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(hmpbStrings[LanguageCode.ENGLISH])).toHaveLength(30); | ||
|
||
expect(electionStrings).toBeDefined(); | ||
expect(Object.keys(electionStrings)).toEqual([ | ||
LanguageCode.ENGLISH, | ||
LanguageCode.CHINESE_SIMPLIFIED, | ||
LanguageCode.CHINESE_TRADITIONAL, | ||
LanguageCode.SPANISH, | ||
]); | ||
assert(electionStrings[LanguageCode.ENGLISH]); | ||
expect(Object.keys(electionStrings[LanguageCode.ENGLISH])).toHaveLength(14); | ||
}); | ||
}); |
36 changes: 36 additions & 0 deletions
36
libs/backend/src/language_and_audio/election_package_strings.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
BallotLanguageConfigs, | ||
Election, | ||
UiStringsPackage, | ||
} from '@votingworks/types'; | ||
import { GoogleCloudTranslator } from './translator'; | ||
import { translateAppStrings } from './app_strings'; | ||
import { translateHmpbStrings } from './ballot_strings'; | ||
import { extractAndTranslateElectionStrings } from './election_strings'; | ||
|
||
/** | ||
* Helper function to generate all necessary strings used in an election package. | ||
* Returns three packages of strings: app strings, HMPB strings, and election strings. | ||
*/ | ||
export async function getAllStringsForElectionPackage( | ||
election: Election, | ||
translator: GoogleCloudTranslator, | ||
ballotLanguageConfigs: BallotLanguageConfigs | ||
): Promise<[UiStringsPackage, UiStringsPackage, UiStringsPackage]> { | ||
const appStrings = await translateAppStrings( | ||
translator, | ||
'latest', | ||
ballotLanguageConfigs | ||
); | ||
const hmpbStrings = await translateHmpbStrings( | ||
translator, | ||
ballotLanguageConfigs | ||
); | ||
const electionStrings = await extractAndTranslateElectionStrings( | ||
translator, | ||
election, | ||
ballotLanguageConfigs | ||
); | ||
|
||
return [appStrings, hmpbStrings, electionStrings]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env node | ||
|
||
require('esbuild-runner').install({ | ||
type: 'transform', | ||
}); | ||
|
||
require('../src/cli/generate-election-package') | ||
.main(process.argv, { | ||
stdin: process.stdin, | ||
stdout: process.stdout, | ||
stderr: process.stderr, | ||
}) | ||
.catch((e) => { | ||
console.error(e); | ||
process.exit(1); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#!/usr/bin/env bash | ||
|
||
# Generates the saved election package fixtures in libs/fixtures | ||
# By default it will reuse the translations and audio from previous runs if new strings | ||
# are not detected to be translated. To force re-translation, set the environment variable | ||
# FORCE_RETRANSLATE to true. | ||
export NODE_ENV=development | ||
|
||
# Check if FORCE_RETRANSLATE is set to true | ||
if [ -z "$FORCE_RETRANSLATE" ]; then | ||
./bin/generate-election-package -e ../fixtures/data/electionPrimaryPrecinctSplits/electionBase.json -o ../fixtures/data/electionPrimaryPrecinctSplits/ -p ../fixtures/data/electionPrimaryPrecinctSplits/election-package-default-system-settings.zip --isMultiLanguage | ||
else | ||
./bin/generate-election-package -e ../fixtures/data/electionPrimaryPrecinctSplits/electionBase.json -o ../fixtures/data/electionPrimaryPrecinctSplits/ --isMultiLanguage | ||
fi | ||
|
||
echo | ||
echo "Note: You need to run \`pnpm build:resources && pnpm build\` in libs/fixtures for the new fixtures to register" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
"clean": "pnpm --filter $npm_package_name... clean:self", | ||
"clean:self": "rm -rf build && tsc --build --clean tsconfig.build.json", | ||
"generate-cvr-fixtures": "./bin/generate-cvr-fixtures", | ||
"generate-election-packages": "./bin/regenerate-election-packages", | ||
"lint": "pnpm type-check && eslint .", | ||
"lint:fix": "pnpm type-check && eslint . --fix", | ||
"test": "is-ci test:ci test:watch", | ||
|
@@ -29,18 +30,22 @@ | |
"@votingworks/basics": "workspace:*", | ||
"@votingworks/fixtures": "workspace:*", | ||
"@votingworks/fs": "workspace:*", | ||
"@votingworks/hmpb": "workspace:*", | ||
"@votingworks/image-utils": "workspace:*", | ||
"@votingworks/types": "workspace:*", | ||
"@votingworks/utils": "workspace:*", | ||
"debug": "4.3.4", | ||
"esbuild": "0.21.2", | ||
"esbuild-runner": "2.2.2", | ||
"js-sha256": "^0.9.0", | ||
"jszip": "^3.9.1", | ||
"nanoid": "^3.3.7", | ||
"uuid": "9.0.1", | ||
"yargs": "17.7.1", | ||
"zod": "3.23.5" | ||
}, | ||
"devDependencies": { | ||
"@types/debug": "4.1.8", | ||
"@types/jest": "^29.5.3", | ||
"@types/node": "20.16.0", | ||
"@types/tmp": "0.2.4", | ||
|
@@ -55,5 +60,8 @@ | |
"tmp": "^0.2.1", | ||
"ts-jest": "29.1.1" | ||
}, | ||
"engines": { | ||
"node": ">= 12" | ||
}, | ||
"packageManager": "[email protected]" | ||
} | ||
} |
Oops, something went wrong.